UITextField占位符文本的多种字体大小 [英] Multiple font sizes for UITextField placeholder text
问题描述
我有一个占位符文本为我的UITextField,其中一部分应该有更大的字体大小,而不是占位符文本的其余部分。我可以使用attributPlaceHolder属性吗?它似乎不尊重我通过一个属性字符串添加的字体大小。
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.
目前我正在尝试一个字体大小,但似乎也不工作, / p>
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;
推荐答案
目前我不认为这是可能的 attributedPlaceHolder
。但是 setAttributedText
可以通过执行以下操作来实现:
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];
这篇关于UITextField占位符文本的多种字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!