我想制作自己的自定义按钮,其中有两个垂直堆叠的标签,如下所示:
顶部标签将是一个 FontAwesome 图标,使用 https://github.com/thii/FontAwesome.swift
我正在尝试这样做:
// Initialize buttonLabel
var buttonLabel: UILabel = UILabel()
buttonLabel.text = "New Event"
//Change initial Label to FontAwesome Image
addEventButton.titleLabel?.font = UIFont.fontAwesomeOfSize(30)
addEventButton.setTitle(String.fontAwesomeIconWithName(.CalendarO), forState: .Normal)
// Add label to button
addEventButton.addSubview(buttonLabel)
调用viewDidLoad
但是,只显示图标。我怎样才能做到这一点?
最佳答案
使用属性字符串,并在字符和字符串之间换行。 NSFontAttributeName 可用于为子字符串设置不同的字体。
UIButton 有一个 setAttributedTitle forState 方法。
关于iOS 一键两个标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31256164/