iOS中如何設(shè)置UITextField placeholder 的顏色和距離

最近有一個(gè)需求: 需要改變UITextField 的 placeholder的顏色和字體大小,我找了一些方法 總結(jié)一下:

1.使用富文本 (attributedPlaceholder)

這種方法的缺點(diǎn)是:如果設(shè)置placeholder的字體比文字字體小的時(shí)候,placeholder的文字會(huì)偏上。

UITextField *tf2 = [[UITextField alloc] init];
tf2.frame = CGRectMake(75, 160, 200, 40);
tf2.borderStyle = UITextBorderStyleRoundedRect;
tf1.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"attributedPlaceholder" attributes:@{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:13]}];
[self.view addSubview:tf2];

2.KVC修改Placeholder

UITextField *tf3 = [[UITextField alloc] init];
tf3.frame = CGRectMake(75, 220, 200, 40);
tf3.borderStyle = UITextBorderStyleRoundedRect;
tf3.placeholder = @"KVC修改Placeholder";
[tf3 setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[tf3 setValue:[UIFont boldSystemFontOfSize:13] forKeyPath:@"_placeholderLabel.font"];
[self.view addSubview:tf3];

3.重寫drawPlaceholderInRect

  • JYTextField *tf4 = [[JYTextField alloc] init];
    tf4.frame = CGRectMake(75, 280, 200, 40);
    tf4.borderStyle = UITextBorderStyleRoundedRect;
    tf4.placeholder = @"重寫drawPlaceholderInRect";
    [self.view addSubview:tf4];
  • (void)drawPlaceholderInRect:(CGRect)rect
    {
    UIColor *placeholderColor = [UIColor redColor];//設(shè)置顏色
    [placeholderColor setFill];
    CGRect placeholderRect = CGRectMake(rect.origin.x, (rect.size.height- self.font.pointSize)/2, rect.size.width, self.font.pointSize);//設(shè)置距離
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineBreakMode = NSLineBreakByTruncatingTail;
    style.alignment = self.textAlignment;
    NSDictionary *attr = [NSDictionary dictionaryWithObjectsAndKeys:style,NSParagraphStyleAttributeName, [UIFont systemFontOfSize:13], NSFontAttributeName, placeholderColor, NSForegroundColorAttributeName, nil];
    [self.placeholder drawInRect:placeholderRect withAttributes:attr];
    }

4.UITextField 上添加一個(gè)label

通過 監(jiān)測UITextFiled 的狀態(tài),來判斷l(xiāng)abel 的顯示與隱藏。這個(gè)特別要注意通知的使用。

UITextField *tf5 = [[UITextField alloc] init];
self.tf5 = tf5;
tf5.delegate = self;
tf5.frame = CGRectMake(75, 340, 200, 40);
tf5.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:tf5];

UILabel *tf5Placeholder = [[UILabel alloc] init];
self.tf5Placeholder = tf5Placeholder;
tf5Placeholder.frame = CGRectMake(10, 0, 200, 40);
tf5Placeholder.textColor = [UIColor lightGrayColor];
tf5Placeholder.text = @"label替換";
tf5Placeholder.font = [UIFont systemFontOfSize:14];
[tf5 addSubview:tf5Placeholder];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFiledEditChanged:) name:UITextFieldTextDidChangeNotification object:nil];

-(void)textFiledEditChanged:(NSNotification *)obj{
if ([self.tf5.text length] >0) {
self.tf5Placeholder.hidden = YES;
}else {
self.tf5Placeholder.hidden = NO;
}
}

-- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}

最后附上Demo 地址稍后附上

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容