iOS 富文本 效果整理 基于NSAttributedString

利用 UILabel 的 attributedText 屬性可設(shè)置不同的文本樣式

  • 首先初始化一個(gè)NSAttributedString
NSMutableAttributedString *abStr = [[NSMutableAttributedString alloc] initWithString:@"你看我是富文本"];

這里初始加給label.attributedText,看起來是平平無奇的樣子
這里要先設(shè)置label的初始textColorfont等,展示基本樣式

image

  • 然后選擇一種樣式給label變身吧

1. 設(shè)置字體和大小

NSFontAttributeName

image

//設(shè)置前三個(gè)字 [UIFont systemFontOfSize:15]
[abStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, 3)];

2. 設(shè)置間距

NSParagraphStyleAttributeName

image

// 這里要用NSMutableParagraphStyle來設(shè)置各種屬性
// 下面只是簡單常用的處理行間距
// 還可以設(shè)置首行縮進(jìn),行高漫谷,對齊方式旷坦,段落赎瑰,連字屬性等
NSMutableParagraphStyle *pgpStyle = [[NSMutableParagraphStyle alloc] init];
pgpStyle.lineSpacing = 30; //行間距,注意:實(shí)際視覺行距要計(jì)算到實(shí)際行高
// 行間距是多少倍
// pgpStyle.lineHeightMultiple = 1.5f;
// 首行縮進(jìn)  
// pgpStyle.firstLineHeadIndent = 30.0f;    
// 對齊方式 
// pgpStyle.alignment = NSTextAlignmentLeft; 
// 內(nèi)容超出寬度時(shí),內(nèi)容有...省略的位置 ( "...abc" ,"abc..." ,"ab...c")   
// pgpStyle.lineBreakMode = NSLineBreakByTruncatingTail; 
// 整體縮進(jìn)
// pgpStyle.headIndent = 30;//左邊距    
// pgpStyle.tailIndent = 20;//右邊距  
// 行高  
// 針對不同的字型與字號,可以透過指定最大與最小行距來避免過高或過窄的狀況發(fā)生
// pgpStyle.minimumLineHeight = 10;//最低行高    
// pgpStyle.maximumLineHeight = 20;//最大行高    
// 段落 
// pgpStyle.paragraphSpacing = 15; //段落間距  
// pgpStyle.paragraphSpacingBefore = 30;//段首行空白空間 
// pgpStyle.paragraphSpacing = 30; //段落后面的間距  
[abStr addAttribute:NSParagraphStyleAttributeName
              value:pgpStyle
              range:NSMakeRange(0, 5)];

3. 設(shè)置文本顏色

NSForegroundColorAttributeName

image

[abStr addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 3)];

4. 設(shè)置文字背景色

NSBackgroundColorAttributeName

image

[abStr addAttribute:NSBackgroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 3)];

5. 設(shè)置連字

NSLigatureAttributeName

image

說明:
1.PingFangSC_Bold 和 PingFangSC_Regular 一般不會有連字問題
2.PingFangSC-Semibold等字體或者三方字體就有默認(rèn)連字現(xiàn)象
3.也不是所有的字都會連,一般出現(xiàn)在fi和fl這種字符才會連,具體"連字"的概念問百度or谷歌

NSMutableAttributedString *abStr = [[NSMutableAttributedString alloc]initWithString:@"fiflfi"];
//0表示不連 1表示連 實(shí)際中PingFangSC-Semibold等是默認(rèn)連的
[abStr addAttribute:NSLigatureAttributeName value:@(1) range:NSMakeRange(0, abStr.length)];

6. 設(shè)置文字間距

NSKernAttributeName

image

[abStr addAttribute:NSKernAttributeName value:@(10) range:NSMakeRange(0, 3)];

7. 設(shè)置刪除線

NSStrikethroughColorAttributeName
NSStrikethroughStyleAttributeName

image

說明:
1.可設(shè)置單.雙刪除線
2.value賦值 [1, 7]是單線, [9, 15]是雙線,區(qū)間內(nèi)數(shù)值越大,線越粗,注意超出區(qū)間內(nèi)的值,刪除線就會失效

[abStr addAttribute:NSStrikethroughStyleAttributeName value:@(5) range:NSMakeRange(0, 3)];
[abStr addAttribute:NSStrikethroughStyleAttributeName value:@(13) range:NSMakeRange(3, 4)];
    
[abStr addAttribute:NSStrikethroughColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 3)];
[abStr addAttribute:NSStrikethroughColorAttributeName value:[UIColor greenColor] range:NSMakeRange(3, 4)];

8. 設(shè)置下劃線

NSUnderlineStyleAttributeName
NSUnderlineColorAttributeName

image

一般常用:value賦值
NSUnderlineStyleSingle //單根細(xì)線
NSUnderlineStyleThick //單根粗線
NSUnderlineStyleDouble //雙根細(xì)線

//樣式
[abStr addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, 3)];
//顏色
[abStr addAttribute:NSUnderlineColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 3)];
[abStr addAttribute:NSUnderlineColorAttributeName value:[UIColor greenColor] range:NSMakeRange(4, 3)];

9. 文字描邊

NSStrokeWidthAttributeName
NSStrokeColorAttributeName

image

NSStrokeWidthAttributeName 這個(gè)設(shè)置描邊字符的寬度,(-∞, 0)區(qū)間向內(nèi),(0, +∞)區(qū)間向外, 注意value設(shè)置過大就糊了喲
NSStrokeColorAttributeName 這個(gè)設(shè)置描邊字符的顏色

[abStr addAttribute:NSStrokeWidthAttributeName value:@(2) range:NSMakeRange(0, 3)];
[abStr addAttribute:NSStrokeColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 3)];
    
[abStr addAttribute:NSStrokeWidthAttributeName value:@(-2) range:NSMakeRange(3, 3)];
[abStr addAttribute:NSStrokeColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(3, 3)];

10. 設(shè)置文字陰影

NSShadowAttributeName

image

NSShadow *shadow = [[NSShadow alloc]init];
shadow.shadowOffset = CGSizeMake(5, 5);
shadow.shadowColor = [UIColor orangeColor];
// shadow.shadowBlurRadius = 5; 陰影模糊度
//貌似range設(shè)置無效了,都是這個(gè)文本
[abStr addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(0, abStr.length)];

11. 設(shè)置文本特殊效果(用處少)

NSTextEffectAttributeName

只有一直效果類型為NSTextEffectLetterpressStyle //凸版樣式

12. 設(shè)置文件附件(圖文混排)

NSAttachmentAttributeName

image

NSTextAttachment *tathment = [[NSTextAttachment alloc]init];
tathment.image = [UIImage imageNamed:@"absImg.png"];
tathment.bounds = CGRectMake(0, 0, 35, 35);
NSAttributedString *imgAbs = [NSAttributedString attributedStringWithAttachment:tathment];
    
[abStr insertAttributedString:imgAbs atIndex:3];

13. 設(shè)置鏈接(只有UITextView可用)

NSLinkAttributeName

value 賦值類型是 NSURL or NSString

image
UITextView *tv = [[UITextView alloc]initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 200)];
tv.font = [UIFont systemFontOfSize:25];
tv.textAlignment = NSTextAlignmentCenter;
tv.editable = NO;
[self.view addSubview:tv];
    
NSMutableAttributedString *abStr = [[NSMutableAttributedString alloc]initWithString:@"你看我是富文本"];
[abStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:25] range:NSMakeRange(0, abStr.length)];
NSMutableParagraphStyle *pgpStyle = [[NSMutableParagraphStyle alloc] init];
pgpStyle.alignment = NSTextAlignmentCenter;
[abStr addAttribute:NSParagraphStyleAttributeName value:pgpStyle range:NSMakeRange(0, abStr.length)];
    
NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
[abStr addAttribute:NSLinkAttributeName value:url range:NSMakeRange(abStr.length-3, 3)];
tv.attributedText = abStr;

14. 文字上下偏移

NSBaselineOffsetAttributeName

image

vlaue 賦值 正負(fù)對應(yīng)上下偏移

[abStr addAttribute:NSBaselineOffsetAttributeName value:@(6) range:NSMakeRange(2, 1)];
[abStr addAttribute:NSBaselineOffsetAttributeName value:@(-6) range:NSMakeRange(4, 1)];

15. 文字斜體

NSObliquenessAttributeName

image

vlaue 賦值 正負(fù)對應(yīng)左右斜

[abStr addAttribute:NSObliquenessAttributeName value:@(-0.5) range:NSMakeRange(0, 3)];
[abStr addAttribute:NSObliquenessAttributeName value:@(0.5) range:NSMakeRange(4, 3)];

15. 文字拉伸

NSExpansionAttributeName // 橫向拉伸

image

vlaue 賦值 正負(fù)對應(yīng)變胖瘦

[abStr addAttribute:NSExpansionAttributeName value:@(-0.5) range:NSMakeRange(0, 3)];
[abStr addAttribute:NSExpansionAttributeName value:@(0.5) range:NSMakeRange(4, 3)];

16. 文字書寫方向(一般用于自右向左)

NSWritingDirectionAttributeName

image

[abStr addAttribute:NSWritingDirectionAttributeName value:@[@(NSWritingDirectionRightToLeft|NSWritingDirectionOverride)] range:NSMakeRange(0, abStr.length)];

17. 文字排版(iOS上無效果)

NSVerticalGlyphFormAttributeName

0為水平排版的字,1為垂直排版的字显晶。iOS上效果一直為1

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子雇毫,更是在濱河造成了極大的恐慌,老刑警劉巖踩蔚,帶你破解...
    沈念sama閱讀 221,273評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件棚放,死亡現(xiàn)場離奇詭異,居然都是意外死亡馅闽,警方通過查閱死者的電腦和手機(jī)飘蚯,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評論 3 398
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來福也,“玉大人局骤,你說我怎么就攤上這事”┐眨” “怎么了峦甩?”我有些...
    開封第一講書人閱讀 167,709評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我凯傲,道長犬辰,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,520評論 1 296
  • 正文 為了忘掉前任冰单,我火速辦了婚禮幌缝,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘诫欠。我一直安慰自己涵卵,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,515評論 6 397
  • 文/花漫 我一把揭開白布荒叼。 她就那樣靜靜地躺著轿偎,像睡著了一般。 火紅的嫁衣襯著肌膚如雪被廓。 梳的紋絲不亂的頭發(fā)上贴硫,一...
    開封第一講書人閱讀 52,158評論 1 308
  • 那天,我揣著相機(jī)與錄音伊者,去河邊找鬼英遭。 笑死,一個(gè)胖子當(dāng)著我的面吹牛亦渗,可吹牛的內(nèi)容都是我干的挖诸。 我是一名探鬼主播,決...
    沈念sama閱讀 40,755評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼法精,長吁一口氣:“原來是場噩夢啊……” “哼多律!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起搂蜓,我...
    開封第一講書人閱讀 39,660評論 0 276
  • 序言:老撾萬榮一對情侶失蹤狼荞,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后帮碰,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體相味,經(jīng)...
    沈念sama閱讀 46,203評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,287評論 3 340
  • 正文 我和宋清朗相戀三年殉挽,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了丰涉。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,427評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡斯碌,死狀恐怖一死,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情傻唾,我是刑警寧澤投慈,帶...
    沈念sama閱讀 36,122評論 5 349
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響伪煤,放射性物質(zhì)發(fā)生泄漏加袋。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,801評論 3 333
  • 文/蒙蒙 一带族、第九天 我趴在偏房一處隱蔽的房頂上張望锁荔。 院中可真熱鬧蟀给,春花似錦蝙砌、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至前普,卻和暖如春肚邢,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背拭卿。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評論 1 272
  • 我被黑心中介騙來泰國打工骡湖, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人峻厚。 一個(gè)月前我還...
    沈念sama閱讀 48,808評論 3 376
  • 正文 我出身青樓响蕴,卻偏偏與公主長得像,于是被迫代替她去往敵國和親惠桃。 傳聞我的和親對象是個(gè)殘疾皇子浦夷,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,440評論 2 359