TTTAttributedLabel的簡單使用

因?yàn)樾枨笠獙懸粋€(gè)聊天室,簡單地顯示用戶昵稱和輸入信息,點(diǎn)擊用戶昵稱需要禁言,label上實(shí)現(xiàn)部分字符串點(diǎn)擊好像有點(diǎn)麻煩,就直接使用TTTAttributedLabel了。
TTTAttributedLabel支持xib,我就直接在xib上改了label類型,然鵝改完一直有報(bào)錯(cuò):


image.png

很神奇,不影響項(xiàng)目運(yùn)行,pod update了一下,短暫的消失了一陣,又莫名其妙的出現(xiàn)了,到現(xiàn)在也沒消失。。
如果你要用NSAttributedString的話,直接設(shè)置label的字體字號顏色就不起作用。我看大多都調(diào)用這個(gè)方法:

[_contentLabel setText:str afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
        NSRange clickRange = [[mutableAttributedString string] rangeOfString:msgModel.user];
        [mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[color CGColor] range:clickRange];
        return mutableAttributedString;
}];

簡單設(shè)置點(diǎn)擊部分的顏色,實(shí)現(xiàn)效果是這樣的:

image.png

你想要設(shè)置的屬性必須加在NSMutableAttributedString中,而且設(shè)置label的text而不是attributedText

NSString *str = [NSString stringWithFormat:@"%@: %@", msgModel.user, msgModel.content];
    NSMutableAttributedString *attriStr = [[NSMutableAttributedString alloc] initWithString:str];
    [attriStr addAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} range:[str rangeOfString:str]];
    [attriStr addAttributes:@{NSForegroundColorAttributeName: color} range:NSMakeRange(0, msgModel.user.length + 1)];
    [attriStr addAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:13 weight:UIFontWeightMedium]} range:[str rangeOfString:str]];
    _contentLabel.text = attriStr;

為昵稱部分添加點(diǎn)擊事件,以下兩種方法都可以:

[_contentLabel addLinkToTransitInformation:@{@"selecter":@"forbiddenWords"} withRange:NSMakeRange(0, msgModel.user.length + 1)];

#pragma mark - TTTAttributedLabelDelegate
- (void)attributedLabel:(TTTAttributedLabel *)label
didSelectLinkWithTransitInformation:(NSDictionary *)components {
  
       NSLog(@"禁言%@", _msgModel.user);
}
[_contentLabel addLinkToURL:[NSURL URLWithString:@"forbiddenWords"] withRange:NSMakeRange(0, msgModel.user.length + 1)];

#pragma mark - TTTAttributedLabelDelegate
- (void)attributedLabel:(__unused TTTAttributedLabel *)label
   didSelectLinkWithURL:(NSURL *)url {
    NSLog(@"禁言%@", _msgModel.user);
}

鏈接下面默認(rèn)有下劃線的,可以去掉:

_contentLabel.linkAttributes = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:false] forKey:(NSString *)kCTUnderlineStyleAttributeName];

這時(shí)候點(diǎn)擊昵稱加一個(gè)禁言彈窗,使用UIAlertController,彈窗出來時(shí)會出現(xiàn)一個(gè)問題:

image.png

昵稱的字變灰了,而且彈窗關(guān)掉之后沒有變回原來顏色,變成了黑色。。
然后我把最上面setText的方法換成

    NSString *str = [NSString stringWithFormat:@"%@: %@", msgModel.user, msgModel.content];
    NSMutableAttributedString *attriStr = [[NSMutableAttributedString alloc] initWithString:str];
    [attriStr addAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} range:[str rangeOfString:str]];
    [attriStr addAttributes:@{NSForegroundColorAttributeName: color} range:NSMakeRange(0, msgModel.user.length + 1)];
    [attriStr addAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:13 weight:UIFontWeightMedium]} range:[str rangeOfString:str]];
    _contentLabel.text = attriStr;

彈窗出現(xiàn)的時(shí)候昵稱仍然會變灰,但是彈窗關(guān)掉之后會變回正常的彩色,不知道是不是有屬性可以控制這個(gè)問題,知道的大神告訴我哦

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

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