25

problem:

Button size is enough, however when I change title, the title text cannot fit the button width. Any SDK function can solve this problem, or I need to manually code to solve it?

Please refer to following pictures.

design in the nib file. enter image description here

initial show in simulator enter image description here

when I change the title text enter image description here

tried some ways before

  1. _button.titleLabel.adjustsFontSizeToFitWidth = YES;
    the way will change my font size. I cannot accept the way.

  2. [_button setTitleEdgeInsets:UIEdgeInsetsMake(10.0, 10.0, 0.0,0.0)];
    the way change label's position only, not label size.

  3. [_button.titleLabel sizeToFit];
    result is same with picture(3).

  4. [_button sizeToFit];
    title moved to upper left corner, and the title still the same result.

Just confused, my button size is big enough, why title size is so small?

CCC
  • 2,164
  • 8
  • 27
  • 47

5 Answers5

43

Use this.

Objective-c

button.titleLabel.numberOfLines = 1;
button.titleLabel.adjustsFontSizeToFitWidth = YES;
button.titleLabel.lineBreakMode = NSLineBreakByClipping; 

Swift 2.0

button.titleLabel?.numberOfLines = 0
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping

NOTE: Swift code courtesy: @Rachel Harvey

Swift 5.0

button.titleLabel?.numberOfLines = 0
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.titleLabel?.lineBreakMode = .byWordWrapping
Nilesh
  • 1,493
  • 18
  • 29
14

For people who come across with this question:

Try using the setter:

 [self.myButton setTitle:@"Title" forState:UIControlStateNormal];
 [self.myButton sizeToFit];
Jonathan P. Diaz
  • 3,213
  • 1
  • 18
  • 13
3

Make sure also that the label doesn't adjust later spacing, as that seems to take precedence over adjusting font size. Also make sure the minimumScaleFactor < 1.

button.titleLabel.adjustsFontSizeToFitWidth = YES;
button.titleLabel.adjustsLetterSpacingToFitWidth = NO;
button.titleLabel.minimumScaleFactor = 0.5;
user3099609
  • 2,318
  • 18
  • 20
2

Here is the Swift version:

    let button = UIButton()
    button.titleLabel?.numberOfLines = 1
    button.titleLabel?.adjustsFontSizeToFitWidth = true
    button.titleLabel?.lineBreakMode = NSLineBreakMode.ByClipping
Esqarrouth
  • 38,543
  • 21
  • 161
  • 168
0

In IB, just select the button, go to the editor menu, and choose "Size To Fit Content"

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • hi, I tried, but not solve my problem. I have update (3)(4). thanks – CCC Dec 02 '12 at 04:55
  • 1
    btw, where is the setting on IB "size to fit", I cannot find it in IB. I just find 'scale to fit' in XCode4.5 – CCC Dec 02 '12 at 04:57
  • It should be "Size To Fit Content", I've updated my answer. It's where I said it was -- under the Editor menu. Doing that in IB is different from using sizeToFit in code. – rdelmar Dec 02 '12 at 04:58
  • I got it. editor > "Size To Fit Content", and I remove code mentioned (3) and (4), and tried again. the result is still same. see picture3. thanks – CCC Dec 02 '12 at 05:11
  • I don't know what to say -- it works for me. It may have something to do with the layout constraints. – rdelmar Dec 02 '12 at 05:19
  • yep, sometimes it's strange on IB. I uncheck autolayout setting when I deal with IB. – CCC Dec 02 '12 at 05:30
  • But, I think that "Size To Fit Content" might only work when you use auto layout. – rdelmar Dec 02 '12 at 05:44