美文网首页
iOS-textField 的 placeholder 和 te

iOS-textField 的 placeholder 和 te

作者: 李丘 | 来源:发表于2021-09-02 15:02 被阅读0次

需求背景:textField 的 placeholder 要比 textField 输入状态下的文字要小

实现

要自定义 textField 的 placeholder,我们使用 attributedPlaceholder 来设置:

    NSString *placeholder = @"请输入文字";
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:placeholder];
    
    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, placeholder.length)];
    
    textFiled.attributedPlaceholder = attributedString;
    
    textFiled.font = [UIFont systemFontOfSize:28];

结果发现 attributedPlaceholder 字体大小并不是14,而是28,后面设置的 font 的大小覆盖了 attributedPlaceholder 的字体大小,由于我的编码习惯是先设置 placeholder,后设置font,所以出现了这个小坑。

解决

先设置font,再设置 placeholder

    textFiled.font = [UIFont systemFontOfSize:28];

    textFiled.attributedPlaceholder = attributedString;

相关文章

网友评论

      本文标题:iOS-textField 的 placeholder 和 te

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