UITextField占位符文本的多种字体大小

我有我的UITextField的占位符文本,其中一部分应该有更大的字体大小,而不是占位符文本的其余部分。 我可以使用属性的PROPPALHOLDER属性吗? 它似乎并不尊重我通过属性string添加的字体大小。

目前我正在尝试一种字体大小,但它似乎也不工作,

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

目前,我认为这是不可能的,与attributedPlaceHolder 。 但是通过setAttributedText可以做到以下几点

 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]; 

尝试这个

 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; 

请参阅我的答案,以获得更多的定制

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

tnylee的回答,我写了一个swift解决scheme,弥补了没有swift解决办法。

不同的字体

这是下面的代码:

  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