這里主要分兩部分來介紹文本相關(guān)的內(nèi)容
1,自己封裝能夠滿足需求的類來實現(xiàn)需求
2,使用三方庫(如YYText)實現(xiàn)比較復(fù)雜的需求
宗旨:能自己實現(xiàn)的就不要去用別人的東西.
自行封裝滿足需求的情況:如下圖

WechatIMG9058.jpeg
需求:行間距為10,label的最后一行不要行間距,顯示行數(shù)有時全部顯示,有時只顯示特定行數(shù)(如圖)
實現(xiàn)思路:寫一個UILabel的分類“UILabel+SX.h”并為其添加兩個屬性lineSpacing(行間距)及ConstrainedWidth(行寬),開放下面的方法接口用于實現(xiàn)label的高度計算及各屬性設(shè)置
- (CGFloat)getTextHeightWithNumberOfLines:(NSInteger)numberOfLines lineSpacing:(CGFloat)lineSpacing constrainedWidth:(CGFloat)constrainedWidth
{
if (!self.text || self.text.length == 0) {
return 0;
}
self.numberOfLines = numberOfLines;
self.LineSpacing = lineSpacing;
self.ConstrainedWidth = constrainedWidth;
CGSize textSize = [self.text textSizeWithFont:self.font numberOfLines:self.numberOfLines lineSpacing:self.LineSpacing constrainedWidth:constrainedWidth];
//單行的情況
if (fabs(textSize.height - self.font.lineHeight) < 0.00001f) {
self.LineSpacing = 0.0f;
}
//設(shè)置文字的屬性
NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:self.text];
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:self.LineSpacing];
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;//結(jié)尾部分的內(nèi)容以……方式省略
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self.text length])];
//注釋
[attributedString addAttribute:NSForegroundColorAttributeName value:self.textColor range:NSMakeRange(0, [self.text length])];
[attributedString addAttribute:NSFontAttributeName value:self.font range:NSMakeRange(0, [self.text length])];
[self setAttributedText:attributedString];
return textSize.height;
}
其中
CGSize textSize = [self.text textSizeWithFont:self.font numberOfLines:self.numberOfLines lineSpacing:self.LineSpacing constrainedWidth:constrainedWidth];
此方法是創(chuàng)建的NSString的分類"NSString+SX.h"中的方法,用于計算label.text的真實size
- (CGSize)textSizeWithFont:(UIFont*)font
numberOfLines:(NSInteger)numberOfLines
lineSpacing:(CGFloat)lineSpacing
constrainedWidth:(CGFloat)constrainedWidth{
if (self.length == 0) {
return CGSizeZero;
}
CGFloat oneLineHeight = font.lineHeight;
CGSize textSize = [self boundingRectWithSize:CGSizeMake(constrainedWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
CGFloat rows = textSize.height / oneLineHeight;
CGFloat realHeight = oneLineHeight;
// 0 不限制行數(shù)
if (numberOfLines == 0) {
if (rows >= 1) {
realHeight = (rows * oneLineHeight) + (rows - 1) * lineSpacing;
}
}else{
if (rows > numberOfLines) {
rows = numberOfLines;
}
realHeight = (rows * oneLineHeight) + (rows - 1) * lineSpacing;
}
return CGSizeMake(constrainedWidth, realHeight);
}
簡單的使用如下:
self.customLabel1 = [[UILabel alloc]init];
self.customLabel1.text = @"此label用于演示文本及富文本按照特定行數(shù)顯示或全部顯示的問題,此label用于演示文本及富文本按照特定行數(shù)顯示或全部顯示的問題,此label用于演示文本及富文本按照特定行數(shù)顯示或全部顯示的問題,此label用于演示文本及富文本按照特定行數(shù)顯示或全部顯示的問題,此label用于演示文本及富文本按照特定行數(shù)顯示或全部顯示的問題,";
self.customLabel1.font = [UIFont systemFontOfSize:16];
// NumberOfLines :展示的行數(shù)(0時全部顯示)、行間距、行寬獲取label的真實高度
CGFloat customLabel1Height = [self.customLabel1 getTextHeightWithNumberOfLines:3 lineSpacing:10 constrainedWidth:ScreenWidth - 30];
self.customLabel1.frame = CGRectMake(15, 50, ScreenWidth - 30, customLabel1Height);
[scrollView addSubview:self.customLabel1];
效果如下(numberOfLines=3、numberOfLines=0)

WechatIMG9163 1.png
然后看看富文本的需求:效果如下(紅色背景部分)

WechatIMG9067.png
除了基本設(shè)置之外,還有超鏈接(點擊藍色字體執(zhí)行相應(yīng)事件)、圖片等相應(yīng)的富文本要求
/* YYLabel */
NSString *text = @"無論你高考考了多少分,能不能去你想去的大學,都不用擔心,你能去的地方,一定會帶給你預(yù)想不到的驚喜,你會遇見一些人,覺得相見恨晚,經(jīng)歷一些事,銘記終生,如果那年我們多對或者多錯兩道題,那么我們會不會在不同的地方,認識完全不同的人,做著完全不同的事,錯過我們現(xiàn)在所擁有的一切,高考的迷人之處不是得償所愿,而是陰差陽錯";
NSMutableAttributedString *aString = [[NSMutableAttributedString alloc]initWithString:text];
// 設(shè)置整段文本size
aString.yy_font = [UIFont systemFontOfSize:16];
// 設(shè)置整段文本行間距
aString.yy_lineSpacing = 10;
// 設(shè)置整段文本下劃線
//aString.yy_underlineStyle = NSUnderlineStyleSingle;
// 獲得range 并做相應(yīng)的設(shè)置
NSRange range1 = [text rangeOfString:@"相見恨晚"];
//[aString yy_setTextUnderline:[YYTextDecoration decorationWithStyle:YYTextLineStyleSingle] range:range];
[aString yy_setFont:[UIFont systemFontOfSize:22] range:range1];
//超鏈接的設(shè)置
[aString yy_setTextHighlightRange:range1 color:[UIColor colorWithHexString:@"56bcff"] backgroundColor:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
SXLog(@"您點擊了相見恨晚的超鏈接");
}];
//NSRange range2 = [text rangeOfString:@"高考的迷人之處不是得償所愿"];
NSRange range2 = NSMakeRange(text.length - 20, 13);
[aString yy_setFont:[UIFont systemFontOfSize:22] range:range2];
[aString yy_setTextHighlightRange:range2 color:[UIColor colorWithHexString:@"56bcff"] backgroundColor:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
SXLog(@"您點擊了得償所愿的超鏈接");
}];
/* 加入圖文混排 */
NSMutableAttributedString *attachment = nil;
// 嵌入 UIImage
UIImage *image = [UIImage imageNamed:@"addcenter"];
attachment = [NSMutableAttributedString yy_attachmentStringWithContent:image contentMode:UIViewContentModeCenter attachmentSize:image.size alignToFont:[UIFont systemFontOfSize:16] alignment:YYTextVerticalAlignmentCenter];
[aString appendAttributedString: attachment];
//在圖片后面再插入一個人label
NSMutableAttributedString *xinzengText = [[NSMutableAttributedString alloc] initWithString:@"我是用于測試新增文本的label"];
xinzengText.yy_font = [UIFont systemFontOfSize:16];
xinzengText.yy_color = [UIColor blueColor];
[aString appendAttributedString:xinzengText];
// 使用YYLabel進行渲染
YYLabel *FWBLabel = [[YYLabel alloc]init];
FWBLabel.backgroundColor = [UIColor redColor];
FWBLabel.attributedText = aString;
//FWBLabel.textAlignment = NSTextAlignmentCenter;//導致高亮排版有問題
//FWBLabel.numberOfLines = 0;
[scrollView addSubview:FWBLabel];
// 計算文本尺寸 進行布局
CGSize maxSize = CGSizeMake(ScreenWidth - 30, MAXFLOAT);
YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:maxSize text:aString];
FWBLabel.textLayout = layout;
CGFloat fwbHeight = layout.textBoundingSize.height;
FWBLabel.frame = CGRectMake(15, CGRectGetMaxY(self.customLabel3.frame)+20, ScreenWidth - 30, fwbHeight);
點擊YYText查看詳細介紹及使用!