1
votes

I have a placeholder text for my UITextField which one part should have larger font size rather than the rest of the placeholder text. Can I do this using attributedPlaceHolder property ? It does not seems to respect the font size I am adding through a attributed string.

Currently I am trying one font size , but it does not seems to work either ,

 NSAttributedString *placeholder =  [[NSAttributedString alloc] initWithString:placeholderText
                                                                               attributes:@{NSFontAttributeName : [UIFont fontWithName:textField.font.fontName size:8.0]}];
 textField.attributedPlaceholder = placeholder;
4
Show us your code how you build that attributed string.dibi

4 Answers

2
votes

Try this

UITextField *yourTextField = setHere;

//1. Create Attributed text from your text field placeholder
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:yourTextField.placeholder];

//2. Change Font size only
[attributedText addAttribute:NSFontAttributeName
                       value:[UIFont fontWithName:yourTextField.font.fontName size:8.0]
                       range:NSMakeRange(0, yourTextField.placeholder.length)];

//3. set the attributed placeholder to be shown
yourTextField.attributedPlaceholder = attributedText;

Refer my Answer for more customization like this

1
votes

Currently I don't think this is possible with attributedPlaceHolder. But with setAttributedText it is possible by doing the following

UIFont *futuraFont = [UIFont fontWithName:@"Futura" size:18.0];
NSDictionary *futuraDictionary = [NSDictionary dictionaryWithObject: futuraFont forKey:NSFontAttributeName];
NSMutableAttributedString *fAttrString = [[NSMutableAttributedString alloc] initWithString:title attributes: futuraDictionary];

UIFont *menloFont = [UIFont fontWithName:@"Menlo" size:12.0];
NSDictionary *menloDictionary = [NSDictionary dictionaryWithObject:menloFont forKey:NSFontAttributeName];
NSMutableAttributedString *mAttrString = [[NSMutableAttributedString alloc]initWithString:title2 attributes:menloDictionary];
[mAttrString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:(NSMakeRange(0, [title2 length]))];

[fAttrString appendAttributedString:mAttrString];

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 200, 320, 100)];
[textField setAllowsEditingTextAttributes:YES];
[textField setAttributedText:fAttrString];
[self.view addSubview:textField];
1
votes

Refer to tnylee's answer, I written a swift solution, make up there is no swift solution.

The different font

Here is the code below:

    let title1 = "How to speak English?"
    let title2 = "(plese be careful)"

    let futuraFont:UIFont = UIFont.init(name: "Futura", size: 16)!

    let futuraDictionary:NSDictionary = NSDictionary.init(object: futuraFont, forKey: NSFontAttributeName as NSCopying)

    let menloFont:UIFont = UIFont.init(name: "Menlo", size: 12)!

    let menloDictionary:NSDictionary = NSDictionary.init(object: menloFont, forKey: NSFontAttributeName as NSCopying)

    let mAttrString:NSMutableAttributedString = NSMutableAttributedString.init(string: title2, attributes: (menloDictionary as! [String : Any]))

    let fAttrString:NSMutableAttributedString = NSMutableAttributedString.init(string: title1, attributes: (futuraDictionary as![String : Any]) )

    fAttrString.append(mAttrString)

    textField.attributedPlaceholder = fAttrString
0
votes
NSAttributedString *placeholder =  [[NSAttributedString alloc] initWithString:placeholderText
                                                                   attributes:@{NSFontAttributeName : [UIFont fontWithName:textField.font.fontName size:8.0]}];

textField.allowsEditingTextAttributes = YES;
textField.attributedPlaceholder = placeholder;