NSCharacterSet:如何添加“_”到 alphanumericCharacterSet 文本限制?
构建一个 NSCharacter 集来限制 UITextField 输入用户名。我希望用户也能够输入下划线(因此 [A-Za-z0-9_]),但 alphanumericCharacterSet 不包含它。有没有办法以简短的形式指定这样的范围?我看到 + (id)characterSetWithRange:(NSRange)aRange
,但我不太明白它是如何工作的。
我有一个简单的 UITextField 子类,我将字符集传递给它。该限制工作正常,不允许用户输入除字母数字之外的任何内容。只需要在这些津贴中添加“_”即可。
NSCharacterSet *characterSet = [NSCharacterSet alphanumericCharacterSet];
[textField setAllowed:characterSet];
[textField setFrame:frame];
Building an NSCharacter set to restrict a UITextField for entering user names. I want the user to be able to also enter an underscore (so [A-Za-z0-9_]) but alphanumericCharacterSet does not include it. Is there a way to specify a range like that in short form? I see + (id)characterSetWithRange:(NSRange)aRange
, but I'm not really understanding how that would work.
I've got a simple UITextField sub-class that I pass the character set to. The restriction works fine and doesn't allow the user to enter anything but alpha numeric. Just need to add the "_" to those allowances.
NSCharacterSet *characterSet = [NSCharacterSet alphanumericCharacterSet];
[textField setAllowed:characterSet];
[textField setFrame:frame];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Objective-C
Swift
Objective-C
Swift
另一种方法是使其可变并添加它。
Objective-C
Swift
您可以通过以下方式验证它是否已添加(在 Playground 中):
Another way would have been to make it mutable and add it.
Objective-C
Swift
You could verify it has been added (in a Playground) with: