ios 如何让 UIButton 的文字居中对齐?使用IB
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5712937/
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
How to make UIButton's text alignment center? Using IB
提问by Inder Kumar Rathore
I can't set the title of UIButton using IB as center. My title is multi line.
It is giving like this one
我无法使用 IB 为中心设置 UIButton 的标题。我的标题是多行。
它是这样给予的
But I want like this one
但我想要这样的
I have given space in this but I don't want to do that. As it is not aligned exactly for some cases and I know there is a property of UILabel to set the alignment but I don't want to write a code for that.. just want to set everything from IB.
我在这方面给了空间,但我不想那样做。因为在某些情况下它没有完全对齐,我知道 UILabel 有一个属性来设置对齐,但我不想为此编写代码..只想从 IB 设置所有内容。
Thanks
谢谢
采纳答案by Inder Kumar Rathore
Solution1
解决方案1
You can set the key path in the storyboard
您可以在故事板中设置关键路径
Set the text to your multiline title e.g. hello
? + ?multiline
将文本设置为您的多行标题,例如hello
? +?multiline
You need to press ? + ?to move text to next line.
您需要按? +?将文本移动到下一行。
Then add the key path
然后添加关键路径
titleLabel.textAlignment
as Number
and value 1
, 1
means NSTextAlignmentCenter
titleLabel.numberOfLines
as Number
and value 0
, 0
means any number of lines
titleLabel.textAlignment
asNumber
和 value 1
,1
表示NSTextAlignmentCenter
titleLabel.numberOfLines
asNumber
和 value 0
,0
表示任意数量的行
This will not be reflected on IB/Xcode, but will be in centre at run time (device/simulator)
这不会反映在 IB/Xcode 上,但会在运行时(设备/模拟器)居中
If you want to see the changes on Xcode you need to do the following: (remember you can skip these steps)
如果您想查看 Xcode 上的更改,您需要执行以下操作:(请记住您可以跳过这些步骤)
Subclass the UIButton to make the button designable:
import UIKit @IBDesignable class UIDesignableButton: UIButton {}
Assign this designable subclass to the buttons you're modifying:
子类化 UIButton 以使按钮可设计:
import UIKit @IBDesignable class UIDesignableButton: UIButton {}
将此可设计的子类分配给您正在修改的按钮:
- Iff done right, you will see the visual update in IB when the Designables state is "Up to date" (which can take several seconds):
- 如果操作正确,当 Designables 状态为“最新”(可能需要几秒钟)时,您将在 IB 中看到视觉更新:
Solution2
解决方案2
If you want to write the code, then do the long process
如果你想写代码,那么做漫长的过程
1.Create IBOutlet
for button
2.Write code in viewDidLoad
1.IBOutlet
为按钮创建 2.
在viewDidLoad中编写代码
btn.titleLabel.textAlignment = .Center
btn.titleLabel.numberOfLines = 0
Solution3
解决方案3
In newer version of xcode (mine is xcode 6.1) we have property attributed title
Select Attributed
then select the text and press centre option below
在较新版本的 xcode(我的是 xcode 6.1)中,我们有属性属性标题
选择Attributed
然后选择文本并按下面的中心选项
P.S.The text was not coming multiline for that I have to set the
PS文本不是多行的,因为我必须设置
btn.titleLabel.numberOfLines = 0
btn.titleLabel.numberOfLines = 0
回答by RGML
This will make exactly what you were expecting:
这将完全符合您的期望:
Objective-C:
目标-C:
[myButton.titleLabel setTextAlignment:UITextAlignmentCenter];
For iOS 6 or higher it's
对于 iOS 6 或更高版本
[myButton.titleLabel setTextAlignment: NSTextAlignmentCenter];
as explained in tyler53's answer
Swift:
迅速:
myButton.titleLabel?.textAlignment = NSTextAlignment.Center
Swift 4.x and above
Swift 4.x 及更高版本
myButton.titleLabel?.textAlignment = .center
回答by Joetjah
Use the line:
使用该行:
myButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
This should center the content (horizontally).
这应该使内容居中(水平)。
And if you want to set the text inside the label to the center as well, use:
如果您还想将标签内的文本设置为居中,请使用:
[labelOne setTextAlignment:UITextAlignmentCenter];
[labelOne setTextAlignment:UITextAlignmentCenter];
If you want to use IB, I've got a small example here which is linked in XCode 4 but should provide enough detail (also mind, on top of that properties screen it shows the property tab. You can find the same tabs in XCode 3.x):
如果你想使用 IB,我这里有一个小例子,它在 XCode 4 中链接,但应该提供足够的细节(还请注意,在该属性屏幕的顶部,它显示了属性选项卡。您可以在 XCode 中找到相同的选项卡3.x):
回答by Sirji
For UIButton you should use:-
对于 UIButton,您应该使用:-
[btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
回答by Eduardo Fiorezi
For ios 8 and Swift
对于 ios 8 和 Swift
btn.titleLabel.textAlignment = NSTextAlignment.Center
btn.titleLabel.textAlignment = NSTextAlignment.Center
or
或者
btn.titleLabel.textAlignment = .Center
btn.titleLabel.textAlignment = .Center
回答by tyler53
For those of you who are now using iOS 6 or higher, UITextAlignmentCenter has been deprecated. It is now NSTextAlignmentCenter
对于现在使用 iOS 6 或更高版本的用户,UITextAlignmentCenter 已被弃用。现在是 NSTextAlignmentCenter
EXAMPLE: mylabel.textAlignment = NSTextAlignmentCenter;
Works perfectly.
示例:mylabel.textAlignment = NSTextAlignmentCenter;
完美运行。
回答by Shan Ye
For swift 4, xcode 9
对于 swift 4,xcode 9
myButton.contentHorizontalAlignment = .center
回答by user1862723
Assuming that btn refers to a UIButton, to change a multi-line caption to be centered horizontally, you can use the following statement in iOS 6 or later:
假设 btn 指的是一个 UIButton,要将多行标题改为水平居中,在 iOS 6 或更高版本中可以使用以下语句:
self.btn.titleLabel.textAlignment = NSTextAlignmentCenter;
回答by Morten Gustafsson
For Swift 3.0
对于 Swift 3.0
btn.titleLabel?.textAlignment = .center
回答by Grzegorz R. Kulesza
For Swift 4:
对于 Swift 4:
@IBAction func myButton(sender: AnyObject) {
sender.titleLabel??.textAlignment = NSTextAlignment.center
sender.setTitle("Some centered String", for:UIControlState.normal)
}