ios,TextField
2019-10-09 本文已影响0人
若风_412d
Placeholder 颜色
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"请输入支付宝姓名" attributes:
@{NSForegroundColorAttributeName:KJHexStr(@"8D726B"),
NSFontAttributeName:_ALTF.font }];
_ALTF.attributedPlaceholder = attrString;
iOS 键盘限制只能数字和字母
<UITextFieldDelegate>
textField.keyboardType=UIKeyboardTypeASCIICapable;
#define ALPHANUM @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *set = [[NSCharacterSet characterSetWithCharactersInString:ALPHANUM] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:set] componentsJoinedByString:@""];
return [string isEqualToString:filtered];
}
我们的要求是,只有数值和字母切换(8个textField)

本来用这个就行了
_num1F.keyboardType = UIKeyboardTypeASCIICapable;//默认字母
但是要求默认弹出数字
改为
_num2F.keyboardType = UIKeyboardTypeNumbersAndPunctuation;//默认数字
但是不行,如果用户在浏览器中使用拼音,那么在项目中就变成了数字和拼音的切换了。
后来改了下面的就行了😊:
键盘类型切换时,会刷新默认值
_num1F.keyboardType = UIKeyboardTypeASCIICapable;//默认字母
_num2F.keyboardType = UIKeyboardTypeNumbersAndPunctuation;//默认数字
第一个改UIKeyboardTypeASCIICapable,以后的7个textfield都是UIKeyboardTypeNumbersAndPunctuation
然而问题没有解决,用户可能我操作还是会点到拼音
获取用户输入拼音时候的字母newText
// 有文字输入会触发
- (void)textFieldDidChange:(UITextField *)textField{
//拼音
UITextRange *selectedRange = [textField markedTextRange];
NSString * newText = [textField textInRange:selectedRange];
if(newText.length>0){
[textField becomeFirstResponder]; //不会跳下一个
return;
}
//输入字符只能一个
if (textField.text.length>=2) {
textField.text = @"";
[textField becomeFirstResponder];
return;
}
//输入汉子是为空
if ([textField.text isEqualToString:@""]) {
textField.text = @"";
[textField becomeFirstResponder];
return;
}
暂时告一段落