iOS开发之UITextfield的日常唠叨

依然,先看张图放松下

one.jpg

我们自以为知之甚多的事物的背后,无不潜伏着等量的未知因素。所谓理解,通常不过是误解的总合。by 村上春树

  • 下面总结,目前为止,我还记得起的误解总和!以后想起来的,一块总结在此!

1.设置UITextField的输入长度

UITextField *phoneTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 20, MainScreenWidth-30, 44)];
phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
phoneTextField.placeholder = @"输入手机号";
phoneTextField.textAlignment = NSTextAlignmentLeft;
phoneTextField.font = [UIFont systemFontOfSize:15];
self.phoneTextField = phoneTextField;
phoneTextField.delegate = self;
[self addSubview:phoneTextField];
[phoneTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

下面这个才是关键

- (void)textFieldDidChange:(UITextField *)textField
{
    if (textField == self.phoneTextField) {
        if (textField.text.length > 11) {
            textField.text = [textField.text substringToIndex:11];
        }
    }
}

设置的效果就是---限制输入11位,之后就按啥都不管用了

2.设置UITextField的输入模式,明文,还是密文

self.phoneTextField.secureTextEntry = NO;

3.设置UITextField的placeHolder,占位文字属性

//设置占位文字的颜色
[self.phoneTextField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
//设置输入文字的颜色
self.phoneTextField.textColor = [UIColor redColor];
//设置光标颜色,不多用
[self.phoneTextField setTintColor:[UIColor whiteColor]];

4.背景图片设置,也不是用的很多

//先要去除边框样式(类似UIButtonCustom)
self.phoneTextField.borderStyle = UITextBorderStyleNone;
self.phoneTextField .background = [UIImage imageNamed:@"phoneNum.png"];

5.设置再次编辑是否清空

self.phoneTextField .clearsOnBeginEditing = NO;

6.设置输入框内容的字体样式和大小

self.phoneTextField.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15]
;

7.textAlignment:内容对齐方式

//与UILabel一样,就不在详细写了
self.phoneTextField.textAlignment = UITextAlignmentLeft;

8.设置边框样式

self.phoneTextField.borderStyle = UITextBorderStyleRoundedRect;

//无任何边框
UITextBorderStyleNone ;
//黑色线框
UITextBorderStyleLine ;
//边线+阴影
UITextBorderStyleBezel ;
//浅色带圆弧线框
UITextBorderStyleRoundedRect;

9.设置编辑时,可全部删除,x号按钮

self.phoneTextField.clearButtonMode = UITextFieldViewModeAlways;

10.这个设置我一般不会用到,系统的太丑了

self.phoneTextField.returnKeyType =UIReturnKeyDone;
how do it.jpeg

需要设置这儿的自行百度吧,地址就不贴了

11 .自定义键盘属性(inputView)---我还没用到

//自定义键盘,将原有的键盘隐藏,显示当前自定义视图
UIView *keyBoardContent = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 100)];
keyBoardContent.backgroundColor = [UIColor darkGrayColor];
ettxf.inputView = keyBoardContent;
安安静静的看着就好.jpg

同上,以后用到再来总结,看看就好!

12.自定义辅助键盘视图(inputAccessoryView),这个用的还是挺多的,有时候需要在键盘上绑定一个完成按钮,在输入完成后,键盘回收

UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 80)];
subView.backgroundColor = [UIColor greenColor];
ettxf.inputAccessoryView = subView;
东西不算多.jpg

东西不算多,还有啥常用的,想起来,在添!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 摘要: IOS文本输入框的使用方法总结。 (1)---------------------------------...
    破夕_____________阅读 2,009评论 0 4
  • 摘要: IOS文本输入框的使用方法总结。 (1)---------------------------------...
    翻山越岭的另一边阅读 8,497评论 1 5
  • 尝尽了欢乐和痛苦之后 阅尽了真情和假意之后, 终于知道了自己的错误 错误已经不堪回首,错误已然痛心疾首 曾经的爱情...
    胡不度阅读 351评论 2 10
  • 在某些时候,人总是在不自量力的刷着自己存在感,你所在乎的那些人那些事,未必是他们所在乎的,就在这是就出现了分歧,在...
    墨洺祺妙阅读 453评论 0 1
  • 尽头有座桥 桥下面是片海 海底有礁石 石上又有珊瑚 那不是曼妙的 水生植物 那是一个又一个 堆积的尸骨 长眠于深海...
    青欢9527阅读 183评论 0 2