设置UITextField的内容

设置UITextField的内容

1.使用KVC的方式设置placeholder的字体大小、颜色,比较简单的方式,但是iOS13已经抛弃了这种方式,下面用属性字符串的方式实现

-(UITextField *)textFild{

if (!_textFild) {

CGFloat x = CGRectGetMaxX(_nameLabel.frame) + 8;

_textFild = [[UITextField alloc]initWithFrame:CGRectMake(x, 0, kScreenWidth - x - 20, PlaceOrderCellH)];

_textFild.font = [UIFont systemFontOfSize:14];

_textFild.textColor = [UIColor colorWithHexString:@"#e3e3e3"];

_textFild.placeholder = @"请输入充值帐号";

// 设置placeholder的颜色和字体大小

_textFild.attributedPlaceholder = [[NSMutableAttributedString alloc] initWithString:@"请输入充值帐号" attributes:@{NSForegroundColorAttributeName:HexColor(@"#999999"),NSFontAttributeName:[UIFont boldSystemFontOfSize:14]}];
_textFild.tag = 18;

_textFild.returnKeyType = UIReturnKeyDone;

[_textFild addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

_textFild.delegate = self;

}

return _textFild;

}

  1. placehoder的位置

众所周知,UITextField是有leftView和rightView的,可以通过设置器左右视图来调整placehoder的位置,这里有个问题要注意leftViewMode一定要设置成UITextFieldViewModeAlways,否则,leftView设置就会失败

-(UITextField *)textFild{

if (!_textFild) {

CGFloat x = CGRectGetMaxX(_nameLabel.frame) + 8;

_textFild = [[UITextField alloc]initWithFrame:CGRectMake(x, 0, kScreenWidth - x - 20, PlaceOrderCellH)];

_textField.leftViewMode = UITextFieldViewModeAlways;

_textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 16, 48)];

_textFild.delegate = self;

}

return _textFild;

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

推荐阅读更多精彩内容