UIKit

UIKit - UITextField

2020-04-20  本文已影响0人  ienos

属性

属性名 作用
borderStyle UITextBorderStyleLine : 矩形,黑色边框,透明背景
UITextBorderStyleBezel : 和上面类似,但是是灰色的边框,背景透明
UITextBorderStyleRoundedRect : 圆角矩形,背景是白色,不再是透明
backgroundColor 设置背景颜色,会覆盖上面圆角矩形默认的白色背景
placeholder 设置提示(默认)文字
secureTextEntry 设置密文输入,就是和输入密码时类似的显示为小圆点
keyboardType 设置键盘样式
keyboardAppearance 设置键盘外观
inputView 设置弹出视图,inputView 即弹出的不是键盘而是这个视图
backgroundColor 设置背景颜色,会覆盖上面圆角矩形默认的白色背景 ; 设置的 frame 时,只有高度有用,其他 xy 和宽都是无效的,宽是默认的整个键盘宽度
leftView 设置左视图,就是用户名和密码,有时候放个图片的位置
leftViewMode (默认) UITextFieldViewModeNever : 不显示
UITextFieldViewModeAlways : 显示
UITextFieldViewModeUnlessEditing : 除编辑外出现
UITextFieldViewModeWhileEditing : 编辑出现
rightView 同左视图
rightViewMode 同左视图
clearButtonMode rightViewModeleftViewMode,注意与右视图冲突
clearsOnBeginEditing 再次编辑时是否清空内容
clearsOnInsertion 是否允许再次编辑时在内容中间插入内容
contentVerticalAlignment 内容纵向对齐方式,默认是居中
contentHorizontalAlignment 内容横向对齐
textAlignment 置文字对齐方式
adjustsFontSizeToFitWidth 设置调整文字大小以适配宽度(即输入不下时缩小文字,实在缩小不了了,就向后滚动),默认是向右滚动的
minimumFontSize 设置最小字号,和上面有关,即小于这个字号的时候,我就不缩小了,直接向右滚动
autocapitalizationType 设置字母大小样式
UITextAutocapitalizationTypeAllCharacters : 所有字母大写(用键盘输入的话发现失效,需要用软键盘输入才有效,以下同理
UITextAutocapitalizationTypeWords : 单词首字母大写
UITextAutocapitalizationTypeSentences : 句首字母大写

编辑光标太靠左

textfieldleft view 添加一个 viewxy 无效,x 都是0,而 y 是根据高度来自动调整的。即高度如果超过textField 则默认是 textField 高,如小于 textField 高度,则上下居中显示。唯一有效的就是宽度

    UIView *textfieldleftview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, space/2, btnHeight)];
    self.bottomtextfield.leftView = textfieldleftview;
    self.bottomtextfield.leftViewMode = UITextFieldViewModeAlways;

在 Cell 中的边编辑边自适应

设置 TextView 不能滚动,在代理方法里面实现,然后再用 TableView 的高度协议方法自适应

- (void)textViewDidChange:(UITextView *)textView
{
    [textView sizeThatFits:textView.size];
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}

监听输入

因在 textField 的代理方法中没有 DidChange 的方法 (textView 有),所以需要以下方式实现:

[textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];  
- (void)textFieldDidChange :(UITextField *)theTextField{  
    NSLog( @"text changed: %@", theTextField.text);  
}  

设置光标的颜色

[[UITextField appearance] setTintColor:kColorBrandGreen];//设置UITextField的光标颜色

文本内间距设置

// 占位文字与输入框前距离
- (CGRect)textRectForBounds:(CGRect)bounds{
    return CGRectInset(bounds, dx, dy);
}
// 光标的距离设置
- (CGRect)editingRectForBounds:(CGRect)bounds{
    return CGRectInset(bounds, dx, dy);
}

修改占位字体颜色、字体大小

[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];

iOS 13 之后,上面私有变量不能用,可以使用 attributedPlaceholder

iOS 10 编辑中文时下移

创建 BaseTextField ,重写下面两个方法

- (CGRect)textRectForBounds:(CGRect)bounds {
    return CGRectInset(bounds, 2, 1);
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    return CGRectInset(bounds, 2, 1);
}

iOS 11 内存泄漏

_provider 私有变量的循环引用,创建 BaseTextField ,重写下面两个方法

// 解决 iOS 11 内存泄漏
- (void)didMoveToWindow {
    [super didMoveToWindow];
    if (@available(iOS 11.2, *)) {
        NSString *keyPath = @"textContentView.provider";
        @try {
            if (self.window) {
                id provider = [self valueForKeyPath:keyPath];
                if (!provider && self) {
                    [self setValue:self forKeyPath:keyPath];
                }
            } else {
                [self setValue:nil forKeyPath:keyPath];
            }
        } @catch (NSException *exception) {
            NSLog(@"%@", exception);
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读