基本使用
-(void)createTextFiled {
UITextField *textView = [[UITextField alloc] initWithFrame:CGRectMake(LineX(30), LineX(175), kScreenWidth - LineX(60), LineX(20))];
textView.textColor = rgb(34, 34, 34);
textView.secureTextEntry = YES;
textView.textAlignment = NSTextAlignmentCenter;
textView.font = [UIFont fontWithName:@"STHeitiSC-Medium" size:24];
textView.keyboardType = UIKeyboardTypeNumberPad; // 设定键盘类型
textView.returnKeyType = UIReturnKeyDone;
textView.delegate = self;
[textView addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
[self.view addSubview:textView];
_userTextField = textView;
[textView becomeFirstResponder];
}
#pragma textFeild Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string {
if (textField.text.length >= 6 && ![string isEqualToString:@""]) { //添加这半行代码
return NO;
}
return YES;
}
- (void)textFieldDidChange:(UITextField*)textField {
_userTextField = textField;
NSDictionary *attrsDictionary =@{
NSFontAttributeName: _userTextField.font,
NSKernAttributeName:[NSNumber numberWithFloat:LineX(12)]//这里修改字符间距
};
_userTextField.attributedText=[[NSAttributedString alloc]initWithString:textField.text attributes:attrsDictionary];
NSLog(@"_userTextField===%@",textField.text);
}
产品需求
1
实现方法:uitextfiled 输入完成之后,系统定义文字是居左,如果要居中需要用runtime 获取其所有属性,修改其中label 的属性,可以实现文字居中
实现代码
#import <objc/runtime.h>
unsigned int outCount = 0;
Ivar *ivars = class_copyIvarList([UITextField class], &outCount);
//打印出所有成员变量名
for (int i = 0; i<outCount; i++) {
Ivar ivar = ivars[i];
NSLog(@"333===%s",ivar_getName(ivar));
}
//手动释放
free(ivars);
UILabel *label = [textView valueForKey:@"label"];
label.textAlignment = NSTextAlignmentCenter;