• UILabel自适应高、宽


    根据Label和字体大小自适应高度

    - (CGFloat)getHeightWithLabel:(UILabel *)label andFontSize:(CGFloat)size
    {
        label.numberOfLines = 0;
        NSMutableAttributedString *labelString = [[NSMutableAttributedString alloc] initWithString:label.text];
        [labelString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:size] range:NSMakeRange(0, [labelString length])];
        CGRect labelStringRect = [labelString boundingRectWithSize:CGSizeMake(label.frame.size.width, 9999) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
        CGRect labelRect = label.frame;
        labelRect.size.height = labelStringRect.size.height;
        label.frame = labelRect;
        label.attributedText = labelString;
        return label.frame.size.height;
    }

    根据Label和字体大小自适应宽度

    - (CGFloat)getWidthWithLabel:(UILabel *)label andFontSize:(CGFloat)size
    {
        label.numberOfLines = 0;
        NSMutableAttributedString *labelString = [[NSMutableAttributedString alloc] initWithString:label.text];
        [labelString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:size] range:NSMakeRange(0, [labelString length])];
        CGRect labelStringRect = [labelString boundingRectWithSize:CGSizeMake(9999, label.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
        CGRect labelRect = label.frame;
        labelRect.size.width = labelStringRect.size.width;
        label.frame = labelRect;
        label.attributedText = labelString;
        return label.frame.size.width;
    }
  • 相关阅读:
    机器学习-TensorFlow2.0安装简易教程
    14 深度学习-卷积
    用python画出你的童年回忆
    13-垃圾邮件分类2
    事后诸葛亮分析
    团队项目四:项目冲刺之日志集合贴
    第 1 篇 Scrum 冲刺博客
    团队作业1——团队展示&选题
    结对作业:四则运算(Java+JavaFX)
    JavaGUI之Swing简单入门示例
  • 原文地址:https://www.cnblogs.com/fanzhiying/p/4826107.html
Copyright © 2020-2023  润新知