iOS开发进阶篇

UITextField PlaceHolder居中显示问题

2016-12-13  本文已影响2264人  ToSimple

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

屏幕快照 2016-12-13 下午5.35.35.png

对于以上的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;
}```
上一篇 下一篇

猜你喜欢

热点阅读