Re: Help with iOS 15-style UIButton?


Alex Zavatone
 

Could you point out the specific workaround please?

On Jul 19, 2021, at 12:49 PM, Rick Aurbach via groups.io <rlaurb@...> wrote:

[Edited Message Follows]

I haven't filed a bug yet, but I will. In the meantime, I've constructed the following work around — it's a hack, but it seems to work.

    override open func draw(_ rect: CGRect) {

        drawButton()

        super.draw(rect)

    }

    

    private func drawButton() {

        if #available(iOS 15, *) {

            titleLabel?.lineBreakMode = .byTruncatingTail

            titleLabel?.numberOfLines = 1

            if let text = titleLabel?.text {

                let textSize = sizeText(string: text)

                let imageSize = imageView?.bounds.size ?? CGSize.zero

                let expectedWidth = textSize.width + textGap + imageSize.width + 8

                var currentBtnSize = self.bounds.size

                currentBtnSize = CGSize(width: max(expectedWidth, currentBtnSize.width), height: currentBtnSize.height)

                self.bounds.size = currentBtnSize

                setNeedsUpdateConstraints()

            }

        } else {

            contentHorizontalAlignment = .left

            titleLabel?.adjustsFontSizeToFitWidth = false

            titleLabel?.lineBreakMode = .byTruncatingTail

 

            let imageSize = image(for: .normal)?.size ?? .zero

            let optSize = intrinsicContentSize

#warning("consider changing this to mirrow above bounds code")

            if bounds.width < optSize.width || bounds.height < optSize.height {

                setNeedsLayout()

            }

            let actualTextWidth = optSize.width - textGap - imageSize.width - trailingGap

            imageEdgeInsets = UIEdgeInsets(top: 0.0, left: (actualTextWidth + textGap), bottom: 0.0, right: trailingGap)

            titleEdgeInsets = UIEdgeInsets(top: 0.0, left: -imageSize.width, bottom: 0.0, right: -actualTextWidth)

        }

    }


Join {cocoa@apple-dev.groups.io to automatically receive all group messages.