iOS -UILabel自适应高度之富文本

iOS 常见的自动计算 UIlabel 正常文本 text 的自适应高度,计算富文本的有点少。今天把这个写出来,算作是自己的笔记。下面的 label 用的是 yylabel,换成 UIlabel 是一样的效果。只是UIlabel没有了点击字体对应的实现。

    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.由于支付渠道的要求,您的银行卡在在线支付时需要签署代扣授权书 \n2.输入验证码,即可在线签署授权书(验证码由易签宝发送)";
    NSMutableAttributedString *amStr = [[NSMutableAttributedString alloc] initWithString:tempStr];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    [paragraphStyle setLineSpacing:7];
    NSRange tempRage = [tempStr rangeOfString:@"代扣授权书"];
    [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];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容