美文网首页
IOS 小技巧:自定义 textField 的占位文本(plac

IOS 小技巧:自定义 textField 的占位文本(plac

作者: 阳光的影子v | 来源:发表于2016-06-29 12:27 被阅读137次
    • 方法一
      使用 KVC 方式自定义 textField 的占位文本.
      示例代码
        UITextField *tf = [[UITextField alloc] init];
        tf.placeholder = @“占位文本”;
    //textColor 和 textFont 为自定义的字体颜色字体尺寸
        [tf setValue:textColor forKeyPath:@"_placeholderLabel.textColor"];
        [tf setValue:[UIFont boldSystemFontOfSize:textFont] forKeyPath:@"_placeholderLabel.font"];
    
    • 方法二
      使用官方提供的 API 自定义 textFIeld

    @property(nullable, nonatomic,copy) NSAttributedString *attributedPlaceholder NS_AVAILABLE_IOS(6_0); // default is nil

    
      示例代码
    
    UITextField *tf = [[UITextField alloc] init];
    tf.placeholder = @"占位文本";
    NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
    dictM[NSForegroundColorAttributeName] = [UIColor redColor];
    dictM[NSFontAttributeName] = [UIFont systemFontOfSize:20];
    NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:tf.placeholder attributes:dictM];
    [tf setAttributedPlaceholder:attribute];
    

    相关文章

      网友评论

          本文标题:IOS 小技巧:自定义 textField 的占位文本(plac

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