【问题标题】:UIButton with two lines of text in the title (numberOfLines=2)标题中有两行文本的 UIButton (numberOfLines=2)
【发布时间】:2011-12-11 12:40:41
【问题描述】:

我正在尝试制作一个UIButton,它的titleLabel 中有两行文本。这是我正在使用的代码:

UIButton *titleButton = [[UIButton alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)];
titleButton.titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
[titleButton setTitle:@"This text is very long and should get truncated at the end of the second line" forState:UIControlStateNormal];
titleButton.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
titleButton.titleLabel.numberOfLines = 2;
[self addSubview:titleButton];

当我尝试这个时,文本只出现在一行上。似乎在UIButton.titleLabel 中实现多行文本的唯一方法是设置numberOfLines=0 并使用UILineBreakModeWordWrap。但这并不能保证文本正好是两行。

但是,使用普通的 UILabel 确实有效:

UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)];
titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
titleLabel.text = @"This text is very long and should get truncated at the end of the second line";
titleLabel.numberOfLines = 2;
titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
[self addSubview:titleLabel];

    

有谁知道如何使UIButton 使用两行代码?唯一的解决方案是创建一个单独的UILabel 来保存文本,并将其添加为按钮的子视图?

【问题讨论】:

  • 你见过这个吗? - stackoverflow.com/questions/604632/…
  • Viraj,是的,如果您设置numberOfLines=0 并使用UILineBreakModeWordWrap,您可以获得多行。这样做的问题是,如果文本太长,它可能会给出两行以上。我想要正好在第二行末尾带有省略号的两个单独的字符(如果文本太长)。
  • 哦,那我想添加子视图可能是唯一的方法了。

标签: objective-c ios uibutton uilabel


【解决方案1】:

您可以直接从 Storyboard 修改所需的值。 选择按钮,转到身份检查器并在“用户定义的运行时属性”部分添加以下键值对:

【讨论】:

    【解决方案2】:

    更新了最新 iOS 版本的答案

    由于这是公认的答案,因此在此处添加了@Sean 的答案:

    在按钮的 titleLabel 上设置这些属性。

    button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    button.titleLabel.numberOfLines = 2; // if you want unlimited number of lines put 0
    

    Swift 3 和 4:

    button.titleLabel?.lineBreakMode = .byWordWrapping
    button.titleLabel?.numberOfLines = 2 // if you want unlimited number of lines put 0
    

    旧版 iOS 的原始答案

    如果您想在 UIButton 顶部添加 2 行文本,您应该在其顶部添加一个 UIlabel,这正是这样做的。

    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)];
    titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
    titleLabel.text = @"This text is very long and should get truncated at the end of the second line";
    titleLabel.numberOfLines = 2;
    titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
    [myButton addSubview:titleLabel]; //add label to button instead.
    

    针对界面构建器解决方案进行了更新

    添加了@Borut Tomazin 的答案以获得更完整的答案。 由于@Borut Tomazin 的答案得到了改进,再次更新了这部分。

    您可以更轻松地完成此操作,无需任何代码。在 Interface Builder 中,将 UIButton 上的 Line Break 设置为 Word Wrap。比你可以插入多行标题。只需按Option + Return 键即可创建新行。您还需要将此添加到 Interface Builder 中的用户定义的运行时属性:

    titleLabel.textAlignment Number [1]
    

    【讨论】:

    • UILineBreakMode 已弃用,请使用 NSLineBreakMode 代替它
    • 这在最近的 iOS 版本中是不必要的。请参阅@sean 的回答。
    【解决方案3】:

    您可以更轻松地完成此操作,无需任何代码。在 Interface Builder 中,将 UIButton 上的 Line Break 设置为 Word Wrap。比你可以插入多行标题。只需按Option + Return 键即可创建新行。 您还需要将此添加到 Interface Builder 中的用户定义的运行时属性:

    titleLabel.textAlignment Number [1]
    

    就这么简单。希望对你有帮助...

    【讨论】:

    • 对于简单的情况,此解决方案比当前接受的解决方案更好(更少的对象、代码)。
    • 您设置的属性与以编程方式设置的属性完全相同,因此您的参数“更少对象”无效。代码可能更少。但它更具可读性。任你选;)
    • 哦,我想我不清楚。感谢您试图澄清。并不太关心它是在 IB vs Code 中完成的。更多的原因是您不需要在 Borut 的解决方案中添加额外的标签,并且它也完成了同样的事情。但是,是的,两者都很好用。 :)
    • 对于这样一项基本任务,需要在后台编写代码真的让人不知所措。
    • 您可以通过在 Interface Builder 中添加 titleLabel.textAlignment User Defined Runtime Attribute 并将其设置为 Number = 来实现 100% 无代码的相同目标1(NSTextAlignmentCenter 的值)。
    【解决方案4】:

    您不需要向 UIButton 添加 UILabel。这只是额外的对象和工作。

    在按钮的 titleLabel 上设置这些属性。

    button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    button.titleLabel.numberOfLines = 2;//if you want unlimited number of lines put 0
    

    斯威夫特:

    button.titleLabel!.lineBreakMode = NSLineBreakMode.ByWordWrapping
    button.titleLabel!.numberOfLines = 2//if you want unlimited number of lines put 0
    

    【讨论】:

    • 这是 2013 年的,截至今天,这是最先进的解决方案。
    • @Blip 不幸的是,您不能这样做,需要在代码中完成。可能值得向 Apple 提交请求此功能的错误报告。
    • @bpapa 是的,我同意,故事板应该更灵活。
    • 这对我有用,但是它破坏了按钮intrinsicContentSize 的默认行为,它仍然返回仅对应于一行文本的高度。我通过覆盖按钮的intrinsicContentSize 来修复它,我将高度设置为[self.titleLabel sizeThatFits(CGSizeMake(self.titleLabel.frame.size.width, CGFLOAT_MAX)].height
    • @WillVonUllrich 因为您只能在编辑时更改接受的答案。我将这些答案添加到已接受的答案中,这样它会帮助那些没有进一步了解接受答案的人。
    【解决方案5】:

    为了完全避免编辑代码的需要,从而避免子类化视图的需要,在 Xcode5 及更高版本中,您可以遵循 Borut Tomazin 的建议:

    在 Interface Builder (或故事板) 中将换行符设置为 自动换行。比你可以插入多行标题。刚打 Option + Return 键换行。

    然后,在 User Defined Runtime Attributes 中可以添加

    密钥路径:titleLabel.textAlignment
    类型:Number
    值:1

    注意:这可能并不完全“面向未来”,因为我们正在将 UITextAlignmentCenter 常量转换为其数值(并且该常量可能会随着新 iOS 版本的发布而改变),但在不久的将来。

    【讨论】:

      【解决方案6】:
       button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
      
      button.titleLabel.textAlignment = NSTextAlignmentCenter;
      
      [button setTitle: @"Line1\nLine2" forState: UIControlStateNormal];
      

      【讨论】:

      • 完美的一个!您不必在setTitle 中拆分NSString。 `WordWrapping` 正在为你做这个!
      • 对我来说,我必须在集合标题内拆分字符串。没有它就行不通
      猜你喜欢
      • 1970-01-01
      • 2015-08-21
      • 2011-10-05
      • 2011-06-13
      • 2019-01-20
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      • 1970-01-01
      相关资源
      最近更新 更多