0

enter image description here

As you can see from the picture, the padding at the top and bottom of the UIButtonLabel is strange.

I want the text to always be centered as in UILabel. Here is my code:

extension UILabel {
    func apply(lineHeight: CGFloat, letterSpacing: CGFloat = 0) {
        let attributedString = NSMutableAttributedString(string: text ?? " ")
        let range = NSMakeRange(0, attributedString.length)
        
        // Line Height
        let style = NSMutableParagraphStyle()
        style.minimumLineHeight = lineHeight
        style.maximumLineHeight = lineHeight
        attributedString.addAttribute(.paragraphStyle, value: style, range: range)
        attributedString.addAttribute(.baselineOffset, value: (lineHeight - font.lineHeight) / 2, range: range)
        
        // Letter Spacing
        attributedString.addAttribute(.kern, value: letterSpacing, range: range)
        
        attributedText = attributedString
    }
}

label.apply(lineHeight: 24)
button.titleLabel.apply(lineHeight: 24)
oddK
  • 261
  • 4
  • 14
  • you can not use private function in extension to outside – Reza Khonsari Feb 18 '22 at 00:16
  • 1
    thanks. I edit that code. But that is not the cause of this problem. @RezaKhonsari – oddK Feb 18 '22 at 00:23
  • Button has insets or padding please remove it's padding and check if you're problem is gone: button.titleLabel?.apply(lineHeight: 24) \n var buttonConfig = UIButton.Configuration.plain() \n buttonConfig.contentInsets = .zero \n button.configuration = buttonConfig \n button.setTitleColor(.black, for: []) – Reza Khonsari Feb 18 '22 at 00:32
  • exactly same result :-( @RezaKhonsari – oddK Feb 18 '22 at 00:39
  • also could you add these lines buttonConfig.imagePadding = 0 buttonConfig.titlePadding = 0 – Reza Khonsari Feb 18 '22 at 00:44
  • last code I sent for you works on my project – Reza Khonsari Feb 18 '22 at 00:45
  • Sadly it's still the same. Even the current test is an iOS 14 device, so Button Configuration cannot be applied... @RezaKhonsari – oddK Feb 18 '22 at 00:49
  • Maybe try `button.titleLabel?.baselineAdjustment = .alignCenters` – Shawn Frank Feb 18 '22 at 05:54
  • @ShawnFrank :-( It's still same... – oddK Feb 18 '22 at 06:50
  • @oddK -- are you *sure* you don't have something else going on that would affect the button and label? I just gave your extension a try, and this is what I get: https://i.stack.imgur.com/0co2Y.png and https://i.stack.imgur.com/itQe4.png ... the Top pair has the label centered on the button, the bottom pair has the label only partially covering the button. – DonMag Feb 18 '22 at 13:51
  • @DonMag Actually add something else. Change the font. But both the buttons and labels are the same font, even the same size and weight. – oddK Feb 21 '22 at 00:28
  • @oddK - OK, you'll need to provide complete information. Just tried it with a couple different fonts and cannot reproduce the issue you're seeing. – DonMag Feb 21 '22 at 12:41
  • I re upload question. https://stackoverflow.com/q/71259838/10283519 – oddK Feb 25 '22 at 00:52

0 Answers0