【问题标题】:UIButton title alignment and multiline supportUIButton 标题对齐和多行支持
【发布时间】:2010-10-31 13:46:52
【问题描述】:

如何将UIButton 的标题设置为左对齐,以及如何在UIButton 中显示多行文本?

【问题讨论】:

  • 看起来像 iPhone 开发。
  • Raju,你真的应该接受你的答案......这就是系统工作的原因。
  • UILineBreakModeWordWrap 运行良好,但 UIControlContentHorizo​​ntalAlignmentLeft 就像左中而不是像 UILabel 那样完全居中对齐
  • 接受以下答案之一。
  • 您应该接受以下答案之一。第二个有图表!

标签: iphone ios cocoa-touch uikit uibutton


【解决方案1】:

要设置UIButton 的对齐方式,请在 Interface Builder 的 Attributes Inspector 中查找该按钮。有一个部分叫做对齐。将其设置为左侧。或者对,或者任何你想要的。

要在代码中执行此操作,请使用 UIControl 的 contentHorizontalAlignment 属性。您可以将其设置为以下任何属性:

UIControlContentHorizontalAlignmentCenter
UIControlContentHorizontalAlignmentLeft
UIControlContentHorizontalAlignmentRight
UIControlContentHorizontalAlignmentFill

[myButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];

这些选项看起来都不是特别好(如您在上面看到的),使用UIButton 的contentEdgeInsets 属性重新定位内容可能会更幸运。

在UIButton、check this forum post 上设置多行标题,其中描述了更改按钮标签。

或者您可以使用此代码快速制作一个 2 行按钮:

myButton.titleLabel.textAlignment = UITextAlignmentCenter;
myButton.titleLabel.lineBreakMode = UILineBreakModeCharacterWrap;
[myButton setTitle:@"Siegfried\nRoy" forState:UIControlStateNormal];

Raju,别忘了将问题标记为已接受。

【讨论】:

  • 您没有得到足够的信息来标记您的一些问题已回答 ;-)
  • numberOfLines 适用于 iOS5,但仍需要为 iOS 4 设置 lineBreakMode。
  • 请注意,在设置标题时,使用指定 UIControlState 的消息很重要。如果您只是将其设置为属性,则标签不会针对新文本调整大小。见:stackoverflow.com/questions/4576331/…
  • @AndrewCr 为了清楚起见,我在上面的例子中使用了setTitle:forState。
  • 仅供参考:UITextAlignmentCenter 在 iOS 6 中已弃用,现在应该使用 NSTextAlignmentCenter。
【解决方案2】:

这也是在代码中进行 UIButton 对齐的代码:-

[myButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];

【讨论】:

    【解决方案3】:

    其实你可以把lineBreakMode设置成UILineBreakModeWordWrap或者UILineBreakModeCharacterWrap。如有必要,这会将文本分成多行。

    【讨论】:

      【解决方案4】:

      试试这个

          myButton.titleLabel.textAlignment = UITextAlignmentLeft;
          myButton.titleLabel.lineBreakMode = UILineBreakModeCharacterWrap;
          myButton.titleLabel.numberOfLines = 0;
      

      【讨论】:

        【解决方案5】:

        要添加段落效果,您可以更改标题的大小。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-07-27
          • 1970-01-01
          • 1970-01-01
          • 2017-10-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多