可變字符串高亮加點(diǎn)擊

? ? NSString *text = @"為您推薦了**醫(yī)生,三甲全科教授醫(yī)生,查看醫(yī)生介紹。";

? ? NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString: text];

? ? /**

?? ? *? 設(shè)置整段文本size

?? ? */

? ? one.yy_font = [UIFont systemFontOfSize:15];

? ? /**

?? ? *? 獲得range, 只設(shè)置標(biāo)記文字的size、下劃線

?? ? */

? ? NSRangerange = [textrangeOfString:@"**"];

? ? [oneyy_setTextUnderline:[YYTextDecoration decorationWithStyle:YYTextLineStyleSingle] range:range];

? ? [oneyy_setFont:[UIFontsystemFontOfSize:15]range:range];

? ? /**

?? ? *? 被標(biāo)記的文字顏色

?? ? */

? ? UIColor *textColor = [UIColor colorWithRed:0.093 green:0.492 blue:1.000 alpha:1.000];


? ? /**

?? ? *? 被標(biāo)記文字點(diǎn)中后的背景顏色,會閃現(xiàn)一下

?? ? */

?? // UIColor *tapedBackgroundColor = [UIColor colorWithWhite:0.000 alpha:0.220];

? ? /**

?? ? *? 標(biāo)記文字點(diǎn)擊事件

?? ? */

? ? [oneyy_setTextHighlightRange:rangecolor:textColorbackgroundColor:niltapAction:^(UIView*_NonnullcontainerView,NSAttributedString*_Nonnulltext,NSRangerange,CGRectrect) {

?? ? ? ? NSLog(@"高亮1");

? ? ? ? // 點(diǎn)擊就是跳轉(zhuǎn)超鏈接了,在這里我輸出標(biāo)記文字

//? ? ? ? [self showMessage:[NSString stringWithFormat:@"%@, 字體大小: 30", [text.string substringWithRange:range]]];


? ? }];

? ? // 再標(biāo)記一段文字

? ? NSRangeotherRange = [textrangeOfString:@"查看醫(yī)生介紹"];

? ? [oneyy_setFont:[UIFontsystemFontOfSize:15]range:otherRange];

? ?[oneyy_setTextHighlightRange:otherRangecolor:textColorbackgroundColor:niltapAction:^(UIView*_NonnullcontainerView,NSAttributedString*_Nonnulltext,NSRangerange,CGRectrect) {


?? ? //? [self showMessage:[NSString stringWithFormat:@"%@, 字體大小: 20", [text.string substringWithRange:otherRange]]];


? ? ? ? NSLog(@"dic: %@", [textyy_fontAtIndex:otherRange.location]);

? ? }];

? ? self.chatLb.attributedText = one;

}

- (YYLabel*)chatLb {


? ? if(!_chatLb) {

? ? ? ? _chatLb= [[YYLabelalloc]init];

? ? ? ? _chatLb.font = [UIFont systemFontOfSize:15];

? ? ? ? _chatLb.textAlignment = NSTextAlignmentCenter;

? ? ? ? _chatLb.textVerticalAlignment = YYTextVerticalAlignmentCenter;

? ? ? ? _chatLb.numberOfLines = 0;

? ? ? ? _chatLb.text = @"就診信息已提交,這是您的就診信息";

? ? ? ? [_chatLb becomeFirstResponder];

? ? }

? ? return _chatLb;

}


注意,如果你的YYLabel是加載在UIView或者UIimageView這些控件上,是不會觸發(fā)點(diǎn)擊效果的,因為父視圖本身就不接受交互,你得先打開父視圖的交互


圖一

圖一里面,我是放在氣泡里面的,由于氣泡就是用UIImageView做的,所以不接受用戶交互,玩命點(diǎn)擊都沒觸發(fā)高亮字體的點(diǎn)擊效果,這是一個坑,注意注意.

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

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

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