Вопрос:

How to resize UIButton depending on titleLabel sizel?

swift uibutton

58 просмотра

1 ответ

92 Репутация автора

I use an array of 4 buttons and i want to resize the height of them depending on title size. Or resize characters of the title. What is the best and easiest? My buttons are btn1 btn2 btn3 btn4

for i in 0..<buttons.count{
            buttons[i].setTitle(Questions.[QNumber].Answers[i], for: UIControlState.normal)
        }
Автор: Alex Andreadis Источник Размещён: 08.11.2017 11:41

Ответы (1)


2 плюса

749 Репутация автора

Ok, I have a few solutions for you. Your question asks to resize the height of the button OR the characters of the titleLabel.

  1. It's easier to adjust the size of the characters in the label. Here's the solution for that (replace btn with your buttons):

    btn.titleLabel?.minimumScaleFactor = 0.5
    btn.titleLabel?.numberOfLines = 0
    btn.titleLabel?.adjustsFontSizeToFitWidth = true
    
  2. Resizing the height of the buttons is a bit trickier. I've found a couple links to other answers that may work for you if you prefer this:

Автор: Thomas Wang Размещён: 08.11.2017 11:47
Вопросы из категории :
32x32