ios UIButton 标题对齐和多行支持

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/954589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:59:21  来源:igfitidea点击:

UIButton title alignment and multiline support

iphoneioscocoa-touchuikituibutton

提问by Raju

How do I set the title of a UIButtonto be left-aligned, and how can I show multiple lines of text in a UIButton?

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

回答by Ian Kershaw

Here's the code to do the UIButton alignment in code too: -

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

[myButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];

回答by nevan king

To set the alignment of a UIButton, look in the Attributes Inspector in Interface Builder for the button. There's a section called Alignment. Set it to left. Or right, or whatever you want.

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

enter image description here

在此处输入图片说明

To do it in code, use the contentHorizontalAlignmentproperty of UIControl. You can set it to any of these properties:

要做到这一点的代码,使用contentHorizontalAlignment的财产UIControl。您可以将其设置为以下任何属性:

UIControlContentHorizontalAlignmentCenter
UIControlContentHorizontalAlignmentLeft
UIControlContentHorizontalAlignmentRight
UIControlContentHorizontalAlignmentFill

UIControlContentHorizontalAlignmentCenter
UIControlContentHorizontalAlignmentLeft
UIControlContentHorizontalAlignmentRight
UIControlContentHorizontalAlignmentFill

[myButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];

None of these options look particularly good (as you can see above), and you might have might have more luck using the contentEdgeInsetsproperty of UIButtonto reposition the content.

这些选项中没有一个看起来特别好(如您在上面看到的),并且使用 的contentEdgeInsets属性UIButton重新定位内容可能会更幸运。

To set a multiline titleon a UIButton, check this forum postwhich describes changing the button label.

要设置多标题UIButton检查这个论坛的帖子这说明变更按钮标签。

Or you can use this code to quickly make a 2 line button:

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

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

enter image description here

在此处输入图片说明

Raju, don't forget to mark questions as accepted.

Raju,不要忘记将问题标记为已接受。

回答by Marc

Actually, you can set the lineBreakModeto UILineBreakModeWordWrapor UILineBreakModeCharacterWrap. This breaks the text into multiple lines if necessary.

实际上,您可以将 设置lineBreakModeUILineBreakModeWordWrapUILineBreakModeCharacterWrap。如有必要,这会将文本分成多行。

回答by rakeshNS

Try this

尝试这个

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

回答by Anthone

To add a Paragraph effect you can change the size of title.

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

For alinea effect

对于 alinea 效果