设置UITextField的内容

作者: HunterG | 来源:发表于2016-07-04 17:13 被阅读123次

设置UITextField的内容

1.使用KVC的方式设置placeholder的字体大小、颜色,比较简单的方式,但不是人人都会的

-(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 setValue:[UIColor colorWithHexString:@"#e3e3e3"] forKeyPath:@"_placeholderLabel.textColor"];

[_textFild setValue:[UIFont boldSystemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];

_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;

}

相关文章

网友评论

    本文标题:设置UITextField的内容

    本文链接:https://www.haomeiwen.com/subject/mxjxjttx.html