gpt4 book ai didi

swift - UIView 子类 (Swift4) - 自定义方法 (setNumber) 未按预期工作

转载 作者:行者123 更新时间:2023-11-30 11:27:30 25 4
gpt4 key购买 nike

我创建了一个名为 UIBingoBall 的 UIView 子类来显示宾果球(见下文)。为了简洁起见,我省略了 init - 但该类按原样运行。我可以实例化它,设置 3 个属性“myimage、myLetterLabel 和 myNumberLabel”并查看正确的宾果球。

但是 - 我正在尝试向名为 .setNumber(ballNumber: Int) 的类添加一种方法,该方法将适本地更改球的 3 个属性 - 全部一步完成 - 但当我这样做时,它只能部分工作。它正在更改图像并正确显示图像。它正在更改标签文本 - 但没有显示它。如果我单独更改值(而不是调用函数),则一切正常。

有人可以快速浏览一下并告诉我我做错了什么吗?或者解释为什么行为不同?

这是我想使用的功能:

func setNumber(ballNumber: Int) {if ballNumber > 75 || ballNumber < 1 { return }

let fontModifier = frame.height / 70
let ballLetter:Int = (ballNumber - 1) / 15
self.myLetterLabel.text = bingoLetterArray[ballLetter]
self.myNumberLabel.text = "\(ballNumber)"
self.myimage.image = bingoBallImageArray[ballLetter]
self.myNumberLabel.font = UIFont.systemFont(ofSize: fontModifier / 24, weight: .heavy)
self.myLetterLabel.font = UIFont.systemFont(ofSize: fontModifier / 8, weight: .heavy)

}

以下是属性定义:

class UIBingoBall: UIView {

private let bingoBallImageArray: [UIImage] = [#imageLiteral(resourceName: "BingoBlankB"),#imageLiteral(resourceName: "BingoBlankI"),#imageLiteral(resourceName: "BingoBlankN"),#imageLiteral(resourceName: "BingoBlankG"),#imageLiteral(resourceName: "BingoBlankO")]
private let bingoLetterArray: [String] = ["B", "I", "N", "G", "O"]


var myimage: UIImageView = {
let image = UIImageView()
image.image = #imageLiteral(resourceName: "BlankBallB")
image.contentMode = .scaleAspectFill
image.backgroundColor = .clear
image.translatesAutoresizingMaskIntoConstraints = false
return image
}()

var myLetterLabel: UILabel = {
let label = UILabel()
label.text = "B"
label.textColor = .blue
label.backgroundColor = .clear
label.textAlignment = .center
label.numberOfLines = 1
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont.systemFont(ofSize: 8, weight: .bold)
return label
}()

var myNumberLabel: UILabel = {
let label = UILabel()
label.text = "1"
label.textColor = .blue
label.backgroundColor = .clear
label.textAlignment = .center
label.numberOfLines = 1
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont.systemFont(ofSize: 24, weight: .heavy)
return label
}()

并且,如果有必要,这里是我从 init 调用的 setupViews()。

func setupViews() {
addSubview(myimage)
addSubview(myLetterLabel)
addSubview(myNumberLabel)

myimage.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
myimage.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
myimage.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
myimage.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
myNumberLabel.centerYAnchor.constraint(equalTo: myimage.centerYAnchor, constant: 3).isActive = true
myNumberLabel.centerXAnchor.constraint(equalTo: myimage.centerXAnchor).isActive = true
myLetterLabel.topAnchor.constraint(equalTo: myNumberLabel.topAnchor,constant: -8).isActive = true
myLetterLabel.heightAnchor.constraint(equalToConstant: 10).isActive = true
myLetterLabel.centerXAnchor.constraint(equalTo: myimage.centerXAnchor).isActive = true

}

提前感谢任何能够(并且确实)尝试提供帮助的人!

最佳答案

我认为问题出在字体

let fontModifier = frame.height / 70

self.myNumberLabel.font = UIFont.systemFont(ofSize: fontModifier / 24, weight: .heavy)
self.myLetterLabel.font = UIFont.systemFont(ofSize: fontModifier / 8, weight: .heavy)

它的值可以设置为 0 ,框架高度应该是 70*24 = 1680 的倍数,为标签的字体大小提供 1,这肯定是不可预测的

关于swift - UIView 子类 (Swift4) - 自定义方法 (setNumber) 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50572837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com