Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto change the font size to fit the button in swift

I have tried this but it didn't work, the text is out of the button boundaries.

button.titleLabel!.adjustsFontSizeToFitWidth = true
button.titleLabel!.numberOfLines = 0
button.titleLabel!.minimumScaleFactor = 0.1

When I try the following, all the text fits, but the text remains in a small font:

button.titleLabel!.font = UIFont(name: "Heiti TC", size: 9)

How can I get the font to automatically fit the size of the button?

 func nextQuestion() {

    let currentQuestion = mcArray![questionIdx]

    answers = currentQuestion["Answers"] as! [String]
    correctAnswer = currentQuestion["CorrectAnswer"] as? String
    question = currentQuestion["Question"] as? String

    titlesForButtons()
}

func titlesForButtons() {
    for (idx,button) in answerButtons.enumerate() {
        button.titleLabel!.lineBreakMode = .ByWordWrapping

        button.titleLabel!.font = UIFont(name: "Heiti TC", size: 5)

        button.titleLabel!.numberOfLines = 0

        button.titleLabel!.minimumScaleFactor = 0.1

        button.titleLabel!.baselineAdjustment = .AlignCenters

        button.titleLabel!.textAlignment  = NSTextAlignment.Center

        button.setTitle(answers[idx], forState: .Normal)
        button.enabled = true
        button.backgroundColor = UIColor(red: 83.0/255.0, green: 184.0/255.0, blue: 224.0/255.0, alpha: 1.0)
    }

    questionLabel.text = question
    startTimer()
}

That is the code I have so far it gets the answers from a plist file

like image 678
acekidd Avatar asked Jun 27 '16 19:06

acekidd


People also ask

How do I change the font size on a button in Swift?

button. titleLabel. adjustsFontSizeToFitWidth = YES; With this all labels adjust the text will size.

How do I change the text of a button in Swift?

You can omit UIControlState part and just write like button. setTitle("my text here", forState: . Normal) .

How do I change the size of a button in Xcode?

You can customize width and height of button using storyboard in Xcode. Just select your button and open this window. After that just add the height constraints.


3 Answers

Adjust font size to fit width

Swift

self.button.titleLabel?.adjustsFontSizeToFitWidth = true

Objective-C

[self.button.titleLabel setAdjustsFontSizeToFitWidth:YES];
like image 59
Nancy Avatar answered Oct 21 '22 00:10

Nancy


This worked for me:

 button.titleLabel?.font = UIFont(name: "Avenir Next", size: 20)
    button.titleLabel?.adjustsFontSizeToFitWidth = true
    button.titleLabel?.numberOfLines = 1
    button.titleLabel?.minimumScaleFactor = 0.1
    button.clipsToBounds = true
like image 26
Alex Bailey Avatar answered Oct 20 '22 23:10

Alex Bailey


You can try this:

1.define the title size based on the current font size of your button

let nsTitle = NSString(string:"yourButtonTitle")
let font = button.titleLabel?.font
let titleSize = nsTitle.sizeWithAttributes([NSFontAttributeName:font])

2.check whether your title fits the button title label :

if titleSize.width > button.titleLabel?.bounds.width{

    //set the appropriate font size

}else{

    //set the appropriate font size or do nothing
}
like image 4
Elena Avatar answered Oct 20 '22 23:10

Elena