這幾天公司項目要去寫一個像微博那樣的發(fā)布頁和首頁,這里面就涉及到了富文本,對比了很多的app的發(fā)布頁面焕毫,感覺微博做的真實了不得眯娱,在這里先簡單的介紹一下富文本,過幾天陸續(xù)把發(fā)布頁和首頁列表的代碼抽出來室埋,好好寫篇文章,詳細(xì)的介紹一些富文本
1.什么是富文本伊约?
實際上就是圖文混排词顾,就是過去都是普通的文字,黑色的碱妆,大小一致,但是不能滿足需求昔驱,最后通過富文本技術(shù)疹尾,給一段文字添加不同的屬性,如,大小纳本,顏色窍蓝,超鏈接,鏈接圖片繁成,下劃線吓笙,刪除線等等,大大豐富了我們使用文字的效果巾腕。ios中的富文本專門有一個類面睛,NSAttributedString
,當(dāng)然,還有一個可變的NSMutableAttributedString
類尊搬。普通的lable
叁鉴,button
,textView
等UI控件都有text
這個屬性顯示普通文本佛寿,如果要去顯示富文本幌墓,要使用他們的attributedText
這個屬性賦值,如果你使用了lable.text = attriText
,保證不會顯示
2.使用富文本寫個簡單的東西
2.1 只有文字的富文本
//添加123你好i am fine
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:@"123你好 i am fine"];
//設(shè)置123 redColor冀泻,
[attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:24] range:NSMakeRange(0, 3)];
[attr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 3)];
//設(shè)置“你好” 為綠色
[attr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(4, 2)];
[attr addAttribute:NSUnderlineColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(4, 2)];
[attr addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleDouble) range:NSMakeRange(4, 2)];
//設(shè)置 “i am fine”為高亮
[attr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(7, 9)];
self.label.attributedText = attr;
2.2 帶有圖片的富文本
//添加123你好i am fine
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:@"123你好 i am fine"];
//設(shè)置123 redColor常侣,
[attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:24] range:NSMakeRange(0, 3)];
[attr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 3)];
//設(shè)置“你好” 為綠色
[attr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(4, 2)];
[attr addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleDouble) range:NSMakeRange(4, 2)];
[attr addAttribute:NSUnderlineColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(4, 2)];
//設(shè)置 “i am fine”為高亮
[attr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(7, 9)];
//添加一張照片
NSTextAttachment *attach = [[NSTextAttachment alloc] init];
attach.image = [UIImage imageNamed:@"123.png"];
NSAttributedString *picAttr = [NSAttributedString attributedStringWithAttachment:attach];
[attr appendAttributedString:picAttr];
self.label.attributedText = attr;
2.3 修改富文本中的圖片大小
//通過這句代碼,修改照片的大小
//但是這個bounds和view的bounds不一樣弹渔,這里的x胳施,y是有用的,而view中的沒有用@谈健=砣椤!
attach.bounds = CGRectMake(0, 0, 10, 10);
3.用webView通過html加載富文本
NSString *hmtl = @"<font size='5'>我是好人</font>";
[self.webview loadHTMLString:hmtl baseURL:nil];
通過這樣的文本就能出來了,原來的代碼全丟了欧募,只能那這個糊上了压状,學(xué)學(xué)h5就可以寫了~
4.富文本的屬性都有哪些
常量
1> NSFontAttributeName
(字體)
該屬性所對應(yīng)的值是一個 UIFont 對象。該屬性用于改變一段文本的字體跟继。如果不指定該屬性种冬,則默認(rèn)為12-point Helvetica(Neue)
。
2> NSParagraphStyleAttributeName
(段落)
該屬性所對應(yīng)的值是一個 NSParagraphStyle
對象舔糖。該屬性在一段文本上應(yīng)用多個屬性娱两。如果不指定該屬性,則默認(rèn)為 NSParagraphStyle
的defaultParagraphStyle
方法返回的默認(rèn)段落屬性金吗。
3> NSForegroundColorAttributeName
(字體顏色)
該屬性所對應(yīng)的值是一個 UIColor
對象十兢。該屬性用于指定一段文本的字體顏色趣竣。如果不指定該屬性,則默認(rèn)為黑色旱物。
4> NSBackgroundColorAttributeName
(字體背景色)
該屬性所對應(yīng)的值是一個 UIColor 對象遥缕。該屬性用于指定一段文本的背景顏色。如果不指定該屬性宵呛,則默認(rèn)無背景色单匣。
5> NSLigatureAttributeName
(連字符)
該屬性所對應(yīng)的值是一個 NSNumber 對象(整數(shù))。連體字符是指某些連在一起的字符宝穗,它們采用單個的圖元符號户秤。0 表示沒有連體字符。1 表示使用默認(rèn)的連體字符讽营。2表示使用所有連體符號虎忌。默認(rèn)值為 1(注意,iOS 不支持值為 2)橱鹏。
6> NSKernAttributeName
(字間距)
該屬性所對應(yīng)的值是一個 NSNumber 對象(整數(shù))膜蠢。字母緊排指定了用于調(diào)整字距的像素點數(shù)。字母緊排的效果依賴于字體莉兰。值為 0 表示不使用字母緊排挑围。默認(rèn)值為0。
7> NSStrikethroughStyleAttributeName
(刪除線)
該屬性所對應(yīng)的值是一個 NSNumber 對象(整數(shù))糖荒。該值指定是否在文字上加上刪除線杉辙,該值參考“Underline Style Attributes”
。默認(rèn)值是NSUnderlineStyleNone
捶朵。
8> NSUnderlineStyleAttributeName
(下劃線)
該屬性所對應(yīng)的值是一個 NSNumber
對象(整數(shù))蜘矢。該值指定是否在文字上加上下劃線,該值參考“Underline Style Attributes”
综看。默認(rèn)值是NSUnderlineStyleNone
品腹。
9> NSStrokeColorAttributeName
(邊線顏色)
該屬性所對應(yīng)的值是一個 UIColor
對象。如果該屬性不指定(默認(rèn))红碑,則等同于 NSForegroundColorAttributeName
舞吭。否則,指定為刪除線或下劃線顏色析珊。更多細(xì)節(jié)見“Drawing attributedstrings that are both filled and stroked”
羡鸥。
10> NSStrokeWidthAttributeName
(邊線寬度)
該屬性所對應(yīng)的值是一個 NSNumber 對象(小數(shù))。該值改變描邊寬度(相對于字體size 的百分比)忠寻。默認(rèn)為 0惧浴,即不改變。正數(shù)只改變描邊寬度奕剃。負(fù)數(shù)同時改變文字的描邊和填充寬度衷旅。例如哑姚,對于常見的空心字,這個值通常為3.0芜茵。
11> NSShadowAttributeName
(陰影)
該屬性所對應(yīng)的值是一個 NSShadow 對象。默認(rèn)為 nil倡蝙。
12> NSVerticalGlyphFormAttributeName
(橫豎排版)
該屬性所對應(yīng)的值是一個 NSNumber 對象(整數(shù))九串。0 表示橫排文本。1 表示豎排文本寺鸥。在 iOS 中猪钮,總是使用橫排文本,0 以外的值都未定義胆建。