UIButton 框架不会随着使用 Swift 5 的可访问性大字体而增加 [英] UIButton frame does not increase with accessibility large font using Swift 5

查看:23
本文介绍了UIButton 框架不会随着使用 Swift 5 的可访问性大字体而增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UIButton 标题(.body 或.headline)的大无障碍字体大小不会增加按钮的框架,而只会增加标题文本.在下面的屏幕截图中可以清楚地看到:

Large accessibility font size for UIButton title (.body or .headline) does not increase the frame of button but only increase the title text. It can clearly be seen in the screenshot below:

约束只有顶部、前导和尾部,也在代码中我添加了两行:

Constraints are only top, leading and trailing, also in code I have added 2 lines:

button.adjustsImageSizeForAccessibilityContentSizeCategory = true
button.titleLabel?.numberOfLines = 0

黄色背景色是按钮的颜色,因此识别出只有可点击的区域是黄色区域.我想增加按钮框架,使整个文本区域变得可点击.

The yellow background colour is of the button, thus identifying that only tappable area is the yellow area. I want to increase the button frame so that the whole text area becomes tappable.

推荐答案

开始时多行按钮标签存在一个问题 - 我认为这与使用无障碍字体没有直接关系.

There is an issue with multi-line button labels to begin with - I don't think it's directly related to using accessibility fonts.

尝试使用这个按钮子类:

Try using this button subclass:

class MultilineTitleButton: UIButton {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    func commonInit() -> Void {
        self.titleLabel?.numberOfLines = 0
        self.titleLabel?.textAlignment = .center
        self.setContentHuggingPriority(UILayoutPriority.defaultLow + 1, for: .vertical)
        self.setContentHuggingPriority(UILayoutPriority.defaultLow + 1, for: .horizontal)
    }

    override var intrinsicContentSize: CGSize {
        let size = self.titleLabel!.intrinsicContentSize
        return CGSize(width: size.width + contentEdgeInsets.left + contentEdgeInsets.right, height: size.height + contentEdgeInsets.top + contentEdgeInsets.bottom)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        titleLabel?.preferredMaxLayoutWidth = self.titleLabel!.frame.size.width
    }
}

它设置titleLable的.numberOfLines = 0.textAlignment = .center和拥抱优先级,然后覆盖的intrinsicContentSize告诉auto- 布局 titleLabel 的适当大小.

It sets the titleLable's .numberOfLines = 0, .textAlignment = .center, and hugging priorities, and then override's intrinsicContentSize to tell auto-layout the proper size of the titleLabel.

这篇关于UIButton 框架不会随着使用 Swift 5 的可访问性大字体而增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆