UITextField 设置占位符的三种形式

第一种,直接方法。

  // 设置光标颜色
  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];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容