文本控件之UILabel

特性:可點(diǎn)擊的UILabel

很多視覺稿中都存在這樣的富文本控件:一個(gè)關(guān)鍵詞具有可點(diǎn)擊,點(diǎn)擊后會(huì)打開一個(gè)新頁面。


InteractiveLabel.jpg

可以參考github倉庫:MZSelectableLabel,鑒于網(wǎng)絡(luò)上使用demo太少,這里附上一例

MZSelectableLabel *licenseLabel = [[MZSelectableLabel alloc] init];
licenseLabel.frame = CGRectMake(kLeftRightPadding, greenButton.bottom + 10, SCREEN_WIDTH - 2 * kLeftRightPadding, 18);
licenseLabel.textAlignment = NSTextAlignmentCenter;
licenseLabel.backgroundColor = [UIColor clearColor];
licenseLabel.userInteractionEnabled = YES;
licenseLabel.attributedText = [NSAttributedString attributeStringWithBasePrefix:@"點(diǎn)擊同意代表你同意" keyWordSuffix:@"《XX服務(wù)協(xié)議》"];
[licenseLabel setSelectableRange:[licenseLabel.text rangeOfString:@"《XX服務(wù)協(xié)議》"]];
[self.view addSubview:licenseLabel];
@implementation NSAttributedString (Color)
+ (NSAttributedString *)attributeStringWithBasePrefix:(NSString *)basePrefix keyWordSuffix:(NSString *)keyWordSuffix
{
    NSMutableAttributedString *mergerString = [[NSMutableAttributedString alloc] init];
    
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.alignment = NSTextAlignmentCenter;
    {
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:basePrefix];
        NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:13.0],
                                     NSForegroundColorAttributeName:HEXCOLOR_ALPHA(@"#1D1D26", 0.5),
                                     NSParagraphStyleAttributeName:paragraphStyle};
        [attributedString setAttributes:attributes range:NSMakeRange(0, [attributedString length])];
        [mergerString appendAttributedString:attributedString];
    }
    
    {
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:keyWordSuffix];
        NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:13.0],
                                     NSForegroundColorAttributeName:HEXCOLOR(@"#42C642"),
                                     NSParagraphStyleAttributeName:paragraphStyle};
        [attributedString setAttributes:attributes range:NSMakeRange(0, [attributedString length])];
        [mergerString appendAttributedString:attributedString];
    }
    
    return mergerString;
}

最后編輯于
?著作權(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)容