NSCharacterSet:如何添加“_”到 alphanumericCharacterSet 文本限制?

发布于 2024-08-31 10:49:18 字数 456 浏览 10 评论 0原文

构建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

万水千山粽是情ミ 2024-09-07 10:49:18

Objective-C

NSMutableCharacterSet *_alnum = [NSMutableCharacterSet characterSetWithCharactersInString:@"_"];
[_alnum formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]];

Swift

let _alnum = NSMutableCharacterSet(charactersIn: "_")
_alnum.formUnion(with: .alphanumerics)

Objective-C

NSMutableCharacterSet *_alnum = [NSMutableCharacterSet characterSetWithCharactersInString:@"_"];
[_alnum formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]];

Swift

let _alnum = NSMutableCharacterSet(charactersIn: "_")
_alnum.formUnion(with: .alphanumerics)
不必在意 2024-09-07 10:49:18

另一种方法是使其可变并添加它。

Objective-C

NSMutableCharacterSet *characterSet = [NSMutableCharacterSet alphanumericCharacterSet];
[characterSet addCharactersInString:@"_"];

Swift

let characterSet = NSMutableCharacterSet.alphanumeric()
characterSet.addCharacters(in: "_")

您可以通过以下方式验证它是否已添加(在 Playground 中):

characterSet.characterIsMember(UInt16(Character("^").unicodeScalars.first!.value)) // false
characterSet.characterIsMember(UInt16(Character("_").unicodeScalars.first!.value)) // true -- YAY!
characterSet.characterIsMember(UInt16(Character("`").unicodeScalars.first!.value)) // false

Another way would have been to make it mutable and add it.

Objective-C

NSMutableCharacterSet *characterSet = [NSMutableCharacterSet alphanumericCharacterSet];
[characterSet addCharactersInString:@"_"];

Swift

let characterSet = NSMutableCharacterSet.alphanumeric()
characterSet.addCharacters(in: "_")

You could verify it has been added (in a Playground) with:

characterSet.characterIsMember(UInt16(Character("^").unicodeScalars.first!.value)) // false
characterSet.characterIsMember(UInt16(Character("_").unicodeScalars.first!.value)) // true -- YAY!
characterSet.characterIsMember(UInt16(Character("`").unicodeScalars.first!.value)) // false
杀手六號 2024-09-07 10:49:18
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSCharacterSet *blockedCharacters = [[NSCharacterSet whitespaceCharacterSet] invertedSet];
    NSCharacterSet *blockedCharacters2 = [[NSCharacterSet letterCharacterSet] invertedSet];
    return ([string rangeOfCharacterFromSet:blockedCharacters].location == NSNotFound || [string rangeOfCharacterFromSet:blockedCharacters2].location);  

}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSCharacterSet *blockedCharacters = [[NSCharacterSet whitespaceCharacterSet] invertedSet];
    NSCharacterSet *blockedCharacters2 = [[NSCharacterSet letterCharacterSet] invertedSet];
    return ([string rangeOfCharacterFromSet:blockedCharacters].location == NSNotFound || [string rangeOfCharacterFromSet:blockedCharacters2].location);  

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文