gpt4 book ai didi

objective-c - 如何强制 UITextField 仅支持小写字符?

转载 作者:行者123 更新时间:2023-12-04 03:13:23 27 4
gpt4 key购买 nike

我想覆盖按键事件以强制执行,即使按下大写键,结果输出仍然是小写。

最佳答案

这应该有效(您还应该设置 textfield.autocapitalizationType=UITextAutocapitalizationTypeNone)

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField==_usernameTextfield || textField==_emailTextfield) {
NSRange lowercaseCharRange;
lowercaseCharRange = [string rangeOfCharacterFromSet:[NSCharacterSet uppercaseLetterCharacterSet]];

if (lowercaseCharRange.location != NSNotFound) {
// some uppercase chars where found, need to replace

UITextPosition *beginning = textField.beginningOfDocument;
UITextPosition *start = [textField positionFromPosition:beginning offset:range.location];
// new cursor location after insert/paste/typing
NSInteger cursorOffset = [textField offsetFromPosition:beginning toPosition:start] + string.length;

// convert whole text to lowercase and update the textfield
textField.text = [textField.text stringByReplacingCharactersInRange:range
withString:[string lowercaseString]];

// now reposition the cursor
UITextPosition *newCursorPosition = [textField positionFromPosition:textField.beginningOfDocument offset:cursorOffset];
UITextRange *newSelectedRange = [textField textRangeFromPosition:newCursorPosition toPosition:newCursorPosition];
[textField setSelectedTextRange:newSelectedRange];
return NO;
}
}
return YES;
}

关于objective-c - 如何强制 UITextField 仅支持小写字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25902764/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com