Saf*_*ari 52 iphone uitextfield ios uistoryboard
我有一个问题iOS UIKeyboard.
我有一个UITextField,我想keyboard只有大写字符.
我使用了一个storyboard,我试着设置Cpitalization为" All characters" UITextField properties.
但这不能解决我的问题......有什么建议吗?
cod*_*cat 94
设置你的文本字段类型autocapitalizationType,以UITextAutocapitalizationTypeAllCharacters对的UITextField
self.yourTexField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
Run Code Online (Sandbox Code Playgroud)
致电委托后
//委托方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSRange lowercaseCharRange = [string rangeOfCharacterFromSet:[NSCharacterSet lowercaseLetterCharacterSet]];
if (lowercaseCharRange.location != NSNotFound) {
textField.text = [textField.text stringByReplacingCharactersInRange:range
withString:[string uppercaseString]];
return NO;
}
return YES;
}
Run Code Online (Sandbox Code Playgroud)
Tim*_*m S 18
对于那些寻找快速版本的人:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
textField.text = (textField.text! as NSString).replacingCharacters(in: range, with: string.uppercased())
return false
}
Run Code Online (Sandbox Code Playgroud)
我改变Capitalization: All Characters房产没有任何运气.
编辑:我想建议将此用于swift 4
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
textField.text = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string.uppercaseString)
return false
}
Run Code Online (Sandbox Code Playgroud)
Dan*_*Dan 17
我对上述一些答案的一个问题是,如果你尝试设置textfield.text,你将失去光标位置.因此,如果用户尝试编辑文本的中间部分,光标将跳转到结尾.
这是我的Swift解决方案,仍在使用UITextFieldDelegate:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if textField == textFieldToUppercase {
if string == "" {
// User presses backspace
textField.deleteBackward()
} else {
// User presses a key or pastes
textField.insertText(string.uppercaseString)
}
// Do not let specified text range to be changed
return false
}
return true
}
Run Code Online (Sandbox Code Playgroud)
Ede*_*den 15
语法现在
斯威夫特2
textField.autocapitalizationType = UITextAutocapitalizationType.AllCharacters
Run Code Online (Sandbox Code Playgroud)
斯威夫特3
textField.autocapitalizationType = .allCharacters
Run Code Online (Sandbox Code Playgroud)
小智 6
如果您只想查看键入的字符,无论大写/小写如何,都将大写/大写粘贴到 ViewDidLoad/ViewDidAppear 中的以下代码中
self.myTextField.autocapitalizationType = .allCharacters
当您自动输入时,上面的行将所有字母更改为大写字母
小智 6
你应该避免使用委托方法
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
Run Code Online (Sandbox Code Playgroud)
因为这会在 iOS 13 + QuickPath 输入(iOS 的 Swiftkey 键盘对应物)中触发不需要的行为。
如果您在键盘上滑动并输入“hello”,它会在文本字段中写入“HELLOHELLOHELLOHELLOHELLO”。这是因为该方法被多次调用,并且它通过textField.text = uppercasedValue.
正确的方法是观察.editingChange事件和大写然后是值。例如:
func awakeFromNib() {
textField.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
}
Run Code Online (Sandbox Code Playgroud)
和
@objc func textFieldDidChange() {
textField.text = textField.text?.uppercased()
}
Run Code Online (Sandbox Code Playgroud)
这是我使用的另一种方法,它可以执行以下操作:
首先,在文本字段中发生任何更改时,注册要更新的通知。
textField.addTarget(self, action: #selector(YourClassName.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged)
Run Code Online (Sandbox Code Playgroud)
然后实施textFieldDidChange。
func textFieldDidChange(textField: UITextField) {
textField.text = textField.text?.uppercaseString
}
Run Code Online (Sandbox Code Playgroud)
我选择这样做是为了避免用户看到某些大写字母的不均匀体验,但是一旦他们移到下一个角色,便会更改。
对于 SwiftUI,自动大写和键盘类型选择的语法是:
TextField("Your Placeholder", text: $emailAddress)
.keyboardType(.emailAddress)
.autocapitalization(.none)
Run Code Online (Sandbox Code Playgroud)
您可以使用以下选项进行自动大写:
.none //Specifies that there is no automatic text capitalization.
.words //Specifies automatic capitalization of the first letter of each word.
.sentences //Specifies automatic capitalization of the first letter of each sentence.
.allCharacters //Specifies automatic capitalization of all characters, such as for entry of two-character state abbreviations for the United States.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
38569 次 |
| 最近记录: |