【问题标题】:Example of NSAttributedString with two different font sizes?具有两种不同字体大小的 NSAttributedString 示例?
【发布时间】:2013-08-24 07:25:26
【问题描述】:

NSAttributedString 对我来说真的难以理解。

我想设置一个 UILabel 以具有不同大小的文本,我收集 NSAttributedString 是要走的路,但我无法获得有关此文档的任何地方。

如果有人能帮我举个具体的例子,我会很高兴的。

例如,假设我想要的文本是:

(in small letters:) "Presenting The Great..."
(in huge letters:) "HULK HOGAN!"

有人可以告诉我怎么做吗?甚至,一个简单明了的参考资料,我可以自己学习?我发誓我已经尝试通过文档,甚至通过 Stack Overflow 上的其他示例来理解这一点,但我就是不明白。

【问题讨论】:

    标签: ios nsattributedstring


    【解决方案1】:

    [更新] Swift 5 解决方案:

    let attrString = NSMutableAttributedString(string: "Presenting The Great...",
                                               attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18)]);
    
    attrString.append(NSMutableAttributedString(string: "HULK HOGAN!",
                                                attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 36)]));
    

    Swift 4 解决方案:

    let attrString = NSMutableAttributedString(string: "Presenting The Great...",
                                           attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 18)]);
    
    attrString.append(NSMutableAttributedString(string: "HULK HOGAN!",
                                            attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 36)]));
    

    【讨论】:

      【解决方案2】:

      Swift 3 解决方案

      另外,您可以使用append 函数而不是在 ObjC 或 Swift 中指定索引:

      let attrString = NSMutableAttributedString(string: "Presenting The Great...",
                                                 attributes: [ NSFontAttributeName: UIFont.systemFont(ofSize: 20) ])
      
      attrString.append(NSMutableAttributedString(string: "HULK HOGAN!",
                                                  attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 40) ]))
      

      【讨论】:

        【解决方案3】:

        你会做这样的事情……

        NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"Presenting the great... Hulk Hogan!"];
        [hogan addAttribute:NSFontAttributeName
                      value:[UIFont systemFontOfSize:20.0]
                      range:NSMakeRange(24, 11)];
        

        这会将最后两个单词设置为 20 磅文本;字符串的其余部分将使用默认值(我相信是 12 分)。设置文本大小可能令人困惑的是您必须同时设置字体大小——每个UIFont 对象都封装了这两个属性。

        【讨论】:

        • 查看 SWIFT 范围的答案:stackoverflow.com/a/27041376/1736679
        • 最好计算字符串的范围而不是手动输入。使用: NSRange hulkRange = [@"展示伟大的......绿巨人霍根!" rangeOfString: @"绿巨人霍根!"];
        【解决方案4】:

        如果您想以简单的方式进行操作,我使用了一个名为 OHAttributedLabel 的 git 存储库,它在 NSAttributedString 上提供了一个类别。它可以让您执行以下操作:

        NSMutableAttributedString *mystring = [[NSMutableAttributedString alloc] initWithString:@"My String"];
        [mystring setTextColor:[UIColor colorWithRGB:78 green:111 blue:32 alpha:1]];
        mystring.font = [UIFont systemFontOfSize:14];
        

        如果您不想使用第 3 方库,请查看 this link,获取有关如何开始使用属性字符串的体面教程。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-10-30
          • 2011-11-16
          • 1970-01-01
          • 2021-09-20
          • 2011-12-31
          • 1970-01-01
          相关资源
          最近更新 更多