ios - 强制标签显示其文本

标签 ios swift uilabel uistackview

我想添加一个类似于 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/

相关文章:

ios - 在 ios 默认键盘的标签中使用表情符号

ios - 在不添加联系人的情况下使用 CNContactViewController (swift)

iOS Swift 在 CALayer 中渲染标签的问题

ios - UITextfield 有奇怪的效果

arrays - 从字典中获取值作为 Swift 2 中的数组

iphone - 在 iPhone 上的 UILabel 中查找特定字符的位置

ios - 自定义对象中的动态变量数组类型

iphone - 在 xcode iOS 中将事件添加到日历

swift - Swift 3 中的滑动手势

ios - 通过创建一个标签来放置多个标签的方法