一 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