UIButton titleLabel文本底部为其框架

时间:2018-04-16 08:06:50

标签: ios swift uibutton

如何将UIButton titleLabel文本显示在其框架的底部。目前我正在使用UIEdge上边距并设置负UIButton图像宽度,结果不如预期。我还想将图像中心放在框架上。 `

let leftButton: UIButton = {
            let button = UIButton()
            button.layer.borderWidth = 2
            button.layer.borderColor = UIColor.ublGray1().cgColor
            button.setImage(UIImage.init(named: leftButtonImage)?.withRenderingMode(.alwaysTemplate), for: .normal)
            button.tintColor = UIColor.cerulean()
            button.layer.masksToBounds = false
            button.layer.cornerRadius = 37
            button.translatesAutoresizingMaskIntoConstraints = false
            button.setTitle(leftButtonTitle, for: .normal)
            button.titleEdgeInsets = UIEdgeInsets(top: 110, left: -26, bottom: 0, right: 0)[![enter image description here][1]][1]
            button.imageEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
            button.setTitleColor(UIColor.charcoalGrey(), for: .normal)
            button.titleLabel?.font = UIFont.aspiraMedium(size: 16)
            return button
        }()

` Code Result

2 个答案:

答案 0 :(得分:0)

你可以尝试这样的事情。

enter image description here

enter image description here

答案 1 :(得分:0)

如何将“标题插入” - 底部 - 设置为负数?它有效。

但是如果你想让它坚持到框架的底部,它将无法工作,所以......怎么样:

bt.titleEdgeInsets.top = (bt.imageView?.safeAreaInsets.bottom)!

bt.titleEdgeInsets.top = (bt.imageView?.alignmentRectInsets.bottom)!