iOS 计算文字宽度

作者: 不要虚度美好的时光 | 来源:发表于2021-01-29 09:18 被阅读0次

iOS 计算文字宽度

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    // 计算宽度!!
    static int count = 0;
    NSString *str = @"";
    if (count % 2 == 0) {
        str = @"剩余18460天12小时20分79秒";
    }else if (count % 3 == 0) {
        str = @"已过去60天12小时20分79秒";
    }else if (count % 5 == 0) {
        str = @"已过去60天12小时20分79秒已过去60天12小时20分79秒";
    }else if (count % 7 == 0)  {
        str = @"活动已结束!";
    }else {
        str = @"1122";
    }
    _testLable.text = str;

    CGFloat width1=[(NSString *)str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 21) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]} context:nil].size.width;
    
    _lableWidth_Constraint.constant = width1 + 5;
    
    count++;
}

相关文章

网友评论

    本文标题:iOS 计算文字宽度

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