gpt4 book ai didi

ios - 强制标签显示其文本

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

我想添加一个类似于 reddit 应用程序的投票功能。我认为 UIStackView 非常适合此目的。现在,我正在努力使投票赞成和投票反对按钮之间的标签显示其文本。

Fail

我尝试将 contentCompression 更改为 .fittingSizeLevel.defaultHigh 但这似乎没有任何改变。在您可以看到的图像上,有足够的空间来容纳整个文本,但事实并非如此。我缺少什么方面?

class VotingStackView: UIStackView {

let arrowUpButton: UIButton = {
let button = UIButton()
button.backgroundColor = .clear
button.tintColor = GSSettings.UI.Colors.tintColor
button.imageView?.contentMode = .scaleAspectFit
button.contentEdgeInsets = UIEdgeInsets(top: 16, left: 0, bottom: 16, right: 0)
button.clipsToBounds = true
return button
}()

let arrowDownButton: UIButton = {
let button = UIButton()
button.backgroundColor = .clear
button.tintColor = GSSettings.UI.Colors.tintColor
button.imageView?.contentMode = .scaleAspectFit
button.contentEdgeInsets = UIEdgeInsets(top: 16, left: 0, bottom: 16, right: 0)
button.clipsToBounds = true
return button
}()

let percentageLabel: UILabel = {
let label = UILabel()
label.text = "100%"
label.textColor = GSSettings.UI.Colors.regularTextColor
label.font = GSSettings.UI.Fonts.helveticaLight?.withSize(20)
label.clipsToBounds = false
label.setContentCompressionResistancePriority(UILayoutPriority.fittingSizeLevel, for: .horizontal)
return label
}()

var views: [UIView] = [UIView]()

//MARK: - Init & View Loading
override init(frame: CGRect) {
super.init(frame: frame)
views = [arrowUpButton, percentageLabel ,arrowDownButton]
setupStackView()
setupImages()
setupSubviews()
}

//MARK: - Setup
func setupStackView() {
self.axis = .horizontal
self.spacing = 0
self.alignment = .center
self.distribution = .fillEqually
}

func setupImages() {
let upImage = UIImage(named: "arrow_up")
let downImage = UIImage(named: "arrow_down")

arrowUpButton.setImage(upImage, for: .normal)
arrowDownButton.setImage(downImage, for: .normal)
}

func setupSubviews() {
for view in views {
addArrangedSubview(view)
}
layoutIfNeeded()
}

required init(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

votingStackView 是另一个 StackView 的一部分:

class BottomStackView: UIStackView {

let votingStackView: VotingStackView = {
let stackview = VotingStackView()
return stackview
}()

let addFriendButton: UIButton = {
let button = UIButton()
button.backgroundColor = .clear
button.tintColor = GSSettings.UI.Colors.tintColor
button.setImage(UIImage(named: "plus")?.withRenderingMode(.alwaysTemplate), for: .normal)
return button
}()

let redView: UIView = {
let view = UIView()
view.backgroundColor = .red
return view
}()

var views: [UIView] = [UIView]()

//MARK: - Init & View Loading
override init(frame: CGRect) {
super.init(frame: frame)
views = [votingStackView, addFriendButton, redView]
setupStackView()
setupSubviews()
}

//MARK: - Setup
func setupStackView() {
self.axis = .horizontal
self.spacing = 0
self.alignment = .leading
self.distribution = .fillEqually
}

func setupSubviews() {
for view in views {
addArrangedSubview(view)
}
layoutIfNeeded()
}

required init(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

最佳答案

改变

label.setContentCompressionResistancePriority(UILayoutPriority.fittingSizeLevel, for: .horizontal)

label.setContentCompressionResistancePriority(UILayoutPriority.defaultHigh, for: .horizontal)

来自docs似乎 fittingSizeLevel 没有考虑父级约束,因此使用 defaultHigh 似乎是更安全的选择。

关于ios - 强制标签显示其文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52706932/

26 4 0