集中 UIButton 的标题,如下图所示 Swift
Centralise the title of UIButton as show in the Image Below Swift
我想这样显示按钮标题
但即使在集中标题后我的输出还是这样
frmbtn 是按钮的出口,我为此使用的代码是
frmbtn.setTitle("From Station\nSTN\nSTATION NAME", forState: UIControlState.Normal)
frmbtn.titleLabel!.numberOfLines = 3
frmbtn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Center
我们可以在按钮标题中放置不同的文字大小吗?
你要的全在这里
import UIKit
class ViewController: UIViewController {
@IBOutlet var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
var str : NSMutableAttributedString = NSMutableAttributedString(string: "From station\nSTN\nStation Name")
str.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(20), range: NSRange(location: 13,length: 3))
button.setAttributedTitle(str, forState: UIControlState.Normal)
button.titleLabel!.lineBreakMode = .ByWordWrapping
button.titleLabel?.textAlignment = .Center
button.titleLabel?.adjustsFontSizeToFitWidth = true
}
}
您必须在 titleLabel
本身上设置文本对齐方式。您还可以更改字体大小作为 font
属性.
的一部分
scanButton.titleLabel!.numberOfLines = 3
scanButton.titleLabel!.textAlignment = NSTextAlignment.Center;
scanButton.titleLabel!.font = UIFont.systemFontOfSize(40);
要在 UIButton 上设置多行标题,check this forum post 描述更改按钮标签。
myButton.titleLabel.textAlignment = UITextAlignmentCenter;
myButton.titleLabel.lineBreakMode = UILineBreakModeCharacterWrap;
[myButton setTitle:@"From Station STN station name" forState:UIControlStateNormal];
在简单的方法中,您也可以像这样给 space:
frmbtn.setTitle("From Station\n STN\nSTATION NAME", forState: UIControlState.Normal) \give here space after \n and see the result
frmbtn.titleLabel!.numberOfLines = 3
frmbtn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Center
我想这样显示按钮标题
但即使在集中标题后我的输出还是这样
frmbtn.setTitle("From Station\nSTN\nSTATION NAME", forState: UIControlState.Normal)
frmbtn.titleLabel!.numberOfLines = 3
frmbtn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Center
我们可以在按钮标题中放置不同的文字大小吗?
你要的全在这里
import UIKit
class ViewController: UIViewController {
@IBOutlet var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
var str : NSMutableAttributedString = NSMutableAttributedString(string: "From station\nSTN\nStation Name")
str.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(20), range: NSRange(location: 13,length: 3))
button.setAttributedTitle(str, forState: UIControlState.Normal)
button.titleLabel!.lineBreakMode = .ByWordWrapping
button.titleLabel?.textAlignment = .Center
button.titleLabel?.adjustsFontSizeToFitWidth = true
}
}
您必须在 titleLabel
本身上设置文本对齐方式。您还可以更改字体大小作为 font
属性.
scanButton.titleLabel!.numberOfLines = 3
scanButton.titleLabel!.textAlignment = NSTextAlignment.Center;
scanButton.titleLabel!.font = UIFont.systemFontOfSize(40);
要在 UIButton 上设置多行标题,check this forum post 描述更改按钮标签。
myButton.titleLabel.textAlignment = UITextAlignmentCenter;
myButton.titleLabel.lineBreakMode = UILineBreakModeCharacterWrap;
[myButton setTitle:@"From Station STN station name" forState:UIControlStateNormal];
在简单的方法中,您也可以像这样给 space:
frmbtn.setTitle("From Station\n STN\nSTATION NAME", forState: UIControlState.Normal) \give here space after \n and see the result
frmbtn.titleLabel!.numberOfLines = 3
frmbtn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Center