CJLabel已經(jīng)更新最新版本V2.1.2 ,相關(guān)文章介紹看這里CJLabel圖文混排二 —— UILabel插入圖片以及精確鏈點點擊
在NSAttributedString出現(xiàn)之前,UILabel只能做簡單的文本顯示,如果要顯示富文本我們只能通過Core Text來實現(xiàn),同時UILabel的userInteractionEnabled屬性默認(rèn)是NO的,即不能響應(yīng)用戶的交互操作。在本文我通過UILabel的attributedText來顯示富文本(關(guān)于對NSAttributedString的封裝以及size信息的計算,可參照上一篇文章動態(tài)計算NSAttributedString的size大小),同時判斷每次點擊label時對應(yīng)字符的NSRange位置信息,實現(xiàn)了任意字符的點擊響應(yīng)。
-
先上效果圖image
-
創(chuàng)建UILabel的子類CJLabel
@interface CJLabel : UILabel
/**
* 是否加大點擊響應(yīng)范圍,類似于UIWebView的鏈點點擊效果,默認(rèn)NO
*/
@property (nonatomic, assign) BOOL extendsLinkTouchArea;/** * 文本中內(nèi)容相同的鏈點是否都能夠響應(yīng)點擊, * 必須在設(shè)置self.attributedText前賦值, * 如果值為NO,則取文本中首次出現(xiàn)的鏈點, * 默認(rèn)YES */ @property (nonatomic, assign) IBInspectable BOOL sameLinkEnable; /** * 增加點擊鏈點 * * @param linkString 響應(yīng)點擊的字符串 * @param linkDic 響應(yīng)點擊的字符串的Attribute值 * @param parameter 響應(yīng)點擊的字符串的相關(guān)參數(shù):id,色值,字體大小等 * @param linkBlock 點擊回調(diào) */ - (void)addLinkString:(NSString *)linkString linkAddAttribute:(NSDictionary *)linkDic linkParameter:(id)parameter block:(CJLinkLabelModelBlock)linkBlock; /** * 取消點擊鏈點 * * @param linkString 取消點擊的字符串 */ - (void)removeLinkString:(NSAttributedString *)linkString; @end
方法- addLinkString: linkAddAttribute: linkParameter: block:
添加要響應(yīng)點擊事件的字符串,block為點擊該字符串對應(yīng)的回調(diào)事件;- removeLinkString:
移除點擊事件。
- (void)addLinkString:(NSAttributedString *)linkString block:(CJLinkLabelModelBlock)linkBlock {
CJLinkLabelModel *linkModel = [[CJLinkLabelModel alloc]initLinkLabelModelWithString:linkString range:[self getRangeWithLinkString:linkString] block:linkBlock];
if (nil != linkModel) {
[self.linkArray addObject:linkModel];
}
}
CJLinkLabelModel
是自定義的點擊鏈點model類,包含三個屬性linkBlock(該鏈點的點擊回調(diào)事件)、linkString(鏈點對應(yīng)的字符串)、range(鏈點對應(yīng)的NSRange信息)、parameter(可傳遞點擊鏈點的相關(guān)參數(shù):id,色值,字體大小等)。每添加一段能夠響應(yīng)點擊事件的字符串,都新創(chuàng)建一個CJLinkLabelModel
,并add到self.linkArray
中。
設(shè)置CJLabel的屬性self.userInteractionEnabled = YES
,同時重寫- touchesBegan: withEvent:
方法
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {
UITouch * touch = touches.anyObject;
//獲取觸摸點擊當(dāng)前view的坐標(biāo)位置
CGPoint location = [touch locationInView:self];
//判斷是否響應(yīng)了點擊事件
if(![self needResponseTouchLabel:location]) {
[self.nextResponder touchesBegan:touches withEvent:event];
}
}
- needResponseTouchLabel
方法將點擊坐標(biāo)轉(zhuǎn)化為對應(yīng)的第index個字符,然后遍歷self.linkArray
數(shù)組,判斷該字符是否在需響應(yīng)點擊事件的字符串?dāng)?shù)組內(nèi),是的話則響應(yīng)點擊回調(diào),否則向self.nextResponder
繼續(xù)傳遞點擊交互。
另外self.extendsLinkTouchArea
可以設(shè)置是否加大點擊響應(yīng)范圍,類似于UIWebView的鏈點點擊效果,默認(rèn)NO。
- removeLinkString:
方法
- (void)removeLinkString:(NSAttributedString *)linkString {
__block NSUInteger index = 0;
__block BOOL needRemove = NO;
[self.linkArray enumerateObjectsUsingBlock:^(id num, NSUInteger idx, BOOL *stop){
CJLinkLabelModel *linkModel = (CJLinkLabelModel *)num;
if ([linkModel.linkString isEqualToAttributedString:linkString]) {
index = idx;
needRemove = YES;
*stop = YES;
}
}];
[self.linkArray removeObjectAtIndex:index];
}
CJLabel的使用
- 使用cocoapods安裝
Podfileplatform :ios, '7.0'
pod 'CJLabel', '~> 1.0.3' - 直接引用
從GitHub下載CJLabel文件夾,直接引用到工程中。
最后附上demo地址