集中 UIButton 的标题,如 Swift 下的图像所示 [英] Centralise the title of UIButton as show in the Image Below Swift
本文介绍了集中 UIButton 的标题,如 Swift 下的图像所示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想像这样显示按钮标题
I want to show Button title like this
但是即使在标题集中之后我的输出也是这样
but my output is comming like this even after centralising the title
frmbtn 是按钮的出口,我为此使用的代码是
frmbtn is the outlet of button And the Code I am Using for this is
frmbtn.setTitle("From Station\nSTN\nSTATION NAME", forState: UIControlState.Normal)
frmbtn.titleLabel!.numberOfLines = 3
frmbtn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Center
我们可以在按钮标题中放置不同的文字大小吗?
And Can we put different text sizes in the button title ?
推荐答案
这里有你想要的全部.
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
}
}
这篇关于集中 UIButton 的标题,如 Swift 下的图像所示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文