超簡單的富文本

寫項目不免遇到使用富文本處理的時候,好比有的時候用戶點擊查看軟件的某個功能說明,須要着重標註的部分要突出顯示,有時候做爲提示用語還要加上一個圖片起到引導的做用提示用戶該作什麼或者完善什麼等等,這個時候就要用到富文原本處理了。固然,也可使用coretext來處理也能夠,簡單的需求通常使用NSAttributedString徹底能夠應付,使用coretext通常處理比較複雜的場景,還有使用NSAttributedString加載圖片的時候是不支持gif圖片的顯示,只能顯示靜態圖片,這點要認識到,因此若是處理相似鬥魚的彈幕那種仍是coretext會更完美。git

效果:github


本工具使用的快速,安全,方便安全

1,使用鏈式語法來處理富文本的樣式添加,簡單快速易懂app

2,兩種方式處理富文本顯示工具

使用時,只需加入XDYStringComponent.h和XDYStringComponent.m這倆文件便可學習

上代碼:字體

第一種方式:優化

-(void)initViews{
self.view.backgroundColor = [UIColor whiteColor];
UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(20, 100, [UIScreen mainScreen].bounds.size.width-40, 300)];
bgView.layer.borderColor = [UIColor blackColor].CGColor;
bgView.layer.borderWidth = 1;
[self.view addSubview:bgView];

UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width-40, 100)];
label1.textAlignment = NSTextAlignmentCenter;
label1.numberOfLines = 0;
label1.lineBreakMode = NSLineBreakByWordWrapping;
[bgView addSubview:label1];

//第一種使用方式
XDYStringComponent *com1 = [[XDYStringComponent alloc] init];
com1.COMText(@"第一段文字紅色,").COMFont([UIFont systemFontOfSize:14]).COMColor([UIColor redColor]).COMLineSpace(@(10)).COMSeperateSpace(@(4));

XDYStringComponent *com2 = [[XDYStringComponent alloc] init];
com2.COMText(@"第二段文字是黑色,").COMFont([UIFont systemFontOfSize:18]).COMColor([UIColor blackColor]).COMLineSpace(@(10)).COMSeperateSpace(@(4));

XDYStringComponent *com3 = [[XDYStringComponent alloc] init];
com3.COMText(@"第三段文字是藍色,").COMFont([UIFont systemFontOfSize:12]).COMColor([UIColor blueColor]).COMLineSpace(@(10)).COMSeperateSpace(@(4));

XDYStringComponent *com4 = [[XDYStringComponent alloc] init];
com4.COMText(@"第四段文字是黃色加圖片").COMFont([UIFont systemFontOfSize:12]).COMAttachImage([UIImage imageNamed:@"t4_chexian_title_baoxian"]).COMLineSpace(@(10)).COMSeperateSpace(@(4));

XDYStringComponent *com = [[XDYStringComponent alloc] init];
[com appendingStringWithString:[com1 appendingStringWithString:[com2 appendingStringWithString:[com3 appendingStringWithString:com4]]]];
//3連上4做爲一個段落 2連上3和4的結合做爲一個段落 1連上2和3的結合做爲一個新的段落。最後賦值給label
label1.attributedText = com.attribuString;

UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, [UIScreen mainScreen].bounds.size.width-40, 100)];
label2.textAlignment = NSTextAlignmentCenter;
label2.numberOfLines = 0;
label2.lineBreakMode = NSLineBreakByWordWrapping;
[bgView addSubview:label2];

XDYStringComponent *newCom = [XDYStringComponent addComponentWithStyleArray:@[
@{@"font":[UIFont systemFontOfSize:12],@"text":@"第一段文字紅色,",@"color":[UIColor redColor]},
@{@"font":[UIFont systemFontOfSize:16],@"text":@"第二段文字是藍色,",@"color":[UIColor blueColor]},
@{@"font":[UIFont systemFontOfSize:18],@"text":@"第二段文字是默認的白色,"},
@{@"font":[UIFont systemFontOfSize:14],@"text":@"第四段文字是黃色加圖片",@"attach":[UIImage imageNamed:@"t4_share_icon_weixin"],@"color":[UIColor cyanColor]}
]];
label2.attributedText = newCom.attribuString;
}


能夠看到,當須要定義富文本的樣式時,直接鏈式語法拼接便可,自由設置字體大小,顏色,陰影,行間距,字間距,等等label可使用的屬性。cdn

固然這種方式使用的時候有侷限性,就是當我有大量文字的時候要加各類不一樣的樣式顏色,就要實例化多個stringComponent對象。而後一個個拼接,稍顯麻煩,對此我寫了個方法作了個優化,就是第二種方式對象

第二種方式:

UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, [UIScreen mainScreen].bounds.size.width-40, 100)];
label2.textAlignment = NSTextAlignmentCenter;
label2.numberOfLines = 0;
label2.lineBreakMode = NSLineBreakByWordWrapping;
[bgView addSubview:label2];

XDYStringComponent *newCom = [XDYStringComponent addComponentWithStyleArray:@[
@{@"font":[UIFont systemFontOfSize:12],@"text":@"第一段文字紅色,",@"color":[UIColor redColor]},
@{@"font":[UIFont systemFontOfSize:16],@"text":@"第二段文字是藍色,",@"color":[UIColor blueColor]},
@{@"font":[UIFont systemFontOfSize:18],@"text":@"第二段文字是默認的白色,"},
@{@"font":[UIFont systemFontOfSize:14],@"text":@"第四段文字是黃色加圖片",@"attach":[UIImage imageNamed:@"t4_share_icon_weixin"],@"color":[UIColor cyanColor]}
]];
label2.attributedText = newCom.attribuString;


這裏封裝了一個方法,方法裏將顯示的樣式定義成固定格式的字典,鍵名就是特徵值,font,color,等等,具體能夠進入實現文件內部方法裏查看,總共沒幾個鍵名,須要完善能夠本身添加就好了

附上連接:github.com/dota4app/St…

有什麼不足的地方歡迎你們指正,我仍是個新手,須要學習的地方不少,以後會不斷完善

相關文章
相關標籤/搜索