日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

iOS使用NSMutableAttributedString实现富文本

發(fā)布時(shí)間:2024/1/17 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS使用NSMutableAttributedString实现富文本 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

轉(zhuǎn)載自:http://snowyshell.blog.163.com/blog/static/2209140342014475383375/

2014-05-07 17:38:33|??分類(lèi):?iOS學(xué)習(xí)|舉報(bào)|字號(hào)?訂閱

下載LOFTER客戶端

在iOS開(kāi)發(fā)中,常常會(huì)有一段文字顯示不同的顏色和字體,或者給某幾個(gè)文字加刪除線或下劃線的需求。之前在網(wǎng)上找了一些資料,有的是重繪UILabel的textLayer,有的是用html5實(shí)現(xiàn)的,都比較麻煩,而且很多UILabel的屬性也不起作用了,效果都不理想。后來(lái)了解到NSMuttableAttstring(帶屬性的字符串),上面的一些需求都可以很簡(jiǎn)便的實(shí)現(xiàn)。

  • 實(shí)例化方法和使用方法
  • 實(shí)例化方法:

    使用字符串初始化

    - (id)initWithString:(NSString?*)str;

    例:

    NSMutableAttributedString?*AttributedStr = [[NSMutableAttributedStringalloc]initWithString:@"今天天氣不錯(cuò)呀"];

    ?

    - (id)initWithString:(NSString?*)str attributes:(NSDictionary?*)attrs;

    ?

    字典中存放一些屬性名和屬性值,如:

    NSDictionary?*attributeDict = [NSDictionarydictionaryWithObjectsAndKeys:

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [UIFontsystemFontOfSize:15.0],NSFontAttributeName,

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [UIColorredColor],NSForegroundColorAttributeName,

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?NSUnderlineStyleAttributeName,NSUnderlineStyleSingle,nil];

    ?

    NSMutableAttributedString?*AttributedStr = [[NSMutableAttributedStringalloc]initWithString:@"今天天氣不錯(cuò)呀"?attributes:attributeDict];

    ?

    - (id)initWithAttributedString:(NSAttributedString?*)attester;

    使用NSAttributedString初始化,跟NSMutableString,NSString類(lèi)似

    ?

    使用方法:

    為某一范圍內(nèi)文字設(shè)置多個(gè)屬性

    - (void)setAttributes:(NSDictionary?*)attrs range:(NSRange)range;

    為某一范圍內(nèi)文字添加某個(gè)屬性

    - (void)addAttribute:(NSString?*)name value:(id)value range:(NSRange)range;

    ?

    為某一范圍內(nèi)文字添加多個(gè)屬性

    - (void)addAttributes:(NSDictionary?*)attrs range:(NSRange)range;

    移除某范圍內(nèi)的某個(gè)屬性

    - (void)removeAttribute:(NSString?*)name range:(NSRange)range;

  • 常見(jiàn)的屬性及說(shuō)明
  • NSFontAttributeName? 字體

    NSParagraphStyleAttributeName? 段落格式?

    NSForegroundColorAttributeName? 字體顏色

    NSBackgroundColorAttributeName?? 背景顏色

    NSStrikethroughStyleAttributeName 刪除線格式

    NSUnderlineStyleAttributeName? ? ? 下劃線格式

    NSStrokeColorAttributeName?? ? ?? 刪除線顏色

    NSStrokeWidthAttributeName 刪除線寬度

    NSShadowAttributeName? 陰影

    更多方法和屬性說(shuō)明詳見(jiàn)蘋(píng)果官方說(shuō)明文檔:

    https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableAttributedString_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40003689

  • 3.? ?使用實(shí)例
  • ? ?UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 320, 30)];

    ? testLabel.backgroundColor = [UIColor lightGrayColor];

    ? testLabel.textAlignment = NSTextAlignmentCenter;

    ? NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"今天天氣不錯(cuò)呀"];

    ? [AttributedStr addAttribute:NSFontAttributeName

    ? ? ? ? ? ? ? ? ? ? ? ? value:[UIFont systemFontOfSize:16.0]

    ? ? ? ? ? ? ? ? ? ? ? ? range:NSMakeRange(2, 2)];

    ? [AttributedStr addAttribute:NSForegroundColorAttributeName

    ? ? ? ? ? ? ? ? ? ? ? ? value:[UIColor redColor]

    ? ? ? ? ? ? ? ? ? ? ? ? range:NSMakeRange(2, 2)];

    ? testLabel.attributedText = AttributedStr;

    ? [self.view addSubview:testLabel];

    ?

    運(yùn)行效果:

    另外,其他可以設(shè)置text?的控件(如UIButton,UITextField)也都有該屬性,該文章不夠詳細(xì),只是簡(jiǎn)單介紹,其他效果的實(shí)現(xiàn)參考API中更多的屬性及使用方法。

    轉(zhuǎn)載于:https://www.cnblogs.com/BeyondAverage0908/p/4906812.html

    總結(jié)

    以上是生活随笔為你收集整理的iOS使用NSMutableAttributedString实现富文本的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。