#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"textBg"]];
NSString *path = [[NSBundle mainBundle] pathForResource:@"news" ofType:nil];
NSString *news = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
UITextView *textView = [[UITextView alloc] initWithFrame:self.view.bounds];
textView.text = news;
textView.font = [UIFont fontWithName:@"Verdana" size:20];
NSLog(@"%@",[UIFont familyNames]);
#warning 图片不放在suportingfile,有可能是图片问题,图片放在images.xcassests
textView.backgroundColor = [UIColor clearColor];
textView.alpha = 0.5;
[self.view addSubview:textView];
UIImage *dashBgImage = [self dashBgImage];
UIImageView *bgImageView = [[UIImageView alloc] init];
CGFloat screnW = [UIScreen mainScreen].bounds.size.width;
CGSize textNeedSize = [news boundingRectWithSize:CGSizeMake(screnW, 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;
CGFloat lengths[3] = {20,5,10};
[[UIColor blueColor] set];
CGContextSetLineDash(bitmapContext, 0, lengths, 3);
CGPoint points[2] = {{0,lineY},{bgW,lineY}};
CGContextAddLines(bitmapContext, points, 2);
CGContextStrokePath(bitmapContext);
UIImage *bgImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return bgImage;
}
@end