UILabel自适应内容高度总结

自适应内容高度现在总结两种方法

方法一:  其实就是先按着自己想要的效果给弄出来,然后根据宽度得到这个控件的cgsize

    NSString * stringStr =@"我是内容我是内容我是内容我是内容我是内容我是内容..";

    NSMutableAttributedString  * attstr = [[NSMutableAttributedString alloc] init];

    //label图片文字混排

    NSTextAttachment * textAttach=[[NSTextAttachment alloc]init];

    textAttach.image=[UIImage imageNamed:@"abstract"];

    textAttach.bounds=CGRectMake(0, 0, 42, 20);

    NSAttributedString *strA = [NSAttributedString attributedStringWithAttachment:textAttach];

    [attstr appendAttributedString:strA];

    [attstr appendAttributedString:[[NSAttributedString alloc] initWithString:stringStr]];

    //label行间距

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];

    [paragraphStyle setLineSpacing:10];

    [attstr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [stringStr length])];

    

    UILabel * lbzy = [[UILabel alloc]init];

    lbzy.attributedText=attstr;

    lbzy.lineBreakMode=NSLineBreakByWordWrapping;

    lbzy.numberOfLines=0;

    lbzy.font=[UIFont systemFontOfSize:13];

    //调节高度

    CGSize size = CGSizeMake(KWidth-20, MAXFLOAT);

    CGSize labelSize = [lbzy sizeThatFits:size];

    lbzy.frame = CGRectMake(10, 10,KWidth-20,labelSize.height);//设置labelframe


方法二:   这个是根据宽度得到内容的cgsize,然后赋值给label

    //标题

    NSString * stringStr = [@"我是标题" stringByTrimmingCharactersInSet:whitespace];

    CGSize size=[stringStr sizeWithFontCompatible:[UIFont systemFontOfSize:14] forWidth:(screenW-100) lineBreakMode:NSLineBreakByWordWrapping];

    _newsTitle.frame=CGRectMake(91, 0,screenW-100,size.height);

    _newsTitle.lineBreakMode=NSLineBreakByWordWrapping;

    _newsTitle.numberOfLines=0;

    _newsTitle.text=stringStr;

   

   

 NSDictionary *dictionaryAttributes = @{NSFontAttributeName:font,};

        CGRect stringRect = [self boundingRectWithSize:CGSizeMake(width,MAXFLOAT)

                                               options:NSStringDrawingUsesLineFragmentOrigin

                                            attributes:dictionaryAttributes

                                               context:nil];


 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值