YYKit源碼探究(七十一) —— NSMutableAttributedString分類之Set character attribute as property(十)

版本記錄

版本號(hào) 時(shí)間
V1.0 2018.04.01

前言

iOS圈內(nèi)有幾個(gè)人大家基本都知道,比如說(shuō)王巍、唐巧,還有YYKit框架的作者現(xiàn)任職于滴滴的郭曜源 - ibireme等。這里有一篇唐巧對(duì)他的專訪,還有他的 GitHub - Yaoyuan博客,這里貼出來(lái)框架YYKit 框架。接下來(lái)幾篇我們就一起來(lái)看一下這個(gè)框架。感興趣的可以看上面寫的幾篇。
1. YYKit源碼探究(一) —— 基本概覽
2. YYKit源碼探究(二) —— NSString分類之Hash(一)
3. YYKit源碼探究(三) —— NSString分類之Encode and decode(二)
4. YYKit源碼探究(四) —— NSString分類之Drawing(三)
5. YYKit源碼探究(五) —— NSString分類之Regular Expression(四)
6. YYKit源碼探究(六) —— NSString分類之NSNumber Compatible(五)
7. YYKit源碼探究(七) —— NSString分類之Utilities(六)
8. YYKit源碼探究(八) —— NSNumber分類(一)
9. YYKit源碼探究(九) —— UIFont分類之架構(gòu)分析和Font Traits(一)
10. YYKit源碼探究(十) —— UIFont分類之Create font(二)
11. YYKit源碼探究(十一) —— UIFont分類之Load and unload font(三)
12. YYKit源碼探究(十二) —— UIFont分類之Dump font data(四)
13. YYKit源碼探究(十三) —— UIImage分類之框架結(jié)構(gòu)和Create image部分(一)
14. YYKit源碼探究(十四) —— UIImage分類之Image Info(二)
15. YYKit源碼探究(十五) —— UIImage分類之Modify Image(三)
16. YYKit源碼探究(十六) —— UIImage分類之Image Effect(四)
17. YYKit源碼探究(十七) —— UIImageView分類之架構(gòu)和image部分(一)
18. YYKit源碼探究(十八) —— UIImageView分類之highlight image部分(二)
19. YYKit源碼探究(十九) —— UIScreen分類(一)
20. YYKit源碼探究(二十) —— UIScrollView分類(一)
21. YYKit源碼探究(二十一) —— UITableView分類(一)
22. YYKit源碼探究(二十二) —— UITextField分類(一)
23. YYKit源碼探究(二十三) —— UIView分類(一)
24. YYKit源碼探究(二十四) —— UIPasteboard分類(一)
25. YYKit源碼探究(二十五) —— UIGestureRecognizer分類(一)
26. YYKit源碼探究(二十六) —— UIDevice分類框架及Device Information(一)
27. YYKit源碼探究(二十七) —— UIDevice分類之Network Information(二)
28. YYKit源碼探究(二十八) —— UIDevice分類之Disk Space(三)
29. YYKit源碼探究(二十九) —— UIDevice分類之Memory Information(四)
30. YYKit源碼探究(三十) —— UIDevice分類之CPU Information(五)
31. YYKit源碼探究(三十一) —— UIControl分類(一)
32. YYKit源碼探究(三十二) —— UIColor分類之Create a UIColor Object(一)
33. YYKit源碼探究(三十三) —— UIColor分類之Get color's description(二)
34. YYKit源碼探究(三十四) —— UIColor分類之Retrieving Color Information(三)
35. YYKit源碼探究(三十五) —— UIButton分類之image(一)
36. YYKit源碼探究(三十六) —— UIButton分類之background image(二)
37. YYKit源碼探究(三十七) —— UIBezierPath分類(一)
38. YYKit源碼探究(三十八) —— UIBarButtonItem分類(一)
39. YYKit源碼探究(三十九) —— UIApplication分類(一)
40. YYKit源碼探究(四十) —— NSTimer分類(一)
41. YYKit源碼探究(四十一) —— NSParagraphStyle分類(一)
42. YYKit源碼探究(四十二) —— NSObject分類之YYModel(一)
43. YYKit源碼探究(四十三) —— NSObject分類之KVO(二)
44. YYKit源碼探究(四十四) —— NSObject分類之Sending messages with variable parameters(三)
45. YYKit源碼探究(四十五) —— NSObject分類之Swap method (Swizzling)(四)
46. YYKit源碼探究(四十六) —— NSObject分類之Associate value(五)
47. YYKit源碼探究(四十七) —— NSObject分類之Other(六)
48. YYKit源碼探究(四十八) —— NSNotificationCenter分類(一)
49. YYKit源碼探究(四十九) —— NSKeyedUnarchiver分類(一)
50. YYKit源碼探究(五十) —— NSDictionary分類之Dictionary Convertor(一)
51. YYKit源碼探究(五十一) —— NSDictionary分類之Dictionary Value Getter(二)
52. YYKit源碼探究(五十二) —— NSDictionary分類之NSMutableDictionary(三)
53. YYKit源碼探究(五十三) —— NSDate分類之Component Properties(一)
54. YYKit源碼探究(五十四) —— NSDate分類之Date modify(二)
55. YYKit源碼探究(五十五) —— NSDate分類之Date Format(三)
56. YYKit源碼探究(五十六) —— NSData分類之Hash(一)
57. YYKit源碼探究(五十七) —— NSData分類之Encrypt and Decrypt(二)
58. YYKit源碼探究(五十八) —— NSData分類之Encode and decode(三)
59. YYKit源碼探究(五十九) —— NSData分類之Inflate and deflate(四)
60. YYKit源碼探究(六十) —— NSData分類之Others(五)
61. YYKit源碼探究(六十一) —— NSBundle分類(一)
62. YYKit源碼探究(六十二) —— NSAttributedString分類之基本(一)
63. YYKit源碼探究(六十三) —— NSAttributedString分類之Retrieving character attribute information(二)
64. YYKit源碼探究(六十四) —— NSAttributedString分類之Get character attribute as property(三)
65. YYKit源碼探究(六十五) —— NSAttributedString分類之Get paragraph attribute as property(四)
66. YYKit源碼探究(六十六) —— NSAttributedString分類之Get YYText attribute as property(五)
67. YYKit源碼探究(六十七) —— NSAttributedString分類之Query for YYText(六)
68. YYKit源碼探究(六十八) —— NSAttributedString分類之Create attachment string for YYText(七)
69. YYKit源碼探究(六十九) —— NSAttributedString分類之Utility(八)
70. YYKit源碼探究(七十) —— NSMutableAttributedString分類之Set character attribute(九)

回顧

上一篇主要介紹了NSMutableAttributedString分類的Set character attribute部分,這一篇主要看一下NSMutableAttributedString分類的Set character attribute as property部分。


API

下面我們看一下API

1. @property (nullable, nonatomic, strong, readwrite) UIFont *font; 和 - (void)setFont:(nullable UIFont *)font range:(NSRange)range;

文本的字體,默認(rèn)是Helvetica (Neue) 12。

方法實(shí)現(xiàn)

- (void)setFont:(UIFont *)font {
    /*
     In iOS7 and later, UIFont is toll-free bridged to CTFontRef,
     although Apple does not mention it in documentation.
     
     In iOS6, UIFont is a wrapper for CTFontRef, so CoreText can alse use UIfont,
     but UILabel/UITextView cannot use CTFontRef.
     
     We use UIFont for both CoreText and UIKit.
     */
    [self setFont:font range:NSMakeRange(0, self.length)];
}

- (void)setFont:(UIFont *)font range:(NSRange)range {
    /*
     In iOS7 and later, UIFont is toll-free bridged to CTFontRef,
     although Apple does not mention it in documentation.
     
     In iOS6, UIFont is a wrapper for CTFontRef, so CoreText can alse use UIfont,
     but UILabel/UITextView cannot use CTFontRef.
     
     We use UIFont for both CoreText and UIKit.
     */
    [self setAttribute:NSFontAttributeName value:font range:range];
}

2. @property (nullable, nonatomic, strong, readwrite) NSNumber *kern; 和 - (void)setKern:(nullable NSNumber *)kern range:(NSRange)range;

字距調(diào)整,默認(rèn)是標(biāo)準(zhǔn)字距。 kerning屬性表示后面的字符應(yīng)該從其當(dāng)前字符的字體定義的默認(rèn)偏移量中移動(dòng)多少個(gè)點(diǎn); 整數(shù)的kern指示更遠(yuǎn)的移位,而負(fù)值kern指示更接近當(dāng)前的字符的移位。 如果此屬性不存在,則將使用標(biāo)準(zhǔn)字距。 如果此屬性設(shè)置為0.0,則根本不會(huì)執(zhí)行字距調(diào)整。

方法實(shí)現(xiàn)

- (void)setKern:(NSNumber *)kern {
    [self setKern:kern range:NSMakeRange(0, self.length)];
}

- (void)setKern:(NSNumber *)kern range:(NSRange)range {
    [self setAttribute:NSKernAttributeName value:kern range:range];
}

3. @property (nullable, nonatomic, strong, readwrite) UIColor *color; 和 - (void)setColor:(nullable UIColor *)color range:(NSRange)range;

獲取foreground color,默認(rèn)是black。

方法實(shí)現(xiàn)

- (void)setColor:(UIColor *)color {
    [self setColor:color range:NSMakeRange(0, self.length)];
}

- (void)setColor:(UIColor *)color range:(NSRange)range {
    [self setAttribute:(id)kCTForegroundColorAttributeName value:(id)color.CGColor range:range];
    [self setAttribute:NSForegroundColorAttributeName value:color range:range];
}

- (void)setAttribute:(NSString *)name value:(id)value range:(NSRange)range {
    if (!name || [NSNull isEqual:name]) return;
    if (value && ![NSNull isEqual:value]) [self addAttribute:name value:value range:range];
    else [self removeAttribute:name range:range];
}

4. @property (nullable, nonatomic, strong, readwrite) UIColor *backgroundColor; 和 - (void)setBackgroundColor:(nullable UIColor *)backgroundColor range:(NSRange)range;

獲取background color,默認(rèn)是nil,沒有背景色。

方法實(shí)現(xiàn)

- (void)setBackgroundColor:(UIColor *)backgroundColor {
    [self setBackgroundColor:backgroundColor range:NSMakeRange(0, self.length)];
}

- (void)setBackgroundColor:(UIColor *)backgroundColor range:(NSRange)range {
    [self setAttribute:NSBackgroundColorAttributeName value:backgroundColor range:range];
}

5. @property (nullable, nonatomic, strong, readwrite) NSNumber *strokeWidth; 和 - (void)setStrokeWidth:(nullable NSNumber *)strokeWidth range:(NSRange)range;

獲取描邊的線寬。默認(rèn)值是0,此屬性解釋為字體點(diǎn)值的百分比,用于控制文本繪圖模式:正值僅影響繪圖描邊;負(fù)值是描邊和填充。 輪廓文本的典型值為3.0。

方法實(shí)現(xiàn)

- (void)setStrokeWidth:(NSNumber *)strokeWidth {
    [self setStrokeWidth:strokeWidth range:NSMakeRange(0, self.length)];
}

- (void)setStrokeWidth:(NSNumber *)strokeWidth range:(NSRange)range {
    [self setAttribute:NSStrokeWidthAttributeName value:strokeWidth range:range];
}

6. @property (nullable, nonatomic, strong, readwrite) UIColor *strokeColor; 和 - (void)setStrokeColor:(nullable UIColor *)strokeColor range:(NSRange)range;

獲取描邊的顏色。默認(rèn)是nil,(same as foreground color)。

方法實(shí)現(xiàn)

- (void)setStrokeColor:(UIColor *)strokeColor {
    [self setStrokeColor:strokeColor range:NSMakeRange(0, self.length)];
}

- (void)setStrokeColor:(UIColor *)strokeColor range:(NSRange)range {
    [self setAttribute:(id)kCTStrokeColorAttributeName value:(id)strokeColor.CGColor range:range];
    [self setAttribute:NSStrokeColorAttributeName value:strokeColor range:range];
}

7. @property (nullable, nonatomic, strong, readwrite) NSShadow *shadow; 和 - (void)setShadow:(nullable NSShadow *)shadow range:(NSRange)range;

獲取的是文本陰影,默認(rèn)值是nil,沒有陰影。

方法實(shí)現(xiàn)

- (void)setShadow:(NSShadow *)shadow {
    [self setShadow:shadow range:NSMakeRange(0, self.length)];
}

- (void)setShadow:(NSShadow *)shadow range:(NSRange)range {
    [self setAttribute:NSShadowAttributeName value:shadow range:range];
}

8. @property (nonatomic, readwrite) NSUnderlineStyle strikethroughStyle; 和 - (void)setStrikethroughStyle:(NSUnderlineStyle)strikethroughStyle range:(NSRange)range;

獲取strikethrough style,默認(rèn)值是NSUnderlineStyleNone

方法實(shí)現(xiàn)

- (void)setStrikethroughStyle:(NSUnderlineStyle)strikethroughStyle {
    [self setStrikethroughStyle:strikethroughStyle range:NSMakeRange(0, self.length)];
}

- (void)setStrikethroughStyle:(NSUnderlineStyle)strikethroughStyle range:(NSRange)range {
    NSNumber *style = strikethroughStyle == 0 ? nil : @(strikethroughStyle);
    [self setAttribute:NSStrikethroughStyleAttributeName value:style range:range];
}

9. @property (nullable, nonatomic, strong, readwrite) UIColor *strikethroughColor; 和 - (void)setStrikethroughColor:(nullable UIColor *)strikethroughColor range:(NSRange)range

獲取strikethrough color,默認(rèn)值是nil,(same as foreground color)。

方法實(shí)現(xiàn)

- (void)setStrikethroughColor:(UIColor *)strikethroughColor {
    [self setStrikethroughColor:strikethroughColor range:NSMakeRange(0, self.length)];
}

- (void)setStrikethroughColor:(UIColor *)strikethroughColor range:(NSRange)range {
    if (kSystemVersion >= 7) {
        [self setAttribute:NSStrikethroughColorAttributeName value:strikethroughColor range:range];
    }
}

10. @property (nonatomic, readwrite) NSUnderlineStyle underlineStyle; 和 - (void)setUnderlineStyle:(NSUnderlineStyle)underlineStyle range:(NSRange)range;

獲取下劃線樣式,默認(rèn)值是NSUnderlineStyleNone,沒有下劃線。

方法實(shí)現(xiàn)

- (void)setUnderlineStyle:(NSUnderlineStyle)underlineStyle {
    [self setUnderlineStyle:underlineStyle range:NSMakeRange(0, self.length)];
}

- (void)setUnderlineStyle:(NSUnderlineStyle)underlineStyle range:(NSRange)range {
    NSNumber *style = underlineStyle == 0 ? nil : @(underlineStyle);
    [self setAttribute:NSUnderlineStyleAttributeName value:style range:range];
}

11. @property (nullable, nonatomic, strong, readwrite) UIColor *underlineColor; 和 - (void)setUnderlineColor:(nullable UIColor *)underlineColor range:(NSRange)range;

獲取下劃線顏色,默認(rèn)值是nil。

方法實(shí)現(xiàn)

- (void)setUnderlineColor:(UIColor *)underlineColor {
    [self setUnderlineColor:underlineColor range:NSMakeRange(0, self.length)];
}

- (void)setUnderlineColor:(UIColor *)underlineColor range:(NSRange)range {
    [self setAttribute:(id)kCTUnderlineColorAttributeName value:(id)underlineColor.CGColor range:range];
    if (kSystemVersion >= 7) {
        [self setAttribute:NSUnderlineColorAttributeName value:underlineColor range:range];
    }
}

12. @property (nullable, nonatomic, strong, readwrite) NSNumber *ligature; 和 - (void)setLigature:(nullable NSNumber *)ligature range:(NSRange)range;

獲取Ligature formation control。

默認(rèn)值為int值1,ligature屬性確定顯示字符串時(shí)應(yīng)使用哪種ligature。 值為0表示只應(yīng)使用正確呈現(xiàn)文本所必需的ligature,1表示應(yīng)使用標(biāo)準(zhǔn)ligature,而2表示應(yīng)使用所有可用ligature。 哪些ligature是標(biāo)準(zhǔn)的,取決于腳本和可能的字體。

方法實(shí)現(xiàn)

- (void)setLigature:(NSNumber *)ligature {
    [self setLigature:ligature range:NSMakeRange(0, self.length)];
}

- (void)setLigature:(NSNumber *)ligature range:(NSRange)range {
    [self setAttribute:NSLigatureAttributeName value:ligature range:range];
}

13. @property (nullable, nonatomic, strong, readwrite) NSString *textEffect; 和 - (void)setTextEffect:(nullable NSString *)textEffect range:(NSRange)range

獲取文本效果,默認(rèn)值是nil,當(dāng)前支持的值是NSTextEffectLetterpressStyle。

方法實(shí)現(xiàn)

- (void)setTextEffect:(NSString *)textEffect {
    [self setTextEffect:textEffect range:NSMakeRange(0, self.length)];
}

- (void)setTextEffect:(NSString *)textEffect range:(NSRange)range {
    if (kSystemVersion >= 7) {
        [self setAttribute:NSTextEffectAttributeName value:textEffect range:range];
    }
}

14. @property (nullable, nonatomic, strong, readwrite) NSNumber *obliqueness; 和 - (void)setObliqueness:(nullable NSNumber *)obliqueness range:(NSRange)range

要應(yīng)用于字形的傾斜,默認(rèn)值是0,沒有傾斜。

方法實(shí)現(xiàn)

- (void)setObliqueness:(NSNumber *)obliqueness {
    [self setObliqueness:obliqueness range:NSMakeRange(0, self.length)];
}

- (void)setObliqueness:(NSNumber *)obliqueness range:(NSRange)range {
    if (kSystemVersion >= 7) {
        [self setAttribute:NSObliquenessAttributeName value:obliqueness range:range];
    }
}

15. @property (nullable, nonatomic, strong, readwrite) NSNumber *expansion; 和 - (void)setExpansion:(nullable NSNumber *)expansion range:(NSRange)range

要應(yīng)用于字形的擴(kuò)展因子的日志,默認(rèn)值是0,沒有擴(kuò)展。

方法實(shí)現(xiàn)

- (void)setExpansion:(NSNumber *)expansion {
    [self setExpansion:expansion range:NSMakeRange(0, self.length)];
}

- (void)setExpansion:(NSNumber *)expansion range:(NSRange)range {
    if (kSystemVersion >= 7) {
        [self setAttribute:NSExpansionAttributeName value:expansion range:range];
    }
}

16. @property (nullable, nonatomic, strong, readwrite) NSNumber *baselineOffset; 和 - (void)setBaselineOffset:(nullable NSNumber *)baselineOffset range:(NSRange)range NS_AVAILABLE_IOS(7_0);

字符基線的偏移量,以點(diǎn)為單位。默認(rèn)值是0。

方法實(shí)現(xiàn)

- (void)setBaselineOffset:(NSNumber *)baselineOffset {
    [self setBaselineOffset:baselineOffset range:NSMakeRange(0, self.length)];
}

- (void)setBaselineOffset:(NSNumber *)baselineOffset range:(NSRange)range {
    if (kSystemVersion >= 7) {
        [self setAttribute:NSBaselineOffsetAttributeName value:baselineOffset range:range];
    }
}

17. @property (nonatomic, readwrite) BOOL verticalGlyphForm; 和 - (void)setVerticalGlyphForm:(BOOL)verticalGlyphForm range:(NSRange)range;

獲取Glyph orientation control,默認(rèn)值為NO。NO的值表示要使用水平字形,YES表示要使用垂直字形。

方法實(shí)現(xiàn)

- (void)setVerticalGlyphForm:(BOOL)verticalGlyphForm {
    [self setVerticalGlyphForm:verticalGlyphForm range:NSMakeRange(0, self.length)];
}

- (void)setVerticalGlyphForm:(BOOL)verticalGlyphForm range:(NSRange)range {
    NSNumber *v = verticalGlyphForm ? @(YES) : nil;
    [self setAttribute:NSVerticalGlyphFormAttributeName value:v range:range];
}

18. @property (nullable, nonatomic, strong, readwrite) NSString *language; 和 - (void)setLanguage:(nullable NSString *)language range:(NSRange)range

指定文本語(yǔ)言。

值必須是包含區(qū)域設(shè)置標(biāo)識(shí)符的NSString。 默認(rèn)值未設(shè)置。 當(dāng)這個(gè)屬性被設(shè)置為一個(gè)有效的標(biāo)識(shí)符時(shí),它將被用來(lái)選擇本地化的字形(如果字體支持)和特定于語(yǔ)言環(huán)境的換行規(guī)則。

方法實(shí)現(xiàn)

- (void)setLanguage:(NSString *)language {
    [self setLanguage:language range:NSMakeRange(0, self.length)];
}

- (void)setLanguage:(NSString *)language range:(NSRange)range {
    if (kSystemVersion >= 7) {
        [self setAttribute:(id)kCTLanguageAttributeName value:language range:range];
    }
}

19. @property (nullable, nonatomic, strong, readwrite) NSArray<NSNumber *> *writingDirection; 和 - (void)setWritingDirection:(nullable NSArray<NSNumber *> *)writingDirection range:(NSRange)range;

Specifies a bidirectional override or embedding

可以參考NSWritingDirectionNSWritingDirectionAttributeName。

方法實(shí)現(xiàn)

- (void)setWritingDirection:(NSArray *)writingDirection {
    [self setWritingDirection:writingDirection range:NSMakeRange(0, self.length)];
}

- (void)setWritingDirection:(NSArray *)writingDirection range:(NSRange)range {
    [self setAttribute:(id)kCTWritingDirectionAttributeName value:writingDirection range:range];
}

20. @property (nullable, nonatomic, strong, readwrite) NSParagraphStyle *paragraphStyle; 和 - (void)setParagraphStyle:(nullable NSParagraphStyle *)paragraphStyle range:(NSRange)range;

一個(gè)NSParagraphStyle對(duì)象,用于指定類似line alignment、tab rulers, writing direction等事情。

方法實(shí)現(xiàn)

- (void)setParagraphStyle:(NSParagraphStyle *)paragraphStyle {
    /*
     NSParagraphStyle is NOT toll-free bridged to CTParagraphStyleRef.
     
     CoreText can use both NSParagraphStyle and CTParagraphStyleRef,
     but UILabel/UITextView can only use NSParagraphStyle.
     
     We use NSParagraphStyle in both CoreText and UIKit.
     */
    [self setParagraphStyle:paragraphStyle range:NSMakeRange(0, self.length)];
}

- (void)setParagraphStyle:(NSParagraphStyle *)paragraphStyle range:(NSRange)range {
    /*
     NSParagraphStyle is NOT toll-free bridged to CTParagraphStyleRef.
     
     CoreText can use both NSParagraphStyle and CTParagraphStyleRef,
     but UILabel/UITextView can only use NSParagraphStyle.
     
     We use NSParagraphStyle in both CoreText and UIKit.
     */
    [self setAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
}

后記

本篇主要介紹了NSMutableAttributedString分類的Set character attribute as property,感興趣的給個(gè)贊或者關(guān)注~~~

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容