iOS中的多个占位符文本颜色

时间:2014-07-14 05:13:58

标签: ios uitextfield

占位符中是否可以有多个彩色文字?

例如

  

写文字 - 我会在此前加上。

“写文字”将为黑色,而“我将在此前加上”。将是灰色的。

2 个答案:

答案 0 :(得分:0)

ios中的多个Textfiled占位符。

self.txt_First.attributedPlaceholder = [self String:@"Brewery (Sierra Nevada)" Range:7];

在上面的文本字段中,前七个字符为红色,其他字符为灰色。

使用以下方法设置多种颜色。

-(NSAttributedString *)String:(NSString *)str Range:(int )Rannge
{
    NSMutableAttributedString *attString =
    [[NSMutableAttributedString alloc]
     initWithString:str];

    [attString addAttribute: NSForegroundColorAttributeName
                      value: [UIColor redColor]
                      range: NSMakeRange(0,Rannge)];

    [attString addAttribute: NSForegroundColorAttributeName
                      value: [UIColor graycolor]
                      range: NSMakeRange(Rannge + 1,str.length - Rannge -1)];

    return attString;

}

多个占位符颜色输出:

enter image description here

答案 1 :(得分:0)

我有简单的方法。

UILabel下方添加UITextField并设置UITextField clearColor。并根据需要设置UILabel文字,并将该文字颜色设置为与占位符颜色相同

相关问题