简体   繁体   English

UIButton标题对齐和多行支持

[英]UIButton title alignment and multiline support

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

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

[myButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];

To set the alignment of a UIButton , look in the Attributes Inspector in Interface Builder for the button. 要设置UIButton的对齐UIButton ,请在Interface Builder中的“属性”检查器中查找按钮。 There's a section called Alignment. 有一个称为对齐的部分。 Set it to left. 将其设置为左侧。 Or right, or whatever you want. 或正确,或任何您想要的。

在此处输入图片说明

To do it in code, use the contentHorizontalAlignment property of UIControl . 为此,请使用UIControlcontentHorizontalAlignment属性。 You can set it to any of these properties: 您可以将其设置为以下任何属性:

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 contentEdgeInsets property of UIButton to reposition the content. 这些选项都没有一个看起来特别好(如您在上面看到的),使用UIButtoncontentEdgeInsets属性来重新定位内容可能会带来更多的运气。

To set a multiline title on a UIButton , check this forum post which 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];

在此处输入图片说明

Raju, don't forget to mark questions as accepted. Raju,别忘了将问题标记为已接受。

Actually, you can set the lineBreakMode to UILineBreakModeWordWrap or UILineBreakModeCharacterWrap . 实际上,您可以将lineBreakMode设置为UILineBreakModeWordWrapUILineBreakModeCharacterWrap This breaks the text into multiple lines if necessary. 如有必要,这会将文本分成多行。

Try this 尝试这个

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

To add a Paragraph effect you can change the size of title. 要添加段落效果,您可以更改标题的大小。

对于阿琳娜效应

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM