【问题标题】:Programatically set frame size of labels inside UIButton以编程方式设置 UIButton 内标签的帧大小
【发布时间】:2017-02-16 08:41:08
【问题描述】:

我创建了一个自定义 UIButton。在按钮内我有 2 个标签,一个在另一个之上。顶部是标题,所以它大一点,底部小一点。 我的目标是让两个标签完全覆盖整个按钮,如下所示: 顶部标签将覆盖按钮的顶部 2/3 部分,底部将覆盖按钮的其余 1/3 部分。 我的目标离实现不远了,但我得到了奇怪的行为——标签有点偏离按钮,在某些情况下它们会消失(我可以点击按钮但看不到标签)。 这是我的自定义 UIButton 供参考,我希望这段代码对我的问题有所帮助:

class ButtonWithStats: UIButton {

var num: Int
var name: String
var nameLabel: UILabel?
var numLabel: UILabel?
required init?(coder aDecoder: NSCoder) {
   // fatalError("init(coder:) has not been implemented")
    self.num = 0
    self.name = ""

    self.numLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 0, height: 0)) //just to init the labels.
    self.nameLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
    self.numLabel?.textAlignment = .center
    self.nameLabel?.textAlignment = .center
    self.numLabel?.textColor = UIColor.purple
    self.nameLabel?.textColor = UIColor.black
    self.numLabel?.font = UIFont.init(name: "Helvetica", size: 11)
    self.nameLabel?.font = UIFont.init(name: "Helvetica", size: 13)
    super.init(coder: aDecoder)
}

func setButton(numInput: Int, nameInput: String){
    self.num = numInput
    self.name = nameInput
    self.setLabels()
}

private func setLabels(){

    numLabel?.text = String(self.num)
    nameLabel?.text = self.name
    let widthForName = self.frame.width
    let heightForName = self.frame.height * 2 / 3
    nameLabel?.center = self.center
    nameLabel?.frame = CGRect(origin: self.frame.origin, size: CGSize(width: widthForName, height: heightForName))
    let widthForNum = self.frame.width
    let heightForNum = self.frame.height * 1 / 3
    let yForNum = (self.frame.height * 2 / 3) //+ self.frame.origin.y
    numLabel?.frame = CGRect(x: self.frame.origin.x, y: yForNum, width: widthForNum, height: heightForNum)
    self.addSubview(nameLabel!)
    self.addSubview(numLabel!)
    print("@@@@@@@@@@@@@@@@@@@@@@@@@")
    print("Button \(self.nameLabel?.text) frame is: \(self.frame)")
    print("Label frame is: \(self.nameLabel?.frame)")
    print("Num frame is: \(self.numLabel?.frame)")
    print("@@@@@@@@@@@@@@@@@@@@@@@@@")
}
}

我的控制台打印出这些数据:

@@@@@@@@@@@@@@@@@@@@@@@@@
Button Optional("button one") frame is: (257.5, 0.0, 64.5, 33.0)
Label frame is: Optional((257.5, 0.0, 64.5, 22.0))
Num frame is: Optional((257.5, 22.0, 64.5, 11.0))
@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@
Button Optional("Button two") frame is: (129.0, 0.0, 64.0, 33.0)
Label frame is: Optional((129.0, 0.0, 64.0, 22.0))
Num frame is: Optional((129.0, 22.0, 64.0, 11.0))
@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@
Button Optional("Button three") frame is: (0.0, 0.0, 64.5, 33.0)
Label frame is: Optional((0.0, 0.0, 64.5, 22.0))
Num frame is: Optional((0.0, 22.0, 64.5, 11.0))
@@@@@@@@@@@@@@@@@@@@@@@@@

【问题讨论】:

    标签: swift uibutton frame


    【解决方案1】:

    与其添加2个标签,不如在UIButtonstextLabel上设置numberOfLines

    button.titleLabel?.numberOfLines = 2
    

    或者,添加一个垂直的UIStackView,将2个标签作为子视图,并设置stackView.frame = button.bounds

    let stackView = UIStackView(arrangedSubviews: [numLabel, nameLabel])
    stackView.frame = buttonView.bounds
    addSubview(stackView)
    

    您可以调整stackView.spacing / layoutMargins 等,以根据需要定位标签

    要使numLabel 的高度是nameLabel 的两倍,您可以使用设置约束...

    numLabel.heightAnchor.constraint(equalTo: nameLabel.heightAnchor, multiplier: 2)
    

    我建议您在故事板/xib 中执行此操作,但如果您需要以编程方式执行此操作,您可能会发现创建 xib 来试验 UIStackView 设置很有用。

    【讨论】:

    • 如何使用堆栈视图将大小设置为 2/3 和 1/3?
    • 现在所有的约束都是完美的,问题是按钮不可点击!知道为什么吗?
    • 好的,通过添加以下行来解决它:stackView.isUserInteractionEnabled = false 由于某种原因,stackView 启用了用户交互,因此它阻止了按钮交互。
    猜你喜欢
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多