【问题标题】:Multiple font sizes for UITextField placeholder textUITextField 占位符文本的多种字体大小
【发布时间】:2014-08-18 11:09:31
【问题描述】:

我的 UITextField 有一个占位符文本,其中一部分应该具有更大的字体,而不是占位符文本的其余部分。我可以使用 attributesPlaceHolder 属性来做到这一点吗?它似乎不尊重我通过属性字符串添加的字体大小。

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

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

【问题讨论】:

标签: ios objective-c cocoa-touch


【解决方案1】:

试试这个

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

【讨论】:

  • Nope 似乎不起作用,字体大小没有改变。
【解决方案2】:

目前我认为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];

【讨论】:

  • 一个问题是,我们可以在storyboard中设置不同的字体吗?
【解决方案3】:

参考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

【讨论】:

    【解决方案4】:
    NSAttributedString *placeholder =  [[NSAttributedString alloc] initWithString:placeholderText
                                                                       attributes:@{NSFontAttributeName : [UIFont fontWithName:textField.font.fontName size:8.0]}];
    
    textField.allowsEditingTextAttributes = YES;
    textField.attributedPlaceholder = placeholder;
    

    【讨论】:

      猜你喜欢
      • 2011-10-09
      • 2023-04-11
      • 1970-01-01
      • 2014-03-15
      • 1970-01-01
      • 2014-06-12
      • 1970-01-01
      • 2015-09-04
      • 2019-05-23
      相关资源
      最近更新 更多