ios - Swift iOS - 如何排列子层位置以匹配按钮文本的中心

标签 ios swift calayer centering

我有一个文本按钮。我添加了一个红色背景子图层,并使背景图层的宽度和高度大于按钮文本。我尝试使用以下方法将背景层居中放置在按钮上:

backgroundLayer.position = button.center

它不是居中的。这是我得到的:

enter image description here

我知道我可以直接在按钮上设置背景颜色和 cornerRadius,但是当我这样做时,红色背景会紧贴文本:

button.backgroundColor = UIColor.red
button.layer.cornerRadius = 10

enter image description here

我希望红色背景比文字更宽更高:

backgroundLayer.frame = CGRect(x: 0, y: 0, width: buttonTextSize.width + 10, height: buttonTextSize.height + 5

我想以 Photoshop 为例,但目前我面前没有 Photoshop。这是我能找到的最接近的。这是来自 Vimeo 的按钮。他们没有使用文本,但是 backgroundLayer 比按钮图像更宽更高,并且 backgroundLayer 的位置与按钮的 midX 和 midY 对齐:

enter image description here

如何让背景 subLayer 的位置与按钮文本的中心对齐?

let button: UIButton = {
    let button = UIButton()
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setTitle("Next", for: .normal)
    button.setTitleColor(UIColor.white, for: .normal)
    button.contentHorizontalAlignment = .center
    button.titleLabel?.font = UIFont.systemFont(ofSize: 23)
    return button
}()

let backgroundLayer: CALayer = {
    let layer = CALayer()
    layer.backgroundColor = UIColor.red.cgColor
    layer.cornerRadius = 10
    return layer
}()

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = UIColor.lightGray

    view.addSubview(button)
    button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    button.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    let buttonText = button.titleLabel?.text
    let buttonTextSize = (buttonText! as NSString).size(withAttributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 23.0)])

    // I added 10 points to the backgroundLayer's width and 5 points to the backgroundLayer's height so its wider then the text
    backgroundLayer.frame = CGRect(x: 0, y: 0, width: buttonTextSize.width + 10, height: buttonTextSize.height + 5)
    button.layer.insertSublayer(backgroundLayer, at: 0)

    backgroundLayer.position = button.center
}

最佳答案

这是一个看起来像你想要的样子的按钮(当然你可以调整任何不适合你感觉的参数):

enter image description here

此按钮自动为红色、圆角且比其文本大得多(即使按钮是使用自动布局定位时也是如此)。

下面是它是如何通过子类实现的:

class MyRedButton : UIButton {
    override func layoutSubviews() {
        super.layoutSubviews()
        self.backgroundColor = .red
        self.layer.cornerRadius = 10
    }
    override var intrinsicContentSize: CGSize {
        var sz = super.intrinsicContentSize
        sz.width += 30; sz.height += 30
        return sz
    }
}

关于ios - Swift iOS - 如何排列子层位置以匹配按钮文本的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50589983/

相关文章:

ios - Swift - 搜索 Controller 不断崩溃

ios - 模拟一个 "picture taken"的屏幕闪

ios - CALayer 在后台绘图

ios - 如何将子图层居中放置在另一图层上?

ios - 如何强制 parse.com 将对象添加为字符串而不是数组?

ios - UITextField 中的渐变文本

ios - 在iOS中听不到声音

objective-c - 如何访问 XIB 的所有者?

Swift 3 - 线程 1 : Signal SIGABRT Unrecognised selector sent to Instance

ios - Swift 3 核心数据 NSInternalInconsistencyException entityForName 在此模型中找不到名为 Foo 的实体