1

I'm currently working with the following : Swift (1.2), Xcode (6.3) and iOS 8.3.

I currently have an application that look similar to this sample I've made for this question :

Storyboard

For each UIButton / UILabel I would like to configure their font size depending on which device is used (iPhone 4s to iPhone 6 Plus for example).

As you can see I tried to use the "text-displaying control" in order to add a large font size (44) and another one smaller (33). For this second one, the setting is on "Width: Compact ; Height: Any".

Below are my current results :

Screens

However, as you can see, the font on the iPhone 6 Plus is still the little one. I tried to change the settings (of the little font) for "Width: Compact ; Height: Regular" and with the System font ; but the result is the same :(

How could I have something like this (iPhone 6 Plus with the large font size) ?

Screens OK

Is there a way to :

  • target specific devices with AutoLayout directly within the storyboard ?
  • use some tricks to make it look great (maybe with constraints and minimum font scale) ?
  • do it outside the storyboard (through the code) ? The only solution is to check the value of UIScreen.mainScreen().bounds.size.width ?
lchamp
  • 6,592
  • 2
  • 19
  • 27
  • adjustsFontSizeToFitWidth is not an option? – Nekak Kinich May 27 '15 at 18:43
  • `adjustsFontSizeToFitWidth` is a bit tricky (need all the label to be the same size, ...). I would like, indeed, to keep my code without it. Anyway, it doesn't help on AutoLayout / SizeClass. – lchamp May 27 '15 at 18:51
  • On this test project there is no code appart from the `UITableViewDataSource` in order to display some rows. All the size classes are handled directly within the storyboard. I've uploaded the project here if it can help : http://goo.gl/3Mz5qY . It contains one project being the same as the screenshots in the question and one with "system font & W:Compact/H:Regular". In the first one every label have the small font and in the other one every label have the large font (no matter which iPhone it's build on). _PS : I've removed `CustomCell.swift` has it was irrelevant to the issue._ – lchamp May 27 '15 at 19:57
  • The problem you're having, I think, is that _in portrait orientation_ (which is what your screen shots are showing) an iPhone 6 plus is not distinguishable, by size class, from any other iPhone. Thus size classes, which are all that Interface Builder knows about, are useless to you. So if you want things to differ on iPhone 6 plus, you _must_ use code somehow. I suggest testing for the triple-resolution screen; only the non-zoomed iPhone 6 plus has that. – matt May 27 '15 at 20:06
  • I came across this question / answer : http://stackoverflow.com/questions/28076020/ios-different-font-sizes-within-single-size-class-for-different-devices . Is there a better way to check the _triple resolution screen_ ? Does an iPhone 6 Plus zoomed is equivalent to the iPhone 6 resolution (and iPhone 6 zoomed == iPhone 5 resolution) ? – lchamp May 27 '15 at 20:16
  • On the triple resolution screen, see my answer below. - Yes, iPhone 6 Plus zoomed thinks it is an iPhone 6; I do not need to special-case that one. – matt May 28 '15 at 02:34

1 Answers1

2

I just went through something like this with one of my own apps, and settled on this solution:

// global utility...
var on6plus : Bool {
    return UIScreen.mainScreen().traitCollection.displayScale > 2.5
}

// and then, in my view controller's viewDidLoad...
if on6plus {
    for lab in [self.scoreLabel, self.prevLabel, self.stageLabel] {
        let f = lab.font
        lab.font = f.fontWithSize(f.pointSize + 2)
    }
}
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks for this idea. Even if it still bothers me (as with the screen width switch) because behind the `UIScreen.mainScreen().traitCollection.displayScale > 2.5` there could be not only the iPhone 6 Plus at some point (like a new device in the future). _If no "better" answer can be provided I'll come back in few hours / days and accept yours._ – lchamp May 28 '15 at 20:14
  • 4
    Well, all you are really saying here is that there are not enough size classes. But we all knew _that_ back in June 2014! Just to give another example: it is perfectly reasonable to have one interface on iPad in portrait and another interface on iPad in landscape — as a split view controller demonstrates — but size classes do not distinguish them. Apple simply dropped the ball here: they forced us to use size classes but then they didn't give us enough of them. – matt May 28 '15 at 21:03