2012-07-01 35 views
2

在我的应用程序中,我有一个UILabel,它保存着不断增加的条目数量,例如每次用户按下按钮时,标签的末尾都会添加一个“1”。 但是,我希望能够检测何时向标签添加额外条目会导致它超出其容器的大小,并成为一个丑陋的“111 ...”标签,其所需行为是沿着以下方向的行为:是否可以检测标签是否即将在IOS中运行?

int maximumLengthBeforeOverrun = self.maximumLengthBeforeLabelOverrun; 
if(label.text.length > maximumLengthBeforeOverrun) { 
    NSString * newLabel = [label.text substringTo:label.text.length - 1] 
    label.text = newLabel; 
} 
label.text = [label.text appendWithString:toAppend] 

我的问题是IOS SDK中有一些方法可以为我做到这一点?或者我应该使用不同的方法来显示信息?

回答

4

你可以使用sizeWithFont

NSString *str = @"Test String"; 
CGSize size = [str sizeWithFont:label.font]; 

然后使用规模将它与label.frame.size比较

+0

厂像一个魅力,谢谢。 – ahjmorton

+0

欢迎您:) –

1

sizeWithFont在iOS的7弃用,所以你需要使用sizeWithAttributes

NSString *str = @"Test String"; 
NSDictionary *attributes = @{NSFontAttributeName: label.font}; 
CGSize size = [str sizeWithAttributes:attributes]; 
相关问题