2

I'm trying to fit the letter '6' inside a UIButton so that the font automatically scales down when the program is run on a small screen device, such that the letter 6 is inside from all sides.

I found several other threads offering solutions to the same problem but for some reason they don't work for me. A few threads I found were:

I have tried the following two code sequences but none gets the job done:

What should I do ?

1:

self.sixButton.titleLabel!.numberOfLines = 0;
self.sixButton.titleLabel!.adjustsFontSizeToFitWidth = true;
self.sixButton.titleLabel!.lineBreakMode = NSLineBreakMode.ByWordWrapping

2:

self.sixButton.titleLabel!.minimumScaleFactor = 0.5;
self.sixButton.titleLabel!.numberOfLines = 0;
self.sixButton.titleLabel!.adjustsFontSizeToFitWidth = true;

3: Same as above but with self.sixButton.titleLabel!.numberOfLines = 1;.

I'm using Swift 2.2 with Xcode 7.3.1.

A picture of the button at design time:

enter image description here

A picture of the button at run time:

enter image description here

Community
  • 1
  • 1
Ahmad
  • 12,886
  • 30
  • 93
  • 146
  • Hi. What is the font size of your titleLabel? – Christian Abella Jul 21 '16 at 03:07
  • 120 (font is Verdana). I just increased the font a lot to see if it gets scaled down automatically or not. As is obvious, it didn't scale down. – Ahmad Jul 21 '16 at 03:09
  • You can programatically calculate the size before hand like in here: http://stackoverflow.com/questions/19128797/calculating-uilabel-text-size – Farini Jul 21 '16 at 03:55
  • every font has line height property , it will give line height for one line , in your case you can write like this self.sixButton.titleLabel!.Font.Lineheight – Prashant Tukadiya Jul 21 '16 at 04:41

2 Answers2

1

What I would do is measure the size of the "6" in successively smaller font sizes until we get to a size that fits in the desired bounds, and use that size.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Well that is the most obvious way to do it, but I'm wondering if there's a way to automate all that logic. – Ahmad Jul 21 '16 at 03:34
  • I understand. I'm suggesting that there isn't a _built-in_ way. Of course, once you write the code, the logic is automated; that's what code is. :) – matt Jul 21 '16 at 03:45
  • I should point out, however, that it is more usual to do the opposite of what you are doing: set the text in a desired font and size, and let the button resize itself to fit. That is what it wants to do, after all. – matt Jul 21 '16 at 03:47
  • Here is some code that I provided in a similar situation: http://stackoverflow.com/a/33160782/341994 – matt Jul 21 '16 at 03:50
0

My solution to this would be to have a label and an invisible button that work together. The invisible button sits on top of the label. The behaviour of text in a label is more flexible and predictable than that of a button in my experience. This probably isn't the cleverest or slickest solution, but it works great and it's painless to implement. You just need to ensure that your constraints keep the button and label aligned.

Westside
  • 675
  • 1
  • 7
  • 16