iOS_NSAttributedString 的21種屬性詳細(xì)介紹(圖文混排)

iOS_NSAttributedString 的21種屬性詳細(xì)介紹(圖文混排)

* API: Character Attributes ,NSAttributedString共有21個屬性*

*1.NSFontAttributeName->設(shè)置字體屬性,默認(rèn)值:字體:Helvetica(Neue) 字號:12

*2.NSParagraphStyleAttributeName->設(shè)置文本段落排版格式甜攀,取值為NSParagraphStyle對象(詳情見下面的API說明)?

*3.NSForegroundColorAttributeName->設(shè)置字體顏色,取值為UIColor對象,默認(rèn)值為黑色?

*4.NSBackgroundColorAttributeName->設(shè)置字體所在區(qū)域背景顏色厕吉,取值為UIColor對象聪廉,默認(rèn)值為nil, 透明色?

*5.NSLigatureAttributeName->設(shè)置連體屬性乖订,取值為NSNumber對象(整數(shù)),0表示沒有連體字符逗鸣,1表示使用默認(rèn)的連體字符

*6.NSKernAttributeName->設(shè)置字符間距,取值為NSNumber對象(整數(shù))绰精,正值間距加寬撒璧,負(fù)值間距變窄

?*7.NSStrikethroughStyleAttributeName->設(shè)置刪除線,取值為NSNumber對象(整數(shù))?

*8.NSStrikethroughColorAttributeName->設(shè)置刪除線顏色笨使,取值為UIColor對象卿樱,默認(rèn)值為黑色

?*9.NSUnderlineStyleAttributeName->設(shè)置下劃線巢寡,取值為NSNumber對象(整數(shù))必孤,枚舉常量NSUnderlineStyle中的值,與刪除線類似?

*10.NSUnderlineColorAttributeName->設(shè)置下劃線顏色茴扁,取值為UIColor對象最爬,默認(rèn)值為黑色?

*11.NSStrokeWidthAttributeName->設(shè)置筆畫寬度(粗細(xì))涉馁,取值為NSNumber對象(整數(shù)),負(fù)值填充效果爱致,正值中空效果?

*12.NSStrokeColorAttributeName->填充部分顏色烤送,不是字體顏色,取值為UIColor對象?

*13.NSShadowAttributeName->設(shè)置陰影屬性糠悯,取值為NSShadow對象?

*14.NSTextEffectAttributeName->設(shè)置文本特殊效果帮坚,取值為NSString對象妻往,目前只有圖版印刷效果可用?

*15.NSBaselineOffsetAttributeName->設(shè)置基線偏移值,取值為NSNumber(float),正值上偏试和,負(fù)值下偏?

*16.NSObliquenessAttributeName->設(shè)置字形傾斜度讯泣,取值為NSNumber(float),正值右傾,負(fù)值左傾?

*17.NSExpansionAttributeName->設(shè)置文本橫向拉伸屬性阅悍,取值為NSNumber(float),正值橫向拉伸文本好渠,負(fù)值橫向壓縮文本?

*18.NSWritingDirectionAttributeName->設(shè)置文字書寫方向,從左向右書寫或者從右向左書寫?

*19.NSVerticalGlyphFormAttributeName->設(shè)置文字排版方向节视,取值為NSNumber對象(整數(shù))拳锚,0表示橫排文本,1表示豎排文本

*20.NSLinkAttributeName->設(shè)置鏈接屬性寻行,點(diǎn)擊后調(diào)用瀏覽器打開指定URL地址?

*21.NSAttachmentAttributeName->設(shè)置文本附件,取值為NSTextAttachment對象,常用于文字圖片混排

- (void)creatTitleLabel {self.titleLabel = [[UILabelalloc] initWithFrame:CGRectMake(20,50,320,400)];self.titleLabel.numberOfLines =0;self.titleLabel.layer.borderColor = [UIColorgrayColor].CGColor;self.titleLabel.layer.borderWidth =0.5;self.titleLabel.textAlignment =NSTextAlignmentLeft;? ? [self.view addSubview:self.titleLabel];NSString*string =@"An NSAttributedString object manages character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string. An association of characters and their attributes is called an attributed string. ";/* 這句話就是對這個類的一個最簡明扼要的概括霍掺。NSAttributedString管理一個字符串,以及與該字符串中的單個字符或某些范圍的字符串相關(guān)的屬性拌蜘。它有一個子類NSMutableAttributedString

? ? * 具體實(shí)現(xiàn)時杆烁,NSAttributedString維護(hù)了一個NSString,用來保存最原始的字符串简卧,另有一個NSDictionary用來保存各個子串/字符的屬性兔魂。

? ? */

titleLabel.png

#pragma mark - NSMutableAttributedString 創(chuàng)建/* 三種初始化方法,NSMutableAttributedString沒有初始化方法,使用父類初始化方法, 使用initWithString:, initWithString:attributes:, 或者 initWithAttributedString: */NSAttributedString*attStri = [[NSAttributedStringalloc] initWithString:string attributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:30]}];NSMutableAttributedString*mAttStri = [[NSMutableAttributedStringalloc] initWithString:string];#pragma mark ** 1. NSFontAttributeName 設(shè)置字體屬性/* 字體大小 及 字體類型 */NSRangefont_range = [string rangeOfString:@"An"];? ? [mAttStri addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:30] range:font_range];? ? [mAttStri addAttribute:NSFontAttributeNamevalue:[UIFontfontWithName:@"Courier-BoldOblique"size:17.0] range:NSMakeRange(10,10)];

字體大小 及 字體類型 .png

#pragma mark ** 2. NSParagraphStyleAttributeName 設(shè)置文本段落排版格式NSMutableParagraphStyle*style = [[NSMutableParagraphStylealloc] init];? ? style.firstLineHeadIndent =20;? ? style.lineSpacing =10;? ? ? ? [mAttStri addAttribute:NSParagraphStyleAttributeNamevalue:style range:NSMakeRange(0, mAttStri.length /2)];

設(shè)置文本段落排版格式.png

#pragma mark ** 3. NSForegroundColorAttributeName 設(shè)置字體顏色/* 值為UIColor,字體顏色贞滨,默認(rèn)為黑色. */[mAttStri addAttribute:NSForegroundColorAttributeNamevalue:[UIColorredColor] range:NSMakeRange(0, mAttStri.length)];

設(shè)置字體顏色.png

#pragma mark ** 4. NSBackgroundColorAttributeName 設(shè)置字體所在區(qū)域背景顏色/* 值為UIColor入热,字體背景色,默認(rèn)透明. */[mAttStri addAttribute:NSBackgroundColorAttributeNamevalue:[UIColorgrayColor] range:NSMakeRange(0,20)];

設(shè)置字體所在區(qū)域背景顏色.png

#pragma mark ** 5. NSLigatureAttributeName 設(shè)置連體屬性/* 取值為NSNumber 對象(整數(shù)). 0 表示沒有連體字符, 1 表示使用默認(rèn)的連體字符. 一般中文用不到晓铆,在英文中可能出現(xiàn)相鄰字母連筆的情況 */[mAttStri addAttribute:NSLigatureAttributeNamevalue:@0range:NSMakeRange(0, mAttStri.length)];

#pragma mark ** 6. NSKernAttributeName 設(shè)置字符間距/* 值為浮點(diǎn)數(shù)NSNumber勺良,字距屬性,默認(rèn)值為0骄噪。*/[mAttStri addAttribute:NSKernAttributeNamevalue:@3range:NSMakeRange(0, mAttStri.length)];

設(shè)置字符間距.png

#pragma mark ** 7. NSStrikethroughStyleAttributeName 設(shè)置刪除線/* 值為整型NSNumber尚困,可取值為(取值大小為刪除線的寬度)

? ? ? ? enum {


? ? ? ? NSUnderlineStyleNone = 0×00,


? ? ? ? NSUnderlineStyleSingle = 0×01,


? ? ? ? }; 設(shè)置刪除線。

? ? */[mAttStri addAttribute:NSStrikethroughStyleAttributeNamevalue:@3range:NSMakeRange(3,7)];

設(shè)置刪除線.png

#pragma mark ** 8. NSStrikethroughColorAttributeName 設(shè)置刪除線顏色/* 這個屬性的值是一個UIColor對象. */[mAttStri addAttribute:NSStrikethroughColorAttributeNamevalue:[UIColorblueColor] range:NSMakeRange(3,3)];

設(shè)置刪除線顏色.png

#pragma mark ** 9. NSUnderlineStyleAttributeName 設(shè)置下劃線/* 取值為 NSNumber 對象(整數(shù))链蕊,枚舉常量 NSUnderlineStyle中的值事甜,與刪除線類似 */[mAttStri addAttribute:NSUnderlineStyleAttributeNamevalue:@2range:NSMakeRange(6,5)];

設(shè)置下劃線.png

#pragma mark ** 10. NSUnderlineColorAttributeName 設(shè)置下劃線顏色/* 這個屬性的值是一個UIColor對象.默認(rèn)值為nil. */[mAttStri addAttribute:NSUnderlineColorAttributeNamevalue:[UIColorblackColor] range:NSMakeRange(6,5)];

設(shè)置下劃線顏色.png

#pragma mark ** 11. NSStrokeWidthAttributeName 設(shè)置筆畫寬度(粗細(xì))/* 值為浮點(diǎn)數(shù)NSNumber。設(shè)置筆畫的粗細(xì)滔韵。負(fù)值填充效果逻谦,正值中空效果. */[mAttStri addAttribute:NSStrokeWidthAttributeNamevalue:@10range:NSMakeRange(50,30)];

設(shè)置筆畫寬度(粗細(xì)).png

#pragma mark ** 12. NSStrokeColorAttributeName 填充部分顏色,/* 不是字體顏色陪蜻,取值為 UIColor 對象 默認(rèn)值為nil邦马,設(shè)置的屬性同F(xiàn)oregroundColor。*/[mAttStri addAttribute:NSStrokeColorAttributeNamevalue:[UIColororangeColor] range:NSMakeRange(50,20)];

填充部分顏色.png

#pragma mark ** 13. NSShadowAttributeName 設(shè)置陰影屬性/* 值為NSShadow,設(shè)置筆畫的陰影滋将,默認(rèn)值為nil邻悬。*/NSShadow*shadow = [[NSShadowalloc]init];? ? shadow.shadowOffset =CGSizeMake(10,10);? ? shadow.shadowColor = [UIColorgreenColor];? ? [mAttStri addAttribute:NSShadowAttributeNamevalue:shadow range:NSMakeRange(20,10)];

設(shè)置陰影屬性.png

#pragma mark ** 14. NSTextEffectAttributeName 設(shè)置文本特殊效果/* 這個屬性的值是一個NSString對象。使用此屬性指定的文字效果随闽,如NSTextEffectLetterpressStyle父丰。此屬性的默認(rèn)值為nil,表示沒有文本效應(yīng)掘宪。*/[mAttStri addAttribute:NSTextEffectAttributeNamevalue:NSTextEffectLetterpressStylerange:NSMakeRange(80,10)];

設(shè)置文本特殊效果.png

#pragma mark ** 15. NSBaselineOffsetAttributeName 設(shè)置基線偏移值/* 此屬性的值是包含一個浮點(diǎn)值的NSNumber對象,表示的字符從基線偏移的NSNumber對象蛾扇,默認(rèn)值是0。正值上偏魏滚,負(fù)值下偏 */[mAttStri addAttribute:NSBaselineOffsetAttributeNamevalue:@5range:NSMakeRange(112,10)];

設(shè)置基線偏移值.png

#pragma mark ** 16. NSObliquenessAttributeName 設(shè)置字形傾斜度取值為 NSNumber (float),正值右傾屁桑,負(fù)值左傾/* 此屬性的值是包含一個浮點(diǎn)值的NSNumber對象。默認(rèn)值為0栏赴,表示沒有傾斜, 正值右傾,負(fù)值左傾靖秩。 */[mAttStri addAttribute:NSObliquenessAttributeNamevalue:@0.8range:NSMakeRange(135,15)];

設(shè)置字形傾斜度取值.png

#pragmamark ** 17. NSExpansionAttributeName 設(shè)置文本橫向拉伸屬性/* 取值為 NSNumber(float), 正值橫向拉伸文本, 負(fù)值橫向壓縮文本 */NSRange range =? [stringrangeOfString:@"An association of"];? ? [mAttStri addAttribute:NSExpansionAttributeName value:@1.0range:range];

設(shè)置文本橫向拉伸屬性png

#pragma mark ** 18. NSWritingDirectionAttributeName 設(shè)置文字書寫方向/**? ? ? * 取值為包含NSNumber對象的數(shù)組. 從左向右書寫或者從右向左書寫.? ? *? ? * The values of the NSNumber objects should be0,1,2,or3,forLRE, RLE, LRO,orRLO respectively,andcombinations of NSWritingDirectionLeftToRightandNSWritingDirectionRightToLeftwithNSTextWritingDirectionEmbeddingorNSTextWritingDirectionOverride,asshowninValues of NSWritingDirectionAttributeNameandequivalent markup.? ? ? ? ? */? ? NSRange rang2 = [string rangeOfString:@"characters and their"];? ? [mAttStri addAttribute:NSWritingDirectionAttributeName value:@[@3] range:rang2];

設(shè)置文字書寫方向.png

#pragma mark ** 19. NSVerticalGlyphFormAttributeName 設(shè)置文字排版方向/**

? ? * 值為整型NSNumber须眷,0為水平排版的字,1為垂直排版的字沟突。注意,在iOS中, 總是以橫向排版

? ? *

? ? * In iOS, horizontal text is always used and specifying a different value is undefined.

? ? */[mAttStri addAttribute:NSVerticalGlyphFormAttributeNamevalue:@1range:NSMakeRange(1,10)];

設(shè)置文字排版方向.png

#pragma mark ** 20. NSLinkAttributeName 設(shè)置鏈接屬性/**

? ? * 此屬性的值是NSURL對象(首選)或一個NSString對象花颗。此屬性的默認(rèn)值為nil,表示沒有鏈接惠拭。

? ? * UILabel無法使用該屬性, 可以使用UITextView 控件.

? ? */UITextView*textView = [[UITextViewalloc] initWithFrame:CGRectMake(20,450,320,60)];? ? [self.view addSubview:textView];? ? textView.backgroundColor? = [UIColorlightGrayColor];NSString*strLink =@"百度鏈接";NSAttributedString*attStr? = [[NSAttributedStringalloc] initWithString:strLink attributes:@{NSLinkAttributeName: [NSURLURLWithString:@"http://www.baidu.com"]}];? ? ? ? textView.editable =NO;/* 簽訂協(xié)議, 指定代理人之后. 但點(diǎn)擊鏈接時, 會回調(diào)協(xié)議方法 (- textView:shouldInteractWithURL:inRange:) */textView.delegate =self;? ? ? ? textView.attributedText = attStr;

設(shè)置鏈接屬性.png

#pragma mark ** 21. NSAttachmentAttributeName 設(shè)置文本附件/* 這個屬性的值是一個NSTextAttachment對象扩劝。此屬性的默認(rèn)值為nil,表示無附件职辅。*//**

? ? * 關(guān)于NSTextAttachment類的簡單說明

? ? *

? ? * NSTextAttachment 類有一個指定的初始化方法(- initWithData:ofType:), 需要指定附件文檔的數(shù)據(jù)和附件文件的類型. 如果附件文檔數(shù)據(jù)指定nil, 那么系統(tǒng)將會默認(rèn)指定為image對象作為值. 因此, 也可以通過這個特性實(shí)現(xiàn)圖文混排.

? ? * 下面就以附件為image對象來說明NSAttachmentAttributeName的使用.

? ? *

? ? */UILabel*label = [[UILabelalloc] initWithFrame:CGRectMake(20,550,320,60)];? ? label.backgroundColor = [UIColoryellowColor];? ? [self.view addSubview:label];/* 下面實(shí)現(xiàn)在百度兩個漢字之間插入一個照片 */NSString*stiAtt =@"百度";NSTextAttachment*attach = [[NSTextAttachmentalloc] initWithData:nilofType:nil];? ? attach.bounds =CGRectMake(0,0,50,50);? ? attach.image = [UIImageimageNamed:@"baidu.jpg"];NSAttributedString*strAtt = [NSAttributedStringattributedStringWithAttachment:attach];NSMutableAttributedString*strMatt = [[NSMutableAttributedStringalloc] initWithString:stiAtt];? ? ? ? [strMatt insertAttributedString:strAtt atIndex:1];? ? ? ? label.attributedText = strMatt;self.titleLabel.attributedText = mAttStri;? ? [self.titleLabel sizeToFit];? ? }#pragma mark - textView delegate - (BOOL)textView:(UITextView*)textView shouldInteractWithURL:(NSURL*)URL inRange:(NSRange)characterRange {NSLog(@"%s", __func__);NSLog(@"url: %@", URL);returnYES;}

設(shè)置文本附件.png

小禮物走一走棒呛,來簡書關(guān)注我

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市域携,隨后出現(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ī)與錄音葛碧,去河邊找鬼借杰。 笑死,一個胖子當(dāng)著我的面吹牛进泼,可吹牛的內(nèi)容都是我干的蔗衡。 我是一名探鬼主播,決...
    沈念sama閱讀 40,755評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼乳绕,長吁一口氣:“原來是場噩夢啊……” “哼绞惦!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起刷袍,我...
    開封第一講書人閱讀 39,660評論 0 276
  • 序言:老撾萬榮一對情侶失蹤翩隧,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后呻纹,有當(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
  • 正文 我和宋清朗相戀三年雷酪,在試婚紗的時候發(fā)現(xiàn)自己被綠了淑仆。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,427評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡哥力,死狀恐怖蔗怠,靈堂內(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. 我叫王不留,地道東北人槽地。 一個月前我還...
    沈念sama閱讀 48,808評論 3 376
  • 正文 我出身青樓迁沫,卻偏偏與公主長得像,于是被迫代替她去往敵國和親捌蚊。 傳聞我的和親對象是個殘疾皇子集畅,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,440評論 2 359

推薦閱讀更多精彩內(nèi)容