美文网首页iOS开发进阶篇
UITextField PlaceHolder居中显示问题

UITextField PlaceHolder居中显示问题

作者: ToSimple | 来源:发表于2016-12-13 17:58 被阅读2264次

    有时候,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;
    }```
    

    相关文章

      网友评论

      • 给你快乐:其实我发现只要设置文字对其方式 _textField.textAlignment=1;之后,占位符也会跟着居中显示
      • leeyyl:有个问题,焦点在UITextField的时候,placeholder文字并没有消失
        leeyyl:@ToSimple 确实这样以前没注意到:sweat_smile:
        ToSimple:@宜人悦己地活着 系统的TextField默认的占位符, 当焦点在的时候,也不消失啊, 当你真正输入的时候,就消失了,好像都是这个策略吧
      • 小锤子_:可以,,你要是把 style写出来就更容易懂了

      本文标题:UITextField PlaceHolder居中显示问题

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