设置字符串/.text 的最大长度 [英] Setting Max length of string/.text

查看:34
本文介绍了设置字符串/.text 的最大长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在使用按钮在栏中显示字母这是我使用的代码

I use buttons to display letters in a bar now this is the code I use

-(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 个字母 有没有人可以告诉我怎么做?

The problem I got is that I want it to be able to put in 5 letters max Is there anyone who can tell me how to do this?

谢谢

推荐答案

添加这一行 -

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

...或者将相同的内容应用于您想要截断的任何字符串.

...or apply the same to whatever string you want to truncate.

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

Check out the Apple Docs for some really useful NSString stuff

这篇关于设置字符串/.text 的最大长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆