如何制作UIButton的文本对齐中心?使用IB [英] How to make UIButton's text alignment center? Using IB
问题描述
我无法使用IB作为中心来设置UIButton的标题。我的标题是多行的。
它是这样的一个
p>
但我想要这样一个
>
我已经给了空间,但我不想那样做。由于它没有完全对齐某些情况,我知道UILabel有一个属性来设置对齐但我不想为此编写代码..只想从IB设置所有内容。
谢谢
解决方案1
您可以在故事板中设置关键路径
将文本设置为多行标题,例如你好
⌥ + ↵ multiline
您需要按⌥ + ↵将文本移动到下一行。
然后添加关键路径
titleLabel.textAlignment
as Number
和value 1
, 1
表示 NSTextAlignmentCenter
titleLabel.numberOfLines
as Number
和value 0
, 0
表示任意行数
这不会反映在IB / Xcode上,但会在运行时位于中心(设备/模拟器)
如果你想要查看Xcode上的更改,您需要执行以下操作:(记住您可以跳过这些步骤)
-
UIButton的子类制作按钮可设计:
import UIKit
@IBDesignable class UIDesignableButton:UIButton {}
-
将此可设计的子类分配给您正在修改的按钮:
- Iff做得对,当Designables状态为最新(可能需要几秒钟)时,您将看到IB中的视觉更新:
解决方案2
如果要编写代码,请执行漫长的过程
1.Cre ate IBOutlet
for button
2.Write code in viewDidLoad
btn.titleLabel.textAlignment = .Center
btn.titleLabel.numberOfLines = 0
< hr>
Solution3
在较新的版本xcode(我的是xcode 6.1)我们有属性归属标题
选择归属
然后选择文本并按下中心选项
PS 该文字未出现多行,我必须设置
btn.titleLabel.numberOfLines = 0
I can't set the title of UIButton using IB as center. My title is multi line.
It is giving like this one
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.
Thanks
Solution1
You can set the key path in the storyboard
Set the text to your multiline title e.g. 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
This will not be reflected on IB/Xcode, but will be in centre at run time (device/simulator)
If you want to see the changes on Xcode you need to do the following: (remember you can skip these steps)
Subclass the UIButton to make the button designable:
import UIKit @IBDesignable class UIDesignableButton: UIButton {}
Assign this designable subclass to the buttons you're modifying:
- Iff done right, you will see the visual update in IB when the Designables state is "Up to date" (which can take several seconds):
Solution2
If you want to write the code, then do the long process
1.Create IBOutlet
for button
2.Write code in viewDidLoad
btn.titleLabel.textAlignment = .Center
btn.titleLabel.numberOfLines = 0
Solution3
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
P.S. The text was not coming multiline for that I have to set the
btn.titleLabel.numberOfLines = 0
这篇关于如何制作UIButton的文本对齐中心?使用IB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!