簡體   English   中英

UITextField占位符 - 具有不同顏色和字體的字符串?

[英]UITextField placeholder - String with different colors and Font?

使用不同的字體和不同的文本顏色放置持有者字符 這就像我的占位符文本的字符串。 我的密碼占位符應為​​灰色, (最少6個字符)應為淺灰色,字體較小,如下圖所示。 怎么做到這一點?

在此輸入圖像描述

在swift中使用哪個是相同的邏輯。 我們將在目標c中做到這一點

var myMutableStringTitle = NSMutableAttributedString()

let Name  = "Enter Title" // PlaceHolderText

myMutableStringTitle = NSMutableAttributedString(string:Name, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 10.0)!]) // Font
    myMutableStringTitle.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range:NSRange(location:0,length:5))    // Color
    myMutableStringTitle.addAttribute(NSFontAttributeName, value: UIFont(name: "Georgia", size: 36.0)!, range: NSRange(location: 0, length: 5))

textfield.attributedPlaceholder = myMutableStringTitle ;

您可以使用范圍屬性字符串來實現此目的。 這是一個示例代碼......

NSMutableAttributedString *attString =
    [[NSMutableAttributedString alloc]
     initWithString:@"Enter Name (minimum 6 char)"];

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

    [attString addAttribute: NSFontAttributeName
                      value: [UIFont boldSystemFontOfSize:16.0f]
                      range: NSMakeRange(0,10)];

    [attString addAttribute: NSForegroundColorAttributeName
                      value: [UIColor grayColor]
                      range: NSMakeRange(10 + 1,@"Enter Name (minimum 6 char)".length - 10 -1)];

    [attString addAttribute: NSFontAttributeName
                      value: [UIFont systemFontOfSize:12.0f]
                      range: NSMakeRange(10 + 1,@"Enter Name (minimum 6 char)".length - 10 -1)];



     textField.attributedPlaceholder = attString;

這里10是你輸入名稱的范圍密碼范圍是0.8。

輸出textField是 -

在此輸入圖像描述

希望這個能對您有所幫助..

I hope this is helped to u

  NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"Enter Name (minimum 6 char)"];
[hogan addAttribute:NSFontAttributeName
              value:[UIFont systemFontOfSize:12.0]
              range:NSMakeRange(11, 16)];


NSRange rangeOfUsername = [[hogan string] rangeOfString:@"Enter Name"];
if (rangeOfUsername.location != NSNotFound) {
    [hogan addAttribute:NSForegroundColorAttributeName
                  value:[UIColor redColor]
                  range:rangeOfUsername];
}


textX.attributedPlaceholder = hogan;

這是您需要的輸出

在此輸入圖像描述

SWIFT代碼。

let aString1 =  NSMutableAttributedString(string: "Password", attributes: [NSFontAttributeName: UIFont.boldSystemFontOfSize(18.0),NSForegroundColorAttributeName:UIColor.blackColor()])

let aString2 =  NSAttributedString(string: " (Min 6 chars)", attributes: [NSFontAttributeName: UIFont(name: "HelveticaNeue", size: 10.0)!,NSForegroundColorAttributeName:UIColor.grayColor()])

aString1.appendAttributedString(aString2)
textFeild!.attributedPlaceholder = aString1;

看到這個鏈接

iPhone UITextField - 更改占位符文本顏色

- (void) drawPlaceholderInRect:(CGRect)rect {
    [[UIColor blueColor] setFill];
    [[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]];
}

對於ios 7.0使用此功能

- (void) drawPlaceholderInRect:(CGRect)rect {
    [[UIColor blueColor] setFill];





    UIFont *font = [UIFont fontWithName:@"Palatino-Roman" size:14.0];

    NSDictionary *attrsDictionary =

    [NSDictionary dictionaryWithObjectsAndKeys:
     font, NSFontAttributeName,
     [NSNumber numberWithFloat:1.0], NSBaselineOffsetAttributeName, nil];


    [[self placeholder]  drawInRect:rect withAttributes:attrsDictionary];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM