我的UITextField有一个占位符文本,其中一个部分应该有更大的字体大小而不是占位符文本的其余部分。我可以使用attributionPlaceHolder属性吗?它似乎不尊重我通过属性字符串添加的字体大小。
目前我正在尝试一种字体大小,但它似乎也无法正常工作,
NSAttributedString *placeholder = [[NSAttributedString alloc] initWithString:placeholderText
attributes:@{NSFontAttributeName : [UIFont fontWithName:textField.font.fontName size:8.0]}];
textField.attributedPlaceholder = placeholder;
答案 0 :(得分:2)
试试这个
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;
答案 1 :(得分:1)
目前我认为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];
答案 2 :(得分:0)
NSAttributedString *placeholder = [[NSAttributedString alloc] initWithString:placeholderText
attributes:@{NSFontAttributeName : [UIFont fontWithName:textField.font.fontName size:8.0]}];
textField.allowsEditingTextAttributes = YES;
textField.attributedPlaceholder = placeholder;
答案 3 :(得分:0)
请参阅tnylee
的答案,我写了一个swift
解决方案,弥补没有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