背景平铺

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

转载于:https://www.cnblogs.com/Lu2015-10-03/p/5177815.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值