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
我已经在这方面留出了空间,但我不想那样做.因为它在某些情况下不是完全对齐的,我知道UILabel有一个属性可以设置对齐,但我不想为此编写代码.我只想从IB开始设置所有内容.
谢谢
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
我已经在这方面留出了空间,但我不想那样做.因为它在某些情况下不是完全对齐的,我知道UILabel有一个属性可以设置对齐,但我不想为此编写代码.我只想从IB开始设置所有内容.
谢谢
可以在情节提要中设置关键路径
将文本设置为您的多行标题,例如hello
⌥+↵
您需要按⌥+↵将文本移动到下一行.
然后添加密钥路径
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)个
如果想查看Xcode上的更改,需要执行以下操作:(记住,可以跳过这些步骤)
将UIButton子类化以使按钮可设计:
import UIKit
@IBDesignable class UIDesignableButton: UIButton {}
将此可设计子类指定给要修改的按钮:
If you want to write the code, then do the long process个
1.按钮创建IBOutlet
2.在viewDidLoad中写入代码
btn.titleLabel.textAlignment = .Center
btn.titleLabel.numberOfLines = 0
在较新版本的Xcode(我的版本是Xcode6.1)中,我们有属性属性title
Select Attributed
,然后 Select 文本并按下面的中心选项
P.S.文本不是多行的,因为我必须设置
btn.titleLabel.numberOfLines = 0