欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

IOS ——UITextField PlaceHolder居中显示问题

程序员文章站 2022-03-11 17:35:35
...

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

IOS ——UITextField PlaceHolder居中显示问题

对于以上的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;
}