YYKit源碼探究(七十五) —— NSMutableAttributedString分類之Convenience methods for text highlight(十四)

版本記錄

版本號 時間
V1.0 2018.04.01

前言

iOS圈內(nèi)有幾個人大家基本都知道听盖,比如說王巍、唐巧幢痘,還有YYKit框架的作者現(xiàn)任職于滴滴的郭曜源 - ibireme等唬格。這里有一篇唐巧對他的專訪,還有他的 GitHub - Yaoyuan博客颜说,這里貼出來框架YYKit 框架购岗。接下來幾篇我們就一起來看一下這個框架。感興趣的可以看上面寫的幾篇门粪。
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分類之架構分析和Font Traits(一)
10. YYKit源碼探究(十) —— UIFont分類之Create font(二)
11. YYKit源碼探究(十一) —— UIFont分類之Load and unload font(三)
12. YYKit源碼探究(十二) —— UIFont分類之Dump font data(四)
13. YYKit源碼探究(十三) —— UIImage分類之框架結構和Create image部分(一)
14. YYKit源碼探究(十四) —— UIImage分類之Image Info(二)
15. YYKit源碼探究(十五) —— UIImage分類之Modify Image(三)
16. YYKit源碼探究(十六) —— UIImage分類之Image Effect(四)
17. YYKit源碼探究(十七) —— UIImageView分類之架構和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(九)
71. YYKit源碼探究(七十一) —— NSMutableAttributedString分類之Set character attribute as property(十)
72. YYKit源碼探究(七十二) —— NSMutableAttributedString分類之Set paragraph attribute as property(十一)
73. YYKit源碼探究(七十三) —— NSMutableAttributedString分類之Set YYText attribute as property(十二)
74. YYKit源碼探究(七十四) —— NSMutableAttributedString分類之Set discontinuous attribute for range(十三)

回顧

上一篇主要介紹了NSMutableAttributedString分類的Set discontinuous attribute for range部分喊积,這一篇主要看一下NSMutableAttributedString分類的Convenience methods for text highlight部分。


API

下面我們看一下API

/**
 Convenience method to set text highlight
 
 @param range           text range
 @param color           text color (pass nil to ignore)
 @param backgroundColor text background color when highlight
 @param userInfo        user information dictionary (pass nil to ignore)
 @param tapAction       tap action when user tap the highlight (pass nil to ignore)
 @param longPressAction long press action when user long press the highlight (pass nil to ignore)
 */
- (void)setTextHighlightRange:(NSRange)range
                        color:(nullable UIColor *)color
              backgroundColor:(nullable UIColor *)backgroundColor
                     userInfo:(nullable NSDictionary *)userInfo
                    tapAction:(nullable YYTextAction)tapAction
              longPressAction:(nullable YYTextAction)longPressAction;

/**
 Convenience method to set text highlight
 
 @param range           text range
 @param color           text color (pass nil to ignore)
 @param backgroundColor text background color when highlight
 @param tapAction       tap action when user tap the highlight (pass nil to ignore)
 */
- (void)setTextHighlightRange:(NSRange)range
                        color:(nullable UIColor *)color
              backgroundColor:(nullable UIColor *)backgroundColor
                    tapAction:(nullable YYTextAction)tapAction;

/**
 Convenience method to set text highlight
 
 @param range           text range
 @param color           text color (pass nil to ignore)
 @param backgroundColor text background color when highlight
 @param userInfo        tap action when user tap the highlight (pass nil to ignore)
 */
- (void)setTextHighlightRange:(NSRange)range
                        color:(nullable UIColor *)color
              backgroundColor:(nullable UIColor *)backgroundColor
                     userInfo:(nullable NSDictionary *)userInfo;

下面我們就詳細的看一下這個API

1. - (void)setTextHighlightRange:(NSRange)range color:(nullable UIColor *)color backgroundColor:(nullable UIColor *)backgroundColor userInfo:(nullable NSDictionary *)userInfo tapAction:(nullable YYTextAction)tapAction longPressAction:(nullable YYTextAction)longPressAction;

這個是關于高亮文本的顏色以及點擊的設置玄妈。

方法實現(xiàn)

- (void)setTextHighlightRange:(NSRange)range
                        color:(UIColor *)color
              backgroundColor:(UIColor *)backgroundColor
                     userInfo:(NSDictionary *)userInfo
                    tapAction:(YYTextAction)tapAction
              longPressAction:(YYTextAction)longPressAction {
    YYTextHighlight *highlight = [YYTextHighlight highlightWithBackgroundColor:backgroundColor];
    highlight.userInfo = userInfo;
    highlight.tapAction = tapAction;
    highlight.longPressAction = longPressAction;
    if (color) [self setColor:color range:range];
    [self setTextHighlight:highlight range:range];
}

- (void)setTextHighlight:(YYTextHighlight *)textHighlight range:(NSRange)range {
    [self setAttribute:YYTextHighlightAttributeName value:textHighlight range:range];
}

2. - (void)setTextHighlightRange:(NSRange)range color:(nullable UIColor *)color backgroundColor:(nullable UIColor *)backgroundColor tapAction:(nullable YYTextAction)tapAction;

該方法的作用還是和高亮字體有關乾吻,包括設置顏色和點擊響應髓梅。

方法實現(xiàn)

- (void)setTextHighlightRange:(NSRange)range
                        color:(UIColor *)color
              backgroundColor:(UIColor *)backgroundColor
                    tapAction:(YYTextAction)tapAction {
    [self setTextHighlightRange:range
                          color:color
                backgroundColor:backgroundColor
                       userInfo:nil
                      tapAction:tapAction
                longPressAction:nil];
}

- (void)setTextHighlightRange:(NSRange)range
                        color:(UIColor *)color
              backgroundColor:(UIColor *)backgroundColor
                     userInfo:(NSDictionary *)userInfo
                    tapAction:(YYTextAction)tapAction
              longPressAction:(YYTextAction)longPressAction {
    YYTextHighlight *highlight = [YYTextHighlight highlightWithBackgroundColor:backgroundColor];
    highlight.userInfo = userInfo;
    highlight.tapAction = tapAction;
    highlight.longPressAction = longPressAction;
    if (color) [self setColor:color range:range];
    [self setTextHighlight:highlight range:range];
}

- (void)setTextHighlight:(YYTextHighlight *)textHighlight range:(NSRange)range {
    [self setAttribute:YYTextHighlightAttributeName value:textHighlight range:range];
}

3. - (void)setTextHighlightRange:(NSRange)range color:(nullable UIColor *)color backgroundColor:(nullable UIColor *)backgroundColor userInfo:(nullable NSDictionary *)userInfo;

該方法沒有點擊響應方式,只有區(qū)間和顏色設置绎签。

方法實現(xiàn)

- (void)setTextHighlightRange:(NSRange)range
                        color:(UIColor *)color
              backgroundColor:(UIColor *)backgroundColor
                     userInfo:(NSDictionary *)userInfo {
    [self setTextHighlightRange:range
                          color:color
                backgroundColor:backgroundColor
                       userInfo:userInfo
                      tapAction:nil
                longPressAction:nil];
}

后記

本篇主要介紹了NSMutableAttributedString分類的Convenience methods for text highlight枯饿,感興趣的給個贊或者關注~~~~

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市诡必,隨后出現(xiàn)的幾起案子鸭你,更是在濱河造成了極大的恐慌,老刑警劉巖擒权,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異阁谆,居然都是意外死亡碳抄,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進店門场绿,熙熙樓的掌柜王于貴愁眉苦臉地迎上來剖效,“玉大人,你說我怎么就攤上這事焰盗¤凳” “怎么了?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵熬拒,是天一觀的道長爷光。 經(jīng)常有香客問我,道長澎粟,這世上最難降的妖魔是什么蛀序? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮活烙,結果婚禮上徐裸,老公的妹妹穿的比我還像新娘。我一直安慰自己啸盏,他們只是感情好重贺,可當我...
    茶點故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著回懦,像睡著了一般气笙。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上粉怕,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天健民,我揣著相機與錄音,去河邊找鬼贫贝。 笑死秉犹,一個胖子當著我的面吹牛蛉谜,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播崇堵,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼型诚,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了鸳劳?” 一聲冷哼從身側響起狰贯,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎赏廓,沒想到半個月后涵紊,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡幔摸,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年摸柄,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片既忆。...
    茶點故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡驱负,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出患雇,到底是詐尸還是另有隱情跃脊,我是刑警寧澤,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布苛吱,位于F島的核電站酪术,受9級特大地震影響,放射性物質發(fā)生泄漏翠储。R本人自食惡果不足惜拼缝,卻給世界環(huán)境...
    茶點故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望彰亥。 院中可真熱鬧咧七,春花似錦、人聲如沸任斋。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽废酷。三九已至瘟檩,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間澈蟆,已是汗流浹背墨辛。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留趴俘,地道東北人睹簇。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓奏赘,卻偏偏與公主長得像,于是被迫代替她去往敵國和親太惠。 傳聞我的和親對象是個殘疾皇子磨淌,可洞房花燭夜當晚...
    茶點故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內(nèi)容