iOS -UILabel自適應(yīng)高度之富文本

iOS 常見的自動計算 UIlabel 正常文本 text 的自適應(yīng)高度,計算富文本的有點少。今天把這個寫出來,算作是自己的筆記。下面的 label 用的是 yylabel,換成 UIlabel 是一樣的效果。只是UIlabel沒有了點擊字體對應(yīng)的實現(xiàn)。

    YYLabel *tempLabel = [[YYLabel alloc] initWithFrame:CGRectMake(15, 15, kWidth - 30, 100)];
    tempLabel.numberOfLines = 0;
    tempLabel.font = [UIFont systemFontOfSize:12];
    tempLabel.backgroundColor = [UIColor lightGrayColor];
    NSString *tempStr = @"注意事項:\n1.由于支付渠道的要求,您的銀行卡在在線支付時需要簽署代扣授權(quán)書 \n2.輸入驗證碼,即可在線簽署授權(quán)書(驗證碼由易簽寶發(fā)送)";
    NSMutableAttributedString *amStr = [[NSMutableAttributedString alloc] initWithString:tempStr];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    [paragraphStyle setLineSpacing:7];
    NSRange tempRage = [tempStr rangeOfString:@"代扣授權(quán)書"];
    [amStr yy_setTextHighlightRange:tempRage color:[UIColor redColor] backgroundColor:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        NSLog(@".....");
    }];
    CGFloat tempH = [tempStr boundingRectWithSize:CGSizeMake(kWidth-30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSParagraphStyleAttributeName:paragraphStyle} context:nil].size.height;
    tempLabel.height = tempH;
    tempLabel.attributedText = amStr;
    [self.view addSubview:tempLabel];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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