版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2017.06.07 |
前言
YYText是一個專門處理文字的框架,有了它處理文字變得非常方便,這一篇我繼續介紹YYText的使用方法,希望對大家能有所幫助。大家如感興趣還可以參考:
1.YYText使用篇(一)
2.YYText使用篇(二)
3.YYText使用篇(三)
一、YYText文本行位置調整
該方法的作用是:由于中文、英文、Emoji 等字體高度不一致,或者富文本中出現了不同字號的字體;可能會造成每行文字的高度不一致。這里可以添加一個修改器來實現固定行高,或者自定義文本行位置。
實現的方法:
-
1. 創建一個文本行位置修改類,實現
YYTextLinePositionModifier
協議。 - 2. 設置到 Label 或 TextView。
下面看代碼實現
UILabel *label = [[UILabel alloc] init];
label.text = @"舊時月色,算幾番照我,梅邊吹笛?喚起玉人,不管清寒與攀摘.何遜而今漸老,都忘卻,春風詞筆.women我們!!!!!!!!!!!!!!,梅邊吹笛?喚起玉人,AAAQIWBNMwdpnumberOfLinesnumberOfLinesbiaoqian☆★♀♂ O(∩_∩)O~ (=@__@=) (*^__^*) %>_<% └(^o^)┘; ^ˇ^≡ ‘(*>﹏<*)′ ~(@^_^@)~ (*+﹏+*)~ (^_^)∠※.這都是什么字什么字什么字";
label.numberOfLines = 0;
label.frame = CGRectMake(0.0, 64.0, self.view.bounds.size.width, 500.0);
label.textColor = [UIColor blueColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
[self.view addSubview:label];
下面看輸出結果
從上面可以看見,文字,英文字母還有表情符號等混合排在一起,可以看見由于混排,它們之間每一行的間距是不用的。要知道為什么是這個結果,首先要知道字形描述集,首先要弄懂下邊的圖。
可以看到baseline并不是所有字體的最下端,它只是一條假想的參照線,以此為基礎進行字形的渲染。而從YYLabel所產生的行間距不同來看,應該是利用了上一行字體的下緣線和下一行字體的上緣線做處理,由于漢字和英文、表情之類的上下緣線存在差異,就造成了行間距的不同。那么我們可以忽略上下緣線,以baseline為基準,設置兩行文本之間的間距,這樣看起來就工整多了。
??這里,YYTextLinePositionSimpleModifier對象的fixedLineHeight屬性所修正的是字體渲染的baseline。
下面我們接著看代碼,進行修改
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString: @"舊時月色,算幾番照我,梅邊吹笛?喚起玉人,不管清寒與攀摘.何遜而今漸老,都忘卻,春風詞筆.women我們!!!!!!!!!!!!!!,梅邊吹笛?喚起玉人,AAAQIWBNMwdpnumberOfLinesnumberOfLinesbiaoqian☆★♀♂ O(∩_∩)O~ (=@__@=) (*^__^*) %>_<% └(^o^)┘; ^ˇ^≡ ‘(*>﹏<*)′ ~(@^_^@)~ (*+﹏+*)~ (^_^)∠※.這都是什么字什么字什么字"];
attrStr.yy_color = [UIColor blueColor];
attrStr.yy_font = [UIFont boldSystemFontOfSize:20.0];
YYTextLinePositionSimpleModifier *modifier = [[YYTextLinePositionSimpleModifier alloc] init];
modifier.fixedLineHeight = 20;
YYLabel *label = [[YYLabel alloc] init];
label.numberOfLines = 0;
label.frame = CGRectMake(0.0, 64.0, self.view.bounds.size.width, 500.0);
label.linePositionModifier = modifier;
label.attributedText = attrStr;
[self.view addSubview:label];
下面我們看顯示結果
由上可知,采用YYText可以有效的改變行間距,使行間距具有相同的高度。
二、YYText異步排版和渲染
??我們一般是在主線程渲染UI,在子線程做網絡請求等耗時任務,下面我們看代碼。
dispatch_async(dispatch_get_global_queue(0, 0), ^{
UILabel *label = [[UILabel alloc] init];
label.text = @"舊時月色,算幾番照我,梅邊吹笛?喚起玉人,不管清寒與攀摘.何遜而今漸老,都忘卻,春風詞筆.women我們!!!!!!!!!!!!!!,梅邊吹笛?喚起玉人,AAAQIWBNMwdpnumberOfLinesnumberOfLinesbiaoqian☆★♀♂ O(∩_∩)O~ (=@__@=) (*^__^*) %>_<% └(^o^)┘; ^ˇ^≡ ‘(*>﹏<*)′ ~(@^_^@)~ (*+﹏+*)~ (^_^)∠※.這都是什么字什么字什么字";
label.numberOfLines = 0;
label.frame = CGRectMake(0.0, 64.0, self.view.bounds.size.width, 500.0);
label.textColor = [UIColor blueColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
[self.view addSubview:label];
NSLog(@"%@",[NSThread currentThread]);
});
我們看輸出和效果圖。
2017-06-07 23:33:41.098 YYTextDemo[1059:18833] <NSThread: 0x6080002632c0>{number = 3, name = (null)}
從上可以看出,異步也可以渲染到屏幕上,但是我們一般都是主線程做視圖渲染,YYText提供了異步渲染的功能,下面我們看代碼。
// 如果你在顯示字符串時有性能問題,可以這樣開啟異步模式:
YYLabel *label = [[YYLabel alloc] init];
label.displaysAsynchronously = YES;
label.ignoreCommonProperties = YES; //忽略除了 textLayout 之外的其他屬性
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 創建屬性字符串
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"舊時月色,算幾番照我,梅邊吹笛?喚起玉人,不管清寒與攀摘.何遜而今漸老"];
text.yy_font = [UIFont systemFontOfSize:18];
text.yy_color = [UIColor blueColor];
[text yy_setColor:[UIColor redColor] range:NSMakeRange(0, 20)];
// 創建文本容器
YYTextContainer *container = [[YYTextContainer alloc] init];
container.size = CGSizeMake(400, CGFLOAT_MAX);
container.maximumNumberOfRows = 0;
// 生成排版結果
YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:text];
dispatch_async(dispatch_get_main_queue(), ^{
label.attributedText = text;
label.textLayout = layout;
CGRect rect = layout.textBoundingRect;
rect.size = layout.textBoundingSize;
rect.origin = CGPointMake(0.0, 100);
label.frame = rect;
[self.view addSubview:label];
});
});
下面看效果圖
后記
未完,待續哦~~~