【发布时间】:2019-07-11 02:30:35
【问题描述】:
我想在 Swift 中创建一个带有 titleLabel 的菱形 UIButton。我的问题是,titleLabel 文本缩小并且只显示三个点。如何扩展 titleLabel 的框架以获得足够的标题空间?
这是我的代码(宽度和高度为 70 磅)。
private let diamondButton: UIButton = {
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitleColor(.white, for: .normal)
button.setTitle("More", for: .normal)
button.backgroundColor = .red
button.layer.cornerRadius = 10
button.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 4))
button.titleLabel?.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / -4))
button.titleLabel?.textAlignment = .center
button.titleLabel?.backgroundColor = .blue // Just for demonstration
button.titleLabel?.bounds = button.frame
button.titleLabel?.layer.masksToBounds = true
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
return button
}()
【问题讨论】:
-
by
button.adjustsFontSizeToFitWidth = true你可以调整按钮内的字体大小,也许button.titleLabel?.clipsToBounds = false和button.minimumFontSize = 14可以解决你的问题 -
感谢您的回复阿拉什。不幸的是,它并没有解决问题。我试过:``` button.titleLabel?.clipsToBounds = true button.titleLabel?.adjustsFontSizeToFitWidth = true ```
-
如果你设置了
button.titleLabel?.clipsToBounds = false,你也应该设置button.clipsToBounds = false,并且为了调整字体大小你不需要titleLabel -
还是不行。我还尝试将 UILabel 作为子视图添加到按钮,但这不是一个优雅的解决方案。
-
问题是双旋转不是制作菱形按钮的正确方法。