以编程方式设置 UIButton 内标签的框架大小
Programatically set frame size of labels inside UIButton
我有一个我创建的自定义 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))
@@@@@@@@@@@@@@@@@@@@@@@@@
与其添加 2 个标签,不如在 UIButtons
的 textLabel
上设置 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
设置很有用。
我有一个我创建的自定义 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))
@@@@@@@@@@@@@@@@@@@@@@@@@
与其添加 2 个标签,不如在 UIButtons
的 textLabel
上设置 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
设置很有用。