adjust UIButton font size to width

匿名 (未验证) 提交于 2019-12-03 01:29:01

问题:

I have the following code:

  UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];   button.frame = CGRectMake(0.0, 0.0, 25, 25);    [[button layer] setCornerRadius:5.0f];   [[button layer] setMasksToBounds:YES];   [[button layer] setBackgroundColor:[[UIColor redColor] CGColor]];   [button.titleLabel setFrame:CGRectMake(0,0, 25, 25)];    [button setTitle:[NSString stringWithFormat:@"%@", [[topics objectAtIndex:indexPath.row] unread]] forState:UIControlStateNormal];

The issue is that when the string in the text is not long it shows fine (1-2 digit), however when it's quite long (3++ digit) all I can see is a red button, with no text inside. How do I adjust this?

I don't think that:

[button.titleLabel setAdjustsFontSizeToFitWidth:YES];

does the job right?

回答1:

Try this:

button.titleLabel.numberOfLines = 1; button.titleLabel.adjustsFontSizeToFitWidth = YES; button.titleLabel.lineBreakMode = NSLineBreakByClipping; //<-- MAGIC LINE

I'm not sure why this does the trick but it does :)



回答2:

button.titleLabel.adjustsFontSizeToFitWidth = YES;

should do the work on its own if you are using Auto-Layout and have set a constraint on the button's width.

The other options (minimum scale factor, number of lines etc) can still be used to customize further according to your needs, but are not required.



回答3:

The answer from EliBud doesn't work on iOS 8. I found a solution which works on iOS 8. Below is a swift code:

let label = self.button?.titleLabel label?.minimumScaleFactor = 0.01 label?.adjustsFontSizeToFitWidth = true label?.font = UIFont.systemFontOfSize(100)

You can play with label?.lineBreakMode as I found that results varies for different break modes.



回答4:

adjustsFontSizeToFitWidth wasn't working for me until I set a width constraint on my button in Interface Builder.

Setting the constraint kept the button from growing in size and therefore not realizing it had to shrink the text.



回答5:

iOS 10.3 solution based on the other answers here:

    button.titleLabel!.numberOfLines = 1     button.titleLabel!.adjustsFontSizeToFitWidth = true     button.titleLabel!.baselineAdjustment = .alignCenters

Nobody mentioned baselineAdjustment yet; I needed it because the button label becomes vertically misaligned after adjustsFontSizeToFitWidth takes effect. Apple's baselineAdjustment documentation:

If the adjustsFontSizeToFitWidth property is set to true, this property controls the behavior of the text baselines in situations where adjustment of the font size is required. The default value of this property is alignBaselines. This property is effective only when the numberOfLines property is set to 1.


FWIW, I could never get the label perfectly aligned.



回答6:

in latest swift this seems to work for me

button.titleLabel!.numberOfLines = 1 button.titleLabel!.adjustsFontSizeToFitWidth = true button.titleLabel!.lineBreakMode = NSLineBreakMode.ByClipping


回答7:

Xamarin.iOS solution

var accountButton = new UIButton(); accountButton.SetTitle("Account", UIControlState.Normal);             accountButton.TitleLabel.AdjustsFontSizeToFitWidth = true; accountButton.TitleLabel.Lines = 1; accountButton.TitleLabel.LineBreakMode = UILineBreakMode.Clip; accountButton.TitleLabel.Font = accountButton.TitleLabel.Font.WithSize(35);

I ended up setting the font size to ensure the font was "large" before the system adjusts the size to fit.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!