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

2016-06-29  本文已影响137人  阳光的影子v
    UITextField *tf = [[UITextField alloc] init];
    tf.placeholder = @“占位文本”;
//textColor 和 textFont 为自定义的字体颜色字体尺寸
    [tf setValue:textColor forKeyPath:@"_placeholderLabel.textColor"];
    [tf setValue:[UIFont boldSystemFontOfSize:textFont] forKeyPath:@"_placeholderLabel.font"];

@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];
上一篇下一篇

猜你喜欢

热点阅读