使用Swift 5,UIButton框架不会随着辅助功能大字体而增加 [英] UIButton frame does not increase with accessibility large font using Swift 5

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

问题描述

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:

约束仅是顶部,前导和尾随,在代码中我也添加了2行:

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.

尝试使用此按钮子类:

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
    }
}

它设置标题标签的 .numberOfLines = 0 .textAlignment =。中心,并包含优先级,然后覆盖 intrinsicContentSize 告诉自动布局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.

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

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