iOS屬性字符串的API也不多颂暇,但是沒有系統(tǒng)的整理過概疆,現(xiàn)整理如下:
NSAttributedString所有API
//定義NSAttributedStringKey棘捣,其實就是字符串類型
typedef NSString * NSAttributedStringKey;
//獲取屬性字符串對應的普通字符串
@property (readonly, copy) NSString *string;
//屬性字符串的長度
@property (readonly) NSUInteger length;
//獲取range范圍內(nèi)抛杨,location位置開始的所有屬性字符串耘眨,返回值是字典像云,字典的key是NSAttributedStringKey,字典的value是對應的屬性對象
//range一般傳NULL淤翔,代表所有范圍翰绊,location一般從0開始
- (NSDictionary<NSAttributedStringKey, id> *)attributesAtIndex:(NSUInteger)location effectiveRange:(nullable NSRangePointer)range;
//獲取屬性名為attrName的對應的屬性對象
- (nullable id)attribute:(NSAttributedStringKey)attrName atIndex:(NSUInteger)location effectiveRange:(nullable NSRangePointer)range;
//獲取range范圍內(nèi)對應的屬性字符串
- (NSAttributedString *)attributedSubstringFromRange:(NSRange)range;
//獲取屬性字典
- (NSDictionary<NSAttributedStringKey, id> *)attributesAtIndex:(NSUInteger)location longestEffectiveRange:(nullable NSRangePointer)range inRange:(NSRange)rangeLimit;
//獲取attrName對應的屬性對象
- (nullable id)attribute:(NSAttributedStringKey)attrName atIndex:(NSUInteger)location longestEffectiveRange:(nullable NSRangePointer)range inRange:(NSRange)rangeLimit;
//判斷兩個屬性字符串是否相等
- (BOOL)isEqualToAttributedString:(NSAttributedString *)other;
//初始化屬性字符串
- (instancetype)initWithString:(NSString *)str;
- (instancetype)initWithString:(NSString *)str attributes:(nullable NSDictionary<NSAttributedStringKey, id> *)attrs;
- (instancetype)initWithAttributedString:(NSAttributedString *)attrStr;
typedef NS_OPTIONS(NSUInteger, NSAttributedStringEnumerationOptions) {
NSAttributedStringEnumerationReverse = (1UL << 1),
NSAttributedStringEnumerationLongestEffectiveRangeNotRequired = (1UL << 20)
};
//遍歷enumerationRange范圍內(nèi)的屬性字符串,結果是字典旁壮,結果在回調(diào)block里面
- (void)enumerateAttributesInRange:(NSRange)enumerationRange options:(NSAttributedStringEnumerationOptions)opts usingBlock:(void (NS_NOESCAPE ^)(NSDictionary<NSAttributedStringKey, id> *attrs, NSRange range, BOOL *stop))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
//遍歷enumerationRange范圍內(nèi)监嗜,屬性名attrName對應的屬性對象,結果在回調(diào)block里面
- (void)enumerateAttribute:(NSAttributedStringKey)attrName inRange:(NSRange)enumerationRange options:(NSAttributedStringEnumerationOptions)opts usingBlock:(void (NS_NOESCAPE ^)(id _Nullable value, NSRange range, BOOL *stop))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
NSMutableAttributedString所有API
//獲取屬性字符串對應的普通字符串
@property (readonly, retain) NSMutableString *mutableString;
//將range內(nèi)的東西替換為str
- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str;
//設置range范圍內(nèi)對應的屬性字典
- (void)setAttributes:(nullable NSDictionary<NSAttributedStringKey, id> *)attrs range:(NSRange)range;
//添加range范圍內(nèi)抡谐,屬性名為name對應的屬性對象
- (void)addAttribute:(NSAttributedStringKey)name value:(id)value range:(NSRange)range;
//移除range范圍內(nèi)裁奇,屬性名為name對應的屬性對象
- (void)removeAttribute:(NSAttributedStringKey)name range:(NSRange)range;
//添加range范圍內(nèi)的屬性字典
- (void)addAttributes:(NSDictionary<NSAttributedStringKey, id> *)attrs range:(NSRange)range;
對于addAttribute方法,attrName可以是NSAttributedStringKey麦撵,也可以是其他自定義的字符串作為key刽肠,當然獲取的時候也要使用這個key才可以獲取到對應的屬性對象溃肪,如下:
[attributedText addAttribute:@"specials" value:specials range:NSMakeRange(0, 1)];
NSArray *specials = [self.attributedText attribute:@"specials" atIndex:0 effectiveRange:NULL];
//將range內(nèi)的東西替換為attrString
- (void)replaceCharactersInRange:(NSRange)range withAttributedString:(NSAttributedString *)attrString;
//插入attrString到loc的位置
- (void)insertAttributedString:(NSAttributedString *)attrString atIndex:(NSUInteger)loc;
//后面拼接attrString
- (void)appendAttributedString:(NSAttributedString *)attrString;
//刪除range范圍內(nèi)的東西
- (void)deleteCharactersInRange:(NSRange)range;
//設置attrString
- (void)setAttributedString:(NSAttributedString *)attrString;
//開始編輯屬性字符串調(diào)用
- (void)beginEditing;
//結束編輯屬性字符串調(diào)用
- (void)endEditing;
NSAttributedStringKey
NSFontAttributeName:字體大小,取值為UIFont
NSForegroundColorAttributeName音五,字體顏色惫撰,取值為UIColor
NSBackgroundColorAttributeName,背景顏色躺涝,取值為UIColor
NSStrikethroughStyleAttributeName 設置刪除線厨钻,取值為NSUnderlineStyle,具體值看下面枚舉
NSStrikethroughColorAttributeName 設置刪除線顏色坚嗜,取值為UIColor
NSUnderlineStyleAttributeName 設置下劃線夯膀,取值為NSUnderlineStyle,具體值看下面枚舉
NSUnderlineColorAttributeName 設置下劃線顏色苍蔬,取值為UIColor
NSParagraphStyleAttributeName诱建,設置文本段落排版格式,取值為 NSMutableParagraphStyle 對象(詳情見下面的API說明)
NSAttachmentAttributeName 設置文本附件,取值為NSTextAttachment對象,常用于文字圖片混排
NSLinkAttributeName 設置鏈接屬性银室,取值為NSURL對象,點擊后調(diào)用瀏覽器打開指定URL地址
NSStrokeWidthAttributeName 設置筆畫寬度(粗細)励翼,取值為 NSNumber 對象(整數(shù))蜈敢,負值填充效果,正值中空效果
NSStrokeColorAttributeName 設置筆畫填充部分顏色汽抚,不是字體顏色抓狭,取值為 UIColor 對象
NSShadowAttributeName 設置陰影屬性,取值為 NSShadow 對象
NSTextEffectAttributeName 設置印刷效果造烁,取值為 NSTextEffectStyle否过,目前只有NSTextEffectLetterpressStyle(圖版印刷效果)可用
NSBaselineOffsetAttributeName 設置基線偏移值,取值為 NSNumber (float),正值上偏惭蟋,負值下偏
NSKernAttributeName 設置字符間距苗桂,取值為 NSNumber 對象(整數(shù)),正值間距加寬告组,負值間距變窄
NSLigatureAttributeName 設置連體屬性煤伟,取值為NSNumber 對象(整數(shù)),0 表示沒有連體字符木缝,1 表示使用默認的連體字符
NSObliquenessAttributeName 設置字形傾斜度便锨,取值為 NSNumber (float),正值右傾,負值左傾
NSExpansionAttributeName 設置文本橫向拉伸屬性我碟,取值為 NSNumber (float),正值橫向拉伸文本放案,負值橫向壓縮文本
NSWritingDirectionAttributeName 設置文字書寫方向,取值為NSWritingDirection
NSVerticalGlyphFormAttributeName 設置文字排版方向矫俺,取值為NSTextLayoutOrientation吱殉,默認橫向排版
NSMutableParagraphStyle
文本段落排版格式掸冤,對象屬性如下:
CGFloat lineSpacing; 字體的行間距
CGFloat paragraphSpacing; 段與段之間的間距
NSTextAlignment alignment; 對齊方式,具體值看下面枚舉
CGFloat firstLineHeadIndent; 首行縮進多少
CGFloat headIndent; 頭部縮進多少(首行除外)
CGFloat tailIndent; 尾部縮進多少
NSLineBreakMode lineBreakMode; 換行模式考婴,具體值看下面枚舉
CGFloat minimumLineHeight; 最小行高
CGFloat maximumLineHeight; 最大行高
NSWritingDirection baseWritingDirection; 書寫方向贩虾,具體值看下面枚舉
CGFloat lineHeightMultiple; 行高因子,在受最小和最大行高限制之前沥阱,將自然行高乘以這個因子(如果是正數(shù))缎罢。
CGFloat paragraphSpacingBefore; 上一段的底端(或段末的空格,如果有的話)到本段的頂部的距離考杉。
float hyphenationFactor; 連字符屬性策精,取值 0 到 1 之間,可以斷詞
其他枚舉
設置文字對齊方向:
typedef NS_ENUM(NSInteger, NSTextAlignment) {
NSTextAlignmentLeft = 0, // 默認左對齊
NSTextAlignmentCenter = 1, // 居中對齊
NSTextAlignmentRight = 2, // 右對齊
NSTextAlignmentJustified = 3, // 兩端對齊崇棠。段落的最后一行是自然對齊的咽袜。
NSTextAlignmentNatural = 4 // 指示腳本的默認對齊方式
};
設置換行:
typedef NS_ENUM(NSInteger, NSLineBreakMode) {
NSLineBreakByWordWrapping = 0, // 默認單詞換行,第一行末尾和第二行末尾都保留完整的單詞
NSLineBreakByClipping, // 兩行顯示UILabel能顯示多少就顯示多少枕稀,第一行末尾最后一個單詞沒有被截斷而是全部顯示到第一行询刹,第二行末尾是按字符裁剪,導致單詞typically只剩下typic
NSLineBreakByCharWrapping, // 字符換行萎坷,與Clip的區(qū)別在第一行凹联,將最后一個單詞截斷了
NSLineBreakByTruncatingHead, // 頭部省略,第一行末尾是完整單詞哆档,第二行最前面三個點來表示省略內(nèi)容 "...wxyz"蔽挠。
NSLineBreakByTruncatingMiddle, // 中間省略,第一行末尾是完整單詞瓜浸,第二行中間三個點來表示省略內(nèi)容 "abcd..."
NSLineBreakByTruncatingTail // 尾部省略澳淑,第一行末尾是完整單詞,第二行尾部三個點來表示省略內(nèi)容 "ab...yz"
};
設置書寫方向:
typedef NS_ENUM(NSInteger, NSWritingDirection) {
NSWritingDirectionNatural = -1, // 使用Unicode Bidi算法規(guī)則P2和P3確定方向
NSWritingDirectionLeftToRight = 0, // 從左到右書寫
NSWritingDirectionRightToLeft = 1 // 從右到左書寫
};
設置橫線樣式:
typedef NS_OPTIONS(NSInteger, NSUnderlineStyle) {
NSUnderlineStyleNone 無橫線
NSUnderlineStyleSingle 細單橫線
NSUnderlineStyleDouble 細雙橫線
NSUnderlineStyleThick 厚單橫線
NSUnderlinePatternSolid 連續(xù)的實線
NSUnderlinePatternDot 點插佛,比如這樣:------
NSUnderlinePatterDash 破折號杠巡,比如這樣:—— —— ——
NSUnderlinePatternDashDot 連續(xù)的破折號和點,比如這樣:——-——-——-
NSUnderlinePatternDashDotDot 破折號雇寇、點忽孽、點,比如:——--——--——--
NSUnderlineByWord 在有空格的地方不設置下劃線/刪除線
};
設置文字排版方向:
typedef NS_ENUM(NSInteger, NSTextLayoutOrientation) {
NSTextLayoutOrientationHorizontal = 0, // 橫向排版
NSTextLayoutOrientationVertical = 1, // 豎向排版
}