How to check if UILabel is truncated?

前端 未结 20 2716
不知归路
不知归路 2020-11-28 02:53

I have a UILabel that can be varying lengths depending on whether or not my app is running in portrait or landscape mode on an iPhone or iPad. When the text is

20条回答
  •  借酒劲吻你
    2020-11-28 03:29

    Because all the answers above use depreciated methods, i thought this could be useful:

    - (BOOL)isLabelTruncated:(UILabel *)label
    {
        BOOL isTruncated = NO;
    
        CGRect labelSize = [label.text boundingRectWithSize:CGSizeFromString(label.text) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : label.font} context:nil];
    
        if (labelSize.size.width / labelSize.size.height > label.numberOfLines) {
    
            isTruncated = YES;
        }
    
        return isTruncated;
    }
    

提交回复
热议问题