【问题标题】:how to set different font for a UILabel which has text containing both chinese and english characters separately?如何为 UILabel 设置不同的字体,其中的文本分别包含中文和英文字符?
【发布时间】:2011-11-14 15:23:21
【问题描述】:

我有一个包含中英文字符的 UILabel,

现在我想设置一种中文字体和另一种英文字体,

如何做到这一点?

【问题讨论】:

    标签: iphone ios fonts uikit uilabel


    【解决方案1】:

    我不相信这是可能的。在UILabel 中设置的font 属性将应用于该UILabel 的text 属性中指定的整个字符串。

    【讨论】:

      【解决方案2】:

      通常一个标签只能有一种字体。尽管如此,如果您想为不同的语言显示不同的字体,则可以将不同的语言字符串保留在不同的标签中并按照您想要的方式排列它们。

      查看此内容以确定标签的大小。

      Resize UITableViewCell to UILabel's height dynamically

      这也很有帮助。

      How do I wrap text in a UITableViewCell without a custom cell

      【讨论】:

        【解决方案3】:

        您可能会对以下几件事感兴趣:

        OHAttributedLabel

        TTTAttributedLabel

        OHAttributedLabel 仍然能够处理混合字体、颜色、大小……

        【讨论】:

          【解决方案4】:

          我没有尝试过使用中文字体,但是您可以使用以下代码使用 NSMutableAttributedString 在标签上设置不同/多种字体和其他属性。 Foll 是我的代码:

           UIFont *ArialFont = [UIFont fontWithName:@"arial" size:18.0];
           NSDictionary *arialdict = [NSDictionary dictionaryWithObject: ArialFont forKey:NSFontAttributeName];    
           NSMutableAttributedString *AattrString = [[NSMutableAttributedString alloc] initWithString:title attributes: arialdict];
          
           UIFont *VerdanaFont = [UIFont fontWithName:@"verdana" size:12.0];
           NSDictionary *veradnadict = [NSDictionary dictionaryWithObject:VerdanaFont forKey:NSFontAttributeName];
           NSMutableAttributedString *VattrString = [[NSMutableAttributedString alloc]initWithString: newsDate attributes:veradnadict];    
           [VattrString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:(NSMakeRange(0, 15))];
          
           [AattrString appendAttributedString:VattrString];
          
          
           lblText.attributedText = AattrString;
          

          请注意,lblText 是 UILabel,作为文件所有者的出口。 可以继续添加他想要的任意数量的 NSMutableAttributedString。

          另外请注意,我在我的项目中添加了 verdana 和 arial 字体并为此添加了 plist。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2023-01-19
            • 2016-11-18
            • 1970-01-01
            • 2015-09-23
            • 2010-12-23
            • 1970-01-01
            相关资源
            最近更新 更多