TIANKONGHAIHUO

导航

背景平铺

ViewController.m

//

//  ViewController.m

//  6A04.北京平铺

//

//  Created by huan on 16/1/30.

//  Copyright © 2016 huanxi. All rights reserved.

//

 

#import "ViewController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    //添加控制器的背景

    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"textBg.jpg"]];

    

    //0.bundlenew文件获取文字

    //0.1 获取文件路径

    NSString *path = [[NSBundle mainBundle] pathForResource:@"news" ofType:nil];

    //0.2 读取里面的文字

    NSString *news = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

    

    //1.把文字显示上去 UITextView 显示多行数据,并且可编辑

    UITextView *textView = [[UITextView alloc] initWithFrame:self.view.bounds];

    //设置文本

    textView.text = news;

    //设置TextView的背景

#warning 图片有缓存,clear iOS8中图片不放在Suporting File下,iOS9没有影响

    textView.backgroundColor = [UIColor clearColor];

    //设置文字的大小

//    textView.font = [UIFont systemFontOfSize:20];

    //也可以这样获得字体

    textView.font = [UIFont fontWithName:@"Verdana" size:20];

    // 当前系统可用的字体

    NSLog(@"%@", [UIFont familyNames]);

    textView.alpha = 0.5;

    [self.view addSubview:textView];

    //2.设置文字背景

    //2.1 画一个虚线的背景图片

    UIImage *dashBgImage = [self dashBgImage];

    //2.2 画一个虚线的背景图片平铺(第一种)

//    textView.backgroundColor = [UIColor colorWithPatternImage:dashBgImage];

    //2.2 创建一个ImageView,添加TextView的第一位置

    UIImageView *bgImageView = [[UIImageView alloc] init];

    // 背景设置大小的时候,是根据 文字的字数和字体的大小决定

    //文字需要的尺寸

    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;

    CGSize textNeedSize = [news boundingRectWithSize:CGSizeMake(screenW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:textView.font} context:nil].size;

    bgImageView.frame = CGRectMake(0, 0, textNeedSize.width, textNeedSize.height);

    bgImageView.backgroundColor = [UIColor colorWithPatternImage:dashBgImage];

    [textView insertSubview:bgImageView atIndex:0];

}

 

#pragma mark 带虚线的背景图片

-(UIImage *)dashBgImage{

    //使用位图上下文

    CGFloat bgW = [UIScreen mainScreen].bounds.size.width;

    CGFloat bgH = 22;

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(bgW, bgH), NO, 0.0);

    CGContextRef bitmapContext = UIGraphicsGetCurrentContext();

    //画虚线

    //计算线的位置

    CGFloat lineH = 1;

    CGFloat lineY = bgH - lineH;

    

    //设置虚线每段的距离长度

    [[UIColor redColor]set];

    CGFloat lengths[2] = {10, 5};

    CGContextSetLineDash(bitmapContext, 0, lengths, 2);

    CGPoint points[2] = {{0, lineY}, {bgW, lineY}};

    CGContextAddLines(bitmapContext, points, 2);

    CGContextStrokePath(bitmapContext);

    //获取背景图片

    UIImage *bgImage = UIGraphicsGetImageFromCurrentImageContext();

    //结束编辑

    UIGraphicsEndImageContext();

    return bgImage;

}

@end

posted on 2016-02-02 16:10  日月行程  阅读(139)  评论(0编辑  收藏  举报