设置字符串/ .text的最大长度

时间:2012-04-15 19:24:06

标签: ios xcode string

我使用按钮在条形图中显示字母,现在这是我使用的代码

-(IBAction) clicked: (id)sender{
NSString *titleOfButton = [sender titleForState:UIControlStateNormal];
NSString *newLabelText = titleOfButton;
labelsText.text = [NSString stringWithFormat:@"%@%@", labelsText.text, newLabelText];

 //if ([newLabelText length] >= 5) newLabelText = [newLabelText substringToIndex:5];
 }

我得到的问题是我希望它能够输入5个字母最大是否有人可以告诉我该怎么做?

感谢

1 个答案:

答案 0 :(得分:2)

添加此行 -

if ([newLabelText length] >= 5) newLabelText = [newLabelText substringToIndex:5];

...或将其应用于您要截断的任何字符串。

查看Apple Docs以获取一些非常有用的NSString内容

相关问题