How to check if UILabel is truncated?

前端 未结 20 2746
不知归路
不知归路 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:16

    you can make a category with UILabel

    - (BOOL)isTextTruncated
    
    {
        CGRect testBounds = self.bounds;
        testBounds.size.height = NSIntegerMax;
        CGRect limitActual = [self textRectForBounds:[self bounds] limitedToNumberOfLines:self.numberOfLines];
        CGRect limitTest = [self textRectForBounds:testBounds limitedToNumberOfLines:self.numberOfLines + 1];
        return limitTest.size.height>limitActual.size.height;
    }
    

提交回复
热议问题