一 UILabel浏览器
1.左右对齐:textAlignmentapp
//水平居中对齐 [_textLabel setTextAlignment:NSTextAlignmentCenter]; //最后一行天然对齐 [_textLabel setTextAlignment:NSTextAlignmentJustified];
2.字体:font字体
3.行数:numberOfLinesatom
//0不限制行数 [_textLabel setNumberOfLines:0];
4.阴影颜色:shadowColorurl
阴影偏移值:shadowOffsetspa
[_label setShadowColor:[UIColor purpleColor]]; [_label setShadowOffset:CGSizeMake(1, -1)];
5.换行模式:NSLineBreakModecode
[_textLabel setLineBreakMode:NSLineBreakByWordWrapping];
经常使用:ip
UILineBreakModeWordWrap = 0,
以单词为单位换行,以单位为单位截断。
UILineBreakModeCharacterWrap,
以字符为单位换行,以字符为单位截断。
UILineBreakModeClip,
以单词为单位换行。以字符为单位截断。
实例:文本浏览器的简单应用字符串
@interface ViewController () @property (nonatomic, strong) NSString *contentString; @property (nonatomic, strong) UILabel *textLabel; @property (nonatomic, strong) NSMutableArray *eachPagesRangeArray; @property (nonatomic, assign) NSInteger nowRangeNumber; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.eachPagesRangeArray = [[NSMutableArray alloc] init]; _nowRangeNumber = 0; [self initDataSource]; [self createLabel]; [self calculateEachPageRangeWithFont:[UIFont fontWithName:@"Menlo" size:15]]; } - (void)initDataSource{ //设置背景 UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:self.view.bounds]; [bgImageView setImage:[UIImage imageNamed:@"djfoj"]]; // [bgImageView setContentMode:UIViewContentModeScaleAspectFill]; [self.view addSubview:bgImageView]; //读取文件 //1.获取文件的路径 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"text" ofType:@"txt"]; //读取文件的内容 self.contentString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; NSLog(@"%@", _contentString); } - (void)createLabel{ //建立显示文字的Label self.textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 440)]; _textLabel.text = @"点击开始阅读"; [_textLabel setLineBreakMode:NSLineBreakByWordWrapping]; [_textLabel setTextAlignment:NSTextAlignmentJustified]; [_textLabel setTextAlignment:NSTextAlignmentCenter]; [_textLabel setNumberOfLines:0]; [self.view addSubview:_textLabel]; } - (void)calculateEachPageRangeWithFont:(UIFont *)font { NSRange range = NSMakeRange(0, 0); //判断字符串是否结束 while (range.location + range.length < self.contentString.length) { //游标移动一个字符 range.length ++; //获取当前这个range对应的子字符串 NSString *subString = [self.contentString substringWithRange:range]; //计算这个子字符串所占的高度和宽度 CGSize bigSize = CGSizeMake(self.textLabel.frame.size.width, 2000); NSDictionary *dic = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]; CGRect realSize = [subString boundingRectWithSize:bigSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine attributes:dic context:nil]; // NSLog(@"%.1f %.1f %.1f %.1f %.1f", realSize.origin.x,realSize.origin.y,realSize.size.width, realSize.size.height, _textLabel.frame.size.height); if (realSize.size.height > (_textLabel.frame.size.height)) { range.length --; [_eachPagesRangeArray addObject:[NSValue valueWithRange:range]]; range.location = range.location + range.length; range.length = 0; // NSLog(@"%@", _eachPagesRangeArray); } } //判断最后一页是否为满 [_eachPagesRangeArray addObject:[NSValue valueWithRange:range]]; // NSLog(@"%@", _eachPagesRangeArray); } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ NSRange eachRange = [_eachPagesRangeArray[_nowRangeNumber] rangeValue]; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.5]; [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:_textLabel cache:NO]; [_textLabel setText:[_contentString substringWithRange:eachRange]]; [UIView commitAnimations]; if (_nowRangeNumber == _eachPagesRangeArray.count - 1) { _nowRangeNumber = 0; }else{ _nowRangeNumber++; } } @end