iOS 自定义TextField字体颜色和显示位置,限制字体输入个数

来源:互联网 发布:ios常用多线程编程组件 编辑:程序博客网 时间:2024/06/06 20:49

项目中 肯定需要自定义TextField 来改变默认的字体颜色大小和显示位置

不废话直接上代码

设置字体颜色和大小

 [self.tfUserName setValue:[UIFont systemFontOfSize:font(13)] forKeyPath:@"_placeholderLabel.font"];  [self.tfUserName setValue:RGBColor(208,206,206) forKeyPath:@"_placeholderLabel.textColor"];

设置字体显示位置和placeholder位置 必须继承UiTextField实现自定义UiTextField

//控制placeHolder的位置-(CGRect)placeholderRectForBounds:(CGRect)bounds{    CGRect inset=CGRectMake(34, 1, 44, 14);    return inset;}//控制编辑文本的位置-(CGRect)editingRectForBounds:(CGRect)bounds{    CGRect inset = CGRectMake(bounds.origin.x +34, bounds.origin.y+1, bounds.size.width -10, bounds.size.height);    return inset; }//控制显示文本的位置-(CGRect)textRectForBounds:(CGRect)bounds{    CGRect inset = CGRectMake(bounds.origin.x +34, bounds.origin.y+1, bounds.size.width -10, bounds.size.height);    return inset;}

这里写图片描述

因为前面图片是放在uitextfield 上面 所以我把显示的x坐标 移动的比较多 没有图片的话 5就差不多了

限制textfield输入的长度

[self.loginView.tfUserName addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
//电话号码11位号码限制- (void)textFieldDidChange:(UITextField *)textField{    if (textField == self.loginView.tfUserName) {        if (textField.text.length >11) {            textField.text = [textField.text substringToIndex:11];        }    }}

好了,搞定了

阅读全文
0 0
原创粉丝点击