美文网首页
[iOS]UILabel的高度和宽度自适应

[iOS]UILabel的高度和宽度自适应

作者: 汴城码农 | 来源:发表于2021-03-05 16:59 被阅读0次

    创建类别

    .h中
    + (CGFloat)getHeightByWidth:(CGFloat)width title:(NSString *)title font:(UIFont*)font;
    
    + (CGFloat)getWidthWithTitle:(NSString *)title font:(UIFont *)font;
    
    .m中
    + (CGFloat)getHeightByWidth:(CGFloat)width title:(NSString *)title font:(UIFont *)font
    {
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
        label.text = title;
        label.font = font;
        label.numberOfLines = 0;
        [label sizeToFit];
        CGFloat height = label.frame.size.height;
        return height;
    }
    
    + (CGFloat)getWidthWithTitle:(NSString *)title font:(UIFont *)font {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1000, 0)];
        label.text = title;
        label.font = font;
        [label sizeToFit];
        return label.frame.size.width;
    }
    

    相关文章

      网友评论

          本文标题:[iOS]UILabel的高度和宽度自适应

          本文链接:https://www.haomeiwen.com/subject/jjxdqltx.html