Owa*_*unt 358 cocoa-touch uibutton uikit ios
我有以下代码......
UILabel *buttonLabel = [[UILabel alloc] initWithFrame:targetButton.bounds];
buttonLabel.text = @"Long text string";
[targetButton addSubview:buttonLabel];
[targetButton bringSubviewToFront:buttonLabel];
Run Code Online (Sandbox Code Playgroud)
...我的想法是我可以为按钮添加多行文本,但文本总是被UIButton的backgroundImage遮盖.用于显示按钮子视图的日志记录调用显示已添加UILabel,但无法看到文本本身.这是UIButton中的错误还是我做错了什么?
jes*_*rry 664
对于iOS 6及更高版本,请使用以下内容以允许多行:
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
// you probably want to center it
button.titleLabel.textAlignment = NSTextAlignmentCenter; // if you want to
[button setTitle: @"Line1\nLine2" forState: UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
对于iOS 5及更低版本,请使用以下内容以允许多行:
button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
// you probably want to center it
button.titleLabel.textAlignment = UITextAlignmentCenter;
[button setTitle: @"Line1\nLine2" forState: UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
2017年,iOS9前锋,
通常,只做这两件事:
Ada*_*ite 140
所选答案是正确的但如果您更喜欢在Interface Builder中执行此类操作,则可以执行以下操作:
NiK*_*KKi 53
对于IOS 6:
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.textAlignment = NSTextAlignmentCenter;
Run Code Online (Sandbox Code Playgroud)
如
UILineBreakModeWordWrap and UITextAlignmentCenter
Run Code Online (Sandbox Code Playgroud)
在IOS 6开始时已弃用..
bau*_*dot 30
重申Roger Nolan的建议,但使用明确的代码,这是一般解决方案:
button.titleLabel?.numberOfLines = 0
Run Code Online (Sandbox Code Playgroud)
Md.*_*san 21
button.titleLabel?.lineBreakMode = .byWordWrapping
button.titleLabel?.textAlignment = .center
button.setTitle("Button\nTitle",for: .normal)
Run Code Online (Sandbox Code Playgroud)
Ami*_*ndi 12
我遇到了自动布局的问题,启用多行后,结果是这样的:
因此titleLabel
大小不会影响按钮的大小
我添加Constraints
基于contentEdgeInsets
(在这种情况下contentEdgeInsets为(10,10,10,10)
之后调用makeMultiLineSupport()
:
希望它对你有帮助(swift 5.0):
extension UIButton {
func makeMultiLineSupport() {
guard let titleLabel = titleLabel else {
return
}
titleLabel.numberOfLines = 0
titleLabel.setContentHuggingPriority(.required, for: .vertical)
titleLabel.setContentHuggingPriority(.required, for: .horizontal)
addConstraints([
.init(item: titleLabel,
attribute: .top,
relatedBy: .greaterThanOrEqual,
toItem: self,
attribute: .top,
multiplier: 1.0,
constant: contentEdgeInsets.top),
.init(item: titleLabel,
attribute: .bottom,
relatedBy: .greaterThanOrEqual,
toItem: self,
attribute: .bottom,
multiplier: 1.0,
constant: contentEdgeInsets.bottom),
.init(item: titleLabel,
attribute: .left,
relatedBy: .greaterThanOrEqual,
toItem: self,
attribute: .left,
multiplier: 1.0,
constant: contentEdgeInsets.left),
.init(item: titleLabel,
attribute: .right,
relatedBy: .greaterThanOrEqual,
toItem: self,
attribute: .right,
multiplier: 1.0,
constant: contentEdgeInsets.right)
])
}
}
Run Code Online (Sandbox Code Playgroud)
Val*_*ora 11
有一个更简单的方法:
someButton.lineBreakMode = UILineBreakModeWordWrap;
Run Code Online (Sandbox Code Playgroud)
(针对iOS 3及更高版本编辑:)
someButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
Run Code Online (Sandbox Code Playgroud)
neo*_*eye 11
使用autolayout在iOS7上左对齐:
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.textAlignment = NSTextAlignmentLeft;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
Run Code Online (Sandbox Code Playgroud)
在Xcode 9.3中,你可以使用如下的故事板,
您需要将按钮标题textAlignment设置为居中
button.titleLabel?.textAlignment = .center
您不需要使用新行(\n)设置标题文本,如下所示,
button.setTitle("Good\nAnswer",for: .normal)
只需设置标题,
button.setTitle("Good Answer",for: .normal)
这是结果,
首先,你应该知道UIButton里面已经有了UILabel.您可以使用它进行设置–setTitle:forState:
.
您的示例的问题是您需要将UILabel的numberOfLines
属性设置为默认值1以外的值.您还应该查看lineBreakMode
属性.
Swift 5,用于 UIButton 中的多行文本
let button = UIButton()
button.titleLabel?.lineBreakMode = .byWordWrapping
button.titleLabel?.textAlignment = .center
button.titleLabel?.numberOfLines = 0 // for Multi line text
Run Code Online (Sandbox Code Playgroud)
要固定标题标签与按钮的间距,请在 setTitle之前设置 titleEdgeInsets 和其他属性:
let button = UIButton()
button.titleLabel?.lineBreakMode = .byWordWrapping
button.titleLabel?.numberOfLines = 0
button.titleEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 20, right: 20)
button.setTitle("Dummy button with long long long long long long long long title", for: .normal)
Run Code Online (Sandbox Code Playgroud)
PS 我测试过设置 titleLabel?.textAlignment 是不必要的,标题在.natural
.
归档时间: |
|
查看次数: |
173196 次 |
最近记录: |