麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統 > 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開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄色网欧美 | 在线观看视频日本 | 亚洲va国产va | 久久我不卡 | h色视频在线观看 | 一本色道久久综合狠狠躁篇适合什么人看 | 久久激情免费视频 | 草草在线观看 | 色99999| 久久成人精品视频 | 日韩精品中文字幕在线播放 | 99999久久久久久 | 羞羞网站视频 | 欧美激情视频一区二区免费 | 在线观看国产一区二区 | 羞羞电影网 | 毛片网站视频 | 成人精品视频在线 | 亚洲国产精品一区 | 免费a级毛片永久免费 | 欧美成人免费小视频 | 毛片在线视频免费观看 | 欧美级毛片 | 羞羞电影在线观看 | 91懂色| 久久精品探花 | 日韩av电影在线观看 | 欧美一级做性受免费大片免费 | 免费国产自久久久久三四区久久 | 精品国产91久久久久久久 | 国产精品视频导航 | 一区二区三区欧美在线 | 久久久久久久久久网站 | 国产亚洲精品成人a | 手机av在线电影 | 久久精品视频网站 | 在线影院av| 欧美成人国产va精品日本一级 | 黄色淫片| 久久成人免费观看 | 欧美日韩亚洲精品一区二区三区 |