国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS利用NSMutableAttributedString實現富文本的方法小結

2019-10-21 18:41:08
字體:
來源:轉載
供稿:網友

前言

在iOS開發過程中,經常會用到給字體加下劃線,顯示不同顏色和大小的字體等需求,經常遇到這種需求都是直接到百度或者谷歌直接把代碼粘過來,并沒有做系統的整理,今天剛好有時間,把這部分的內容整理一下,便于后續的開發,閑話不說,接下來就跟著我一起來了解一下NSMutableAttributedString吧.

NSAttributedString

NSAttributedString對象管理適用于字符串中單個字符或字符范圍的字符串和關聯的屬性集(例如字體和字距)。NSAttributedString對象的默認字體是Helvetica 12點,可能與平臺的默認系統字體不同。因此,您可能希望創建適用于您的應用程序的非默認屬性的新字符串。您還可以使用NSParagraphStyle類及其子類NSMutableParagraphStyle來封裝NSAttributedString類使用的段落或標尺屬性。

實例化方法和使用方法

實例化方法

使用字符串初始化

- (instancetype)initWithString:(NSString *)str;

代碼示例

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"測試數據"];

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

- (instancetype)initWithString:(NSString *)str attributes:(NSDictionary<NSString *,id> *)attrs;

代碼示例

NSDictionary *attributedDict = @{          NSFontAttributeName:[UIFont systemFontOfSize:16.0],          NSForegroundColorAttributeName:[UIColor redColor],          NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick)          }; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"測試數據" attributes:attributedDict];

使用NSAttributedString初始化,與NSMutableString,NSString類似

- (instancetype)initWithAttributedString:(NSAttributedString *)attrStr;

使用方法

為某一范圍內的文字設置多個屬性的方法

- (void)setAttributes:(NSDictionary<NSString *,id> *)attrs range:(NSRange)range;

//代碼示例

NSString *string = @"測試數據";NSDictionary *attributedDict = @{          NSFontAttributeName:[UIFont systemFontOfSize:16.0],          NSForegroundColorAttributeName:[UIColor redColor],          NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick)          };NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string]; [attributedString setAttributes:attributedDict range:NSMakeRange(0, string.length)];

為某一范圍內的文字添加某個屬性的方法

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

//代碼示例

NSString *string = @"測試數據";NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string]; [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, string.length)];

為某一范圍內的文字添加多個屬性的方法

- (void)addAttributes:(NSDictionary<NSString *,id> *)attrs range:(NSRange)range;

//代碼示例

NSString *string = @"測試數據";NSDictionary *attributedDict = @{          NSFontAttributeName:[UIFont systemFontOfSize:16.0],          NSForegroundColorAttributeName:[UIColor redColor],          NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick)          };NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string]; [attributedString addAttributes:attributedDict range:NSMakeRange(0, string.length)];

移除某個范圍內的某個屬性的方法

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

//代碼示例

 NSString *string = @"測試數據"; NSDictionary *attributedDict = @{          NSFontAttributeName:[UIFont systemFontOfSize:16.0],          NSForegroundColorAttributeName:[UIColor redColor],          NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick)          };NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string]; [attributedString addAttributes:attributedDict range:NSMakeRange(0, string.length)];  [attributedString removeAttribute:NSForegroundColorAttributeName range:NSMakeRange(0, string.length)];

屬性及說明

key 說明
NSFontAttributeName 字體,value是UIFont對象
NSParagraphStyleAttributeName 繪圖的風格(居中,換行模式,間距等諸多風格),value是NSParagraphStyle對象
NSForegroundColorAttributeName 文字顏色,value是UIFont對象
NSLigatureAttributeName 字符連體,value是NSNumber
NSKernAttributeName 字符間隔
NSStrikethroughStyleAttributeName 刪除線,value是NSNumber
NSUnderlineStyleAttributeName 下劃線,value是NSNumber
NSStrokeColorAttributeName 描繪邊顏色,value是UIColor
NSStrokeWidthAttributeName 描邊寬度,value是NSNumber
NSShadowAttributeName 陰影,value是NSShadow對象
NSTextEffectAttributeName 文字效果,value是NSString
NSAttachmentAttributeName 附屬,value是NSTextAttachment 對象
NSLinkAttributeName 鏈接,value是NSURL or NSString
NSBaselineOffsetAttributeName 基礎偏移量,value是NSNumber對象
NSStrikethroughColorAttributeName 刪除線顏色,value是UIColor
NSObliquenessAttributeName 字體傾斜
NSExpansionAttributeName 字體扁平化
NSVerticalGlyphFormAttributeName 垂直或者水平,value是 NSNumber,0表示水平,1垂直

富文本段落排版格式屬性說明

屬性 說明
lineSpacing 字體的行間距
firstLineHeadIndent 首行縮進
alignment (兩端對齊的)文本對齊方式:(左,中,右,兩端對齊,自然)
lineBreakMode 結尾部分的內容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz")
headIndent 整體縮進(首行除外)
minimumLineHeight 最低行高
maximumLineHeight 最大行高
paragraphSpacing 段與段之間的間距
paragraphSpacingBefore 段首行空白空間
baseWritingDirection 書寫方向(一共三種)
hyphenationFactor 連字屬性 在iOS,唯一支持的值分別為0和1

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鄂州市| 修水县| 鞍山市| 红河县| 潮州市| 邵武市| 上栗县| 元阳县| 九台市| 鹤山市| 博罗县| 景洪市| 佛冈县| 七台河市| 科技| 左贡县| 通江县| 山阴县| 瓮安县| 禹州市| 云林县| 德兴市| 太白县| 江源县| 长丰县| 阿坝县| 锡林郭勒盟| 错那县| 易门县| 贵德县| 宾川县| 防城港市| 鲁山县| 本溪市| 全州县| 连江县| 湖南省| 武邑县| 达拉特旗| 南丹县| 永嘉县|