美文网首页
UITextField 设置占位符的三种形式

UITextField 设置占位符的三种形式

作者: Dosun | 来源:发表于2017-09-25 18:16 被阅读76次

第一种,直接方法。

  // 设置光标颜色
  self.tintColor = [UIColor whiteColor];

//设置占位符的位置
 self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];

第二种重写 drawPlaceholderInRect 方法,可以指定占位符的相对位置。

-(void)drawPlaceholderInRect:(CGRect)rect{
    CGPoint point = CGPointMake(10, (rect.size.height - self.font.lineHeight)*0.5);
    
    [self.placeholder drawAtPoint:point withAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:self.font}];
}

第三种用 KVC

//得到每个私有属性的方法
Ivar *ivarList = class_copyIvarList([UITextField class], &count);
 for (int i = 0; i < count; i++) {
Ivar ivar = ivarList[i];
  NSLog(@"%s", ivar_getName(ivar));
}
free(ivarList);

static NSString * const XMGPlaceholderColorKey = @"placeholderLabel.textColor"

// 设置默认的占位文字颜色
[self setValue:[UIColor blueColor] forKeyPath:XMGPlaceholderColorKey];

相关文章

网友评论

      本文标题:UITextField 设置占位符的三种形式

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