IOS ——UITextField PlaceHolder居中显示问题

来源:互联网 发布:程序员为什么工资高 编辑:程序博客网 时间:2024/06/15 05:49

有时候,UI妹子的设计稿并不按常规来,比如以下设计,让placeHolder居中显示,好在苹果API中提供了TextFieldattributedPlaceholder属性来解决问题



对于以上的UI效果其实用UITextField的属性attributedPlaceholder,并结合NSMutableParagraphStyle使用就可以是占位符居中显示.
具体代码如下

- (QDDLTextField *)phoneTextField{    if (_phoneTextField == nil) {        _phoneTextField = [[QDDLTextField alloc] initWithFrame:CGRectZero leftView:nil inset:KWFloat(30)];        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];        style.alignment = NSTextAlignmentCenter;        NSAttributedString *attri = [[NSAttributedString alloc] initWithString:@"请填写手机号" attributes:@{NSForegroundColorAttributeName:QDDCOLOR(163, 163, 163, 1),NSFontAttributeName:[UIFont qdd_fontOfSize:34], NSParagraphStyleAttributeName:style}];        _phoneTextField.attributedPlaceholder = attri;        _phoneTextField.layer.borderColor = QDDCOLOR(221, 221, 221, 1).CGColor;        _phoneTextField.layer.borderWidth = 1;        _phoneTextField.layer.cornerRadius = 2;        _phoneTextField.keyboardType = UIKeyboardTypePhonePad;    }    return _phoneTextField;}

原创粉丝点击