【问题标题】:How to create a UILabel or UITextView with bold and normal text in it?如何创建带有粗体和普通文本的 UILabel 或 UITextView?
【发布时间】:2012-09-28 09:49:47
【问题描述】:

我正在尝试创建一个UILabelUITextView,其中包含粗体和普通文本。

我已经查看了属性字符串,但是当我在自定义单元格的标签中设置它时,它不显示任何文本。

我也使用了UITextView setContentToHTMLString: 方法,但它没有记录,应用程序被拒绝。

任何人都可以为此提供某种解决方案吗?

【问题讨论】:

  • 哇...我从来没有见过一个问题这么快变成“社区 wiki”(几分钟内修改了 9 次)!
  • 我还是不明白为什么这个问题会变成社区 wiki..

标签: iphone ios uilabel uitextview


【解决方案1】:

使用“NSAttributedString”在单个标签中设置多个字体文本并使用CATextLayer 呈现它:

只需#import "NSAttributedString+Attributes.h"

然后像这样实现它:

NSString *string1 = @"Hi";

NSString *string2 = @"How are you ?";

NSMutableAttributedString *attr1 = [NSMutableAttributedString attributedStringWithString:string1];

[attr1 setFont:[UIFont systemFontOfSize:20]];

NSMutableAttributedString *attr2 = [NSMutableAttributedString attributedStringWithString:string2]

[attr2 setFont:[UIFont boldSystemFontOfSize:20]];

[attr1 appendAttributedString:attr2]

CATextLayer *textLayer = [CATextLayer layer];

layer.string = attr1;

layer.contentsScale = [[UIScreen mainScreen] scale];

(Your_text_label).layer = textLayer;

OR (if you want to render on a view directly)

[(Your_View_Name).layer addSublayer:textLayer];

【讨论】:

    【解决方案2】:

    在 iOS 6.0 之前,您无法使用普通的 UILabel 或 UITextView 执行此操作,但您可以将 NSAttributedString 对象与一些可能的开源解决方案一起使用。

    喜欢TTAttributedLabelOHAttributedLabel

    iOS SDK 中内置的一个解决方案,you could also use a CATextLayer which has a string property that can be set to a NSAttributedString

    而且,就像下面的评论者所说,是的,您可以使用"attributedText" property 来做到这一点。万岁! (用于 Apple 倾听开发者经常重复的功能请求)

    【讨论】:

    • 请注意,UILabel 与 iOS 6 中的 NSAttributedString 兼容。
    • 你实际上可以使用普通的 UILabels 和 UITextView 从 iOS 6 开始使用 attributedText 属性来做到这一点
    • 我正在使用 iOS 5 及更高版本,它会在 iOS 5 中支持吗?
    • 不,但这就是我在回答中指出的其他潜在选择的原因。 :-)
    • @MichaelDautermann 非常感谢。我正在尝试,如果可行,我肯定会批准您的回答。 :)
    【解决方案3】:

    我知道这是一个旧线程,但这是我自己发现的。至少在 Xcode 版本 4.6.3 中,这可以通过使用属性 textView 来实现。更棒的是,一切都可以在Interface Builder中完成!

    步骤如下:

    将您的 textView 放在所需的位置 选择 textView 并打开 Utilities 面板下的 Attributes 选项卡 将 textView 文本更改为“attributed” 输入您想要的文字 现在,突出显示您想要加粗、下划线等的任何文本。 单击字体名称旁边的“T”按钮 在弹出窗口中,选择您想要的字体(例如:粗体) 您应该会在“实用工具”面板中看到所需的字体 尽情享受吧!

    【讨论】:

      【解决方案4】:

      以下代码适用于 iOS 6.0 及更高版本。结果是文本“This is bold”将变为粗体,而“This is not bold”。将是普通文本。

      if ([self.registrationLabel respondsToSelector:@selector(setAttributedText:)])
      {
          // iOS6 and above : Use NSAttributedStrings
          const CGFloat fontSize = 17;
          UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
          UIFont *regularFont = [UIFont systemFontOfSize:fontSize];
          //UIColor *foregroundColor = [UIColor clearColor];
      
          // Create the attributes
          NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
                                    boldFont, NSFontAttributeName, nil];
          NSDictionary *subAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
                                    regularFont, NSFontAttributeName, nil];
          const NSRange range = NSMakeRange(0,12); // range of " 2012/10/14 ". Ideally this should not be hardcoded
      
          // Create the attributed string (text + attributes)
          NSString *text = @"This is bold and this is not bold.;
          NSMutableAttributedString *attributedText =
          [[NSMutableAttributedString alloc] initWithString:text
                                                 attributes:subAttrs];
          [attributedText setAttributes:attrs range:range];
      
          // Set it in our UILabel and we are done!
          [self.registrationLabel setAttributedText:attributedText];
      }
      

      【讨论】:

        【解决方案5】:

        如果您在检查器中编辑属性文本时遇到问题,请将文本复制/粘贴到富文本编辑器中,进行调整,将 textView 文本选项切换为属性并粘贴。瓦拉。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-02-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-10-04
          • 1970-01-01
          相关资源
          最近更新 更多