0

I prefer to create my interfaces in code rather than using IB, but I wasn't able to change the font size of a UITextView that I'd created. Nothing was working at all. To try and debug the problem I added a UITextView to my main view using IB to see if that worked - and it did. The weird thing is that my programmatically created version also then worked, but only when the IB version is on the view. As soon as I delete it then the programmatically created version stops working - font size doesn't change.

Using traits, I can actually turn off the IB created version so that it's not installed and therefore never appears, which still allows the programmatically created version to work.

Anyone else seen this? Having a random element in IB isn't the end of the world, but it's frustrating and the sort of thing that will be difficult to explain/debug in the future.

fullWidthDetailText.isEditable = true
fullWidthDetailText.isSelectable = true
fullWidthDetailText.font = UIFont(name: "Roboto", size: 50)
fullWidthDetailText.adjustsFontForContentSizeCategory = false
fullWidthDetailText.text = "Hello world"
fullWidthDetailText.backgroundColor = UIColor.clear
fullWidthDetailText.textColor = UIColor.white
fullWidthDetailView.addSubview(fullWidthDetailText)
Sulthan
  • 128,090
  • 22
  • 218
  • 270
Roo
  • 259
  • 1
  • 3
  • 15
  • Thanks to the comments below, I've figured this out. I hadn't included the font in my plist file. Using IB must account for this and include the font anyway, where code requires the font to be listed in the plist file. One to watch out for as that feels like a bug. – Roo Jan 01 '19 at 22:38

2 Answers2

2

The name of the font is probably incorrect. I would expect it to be something like "Roboto-Regular" but to be absolutely sure, print the names of all available fonts to your console and find the one you want:

for family in UIFont.familyNames.sorted() {
    print("\(family): ", UIFont.fontNames(forFamilyName: family).sorted().joined(separator: ", "))
}

See How to check if a font is available in version of iOS? for details.

Note that font names are usually not user friendly, Interface Builder actually displays the font family.

If you still have trouble, see Adding a Custom Font to Your App and verify that you have done everything correctly.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • This does help, but not as expected. Running the code above with the duplicate created via IB lists Roboto as a font (as expected). If I don't add the element using IB and run the code the Robot isn't listed as a font. It's like the use of the font in IB is somehow properly including the font. – Roo Jan 01 '19 at 22:35
  • @Roo I think that cannot be correct, I wonder whether it's not somehow connected to broken build. Did you try to clean up your project? – Sulthan Jan 01 '19 at 23:02
0

Roboto is not installed on most iOS devices. That's a traditional Android font, not an iOS font. So UIFont(name: "Roboto", size 50) returns nil. If you pick a font that is installed on iOS (such as Helvetica, which is very close to Roboto), I can't reproduce this issue; the text appears in 50pt font as expected. Similarly .systemFont(ofSize: 50) seems to work fine.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • 1
    Hmm, maybe Roboto is on his computer and IB can thus see it? In that case his “solution” wouldn’t work on a device anyway – matt Jan 01 '19 at 21:56
  • I've packaged the font as part of the bundle and it loads correctly, you just can't change the font size unless you add an element with the font in IB. Very strange. – Roo Jan 01 '19 at 22:14
  • 1
    That "Roboto" font is probably just a bad name, I would guess it has to be something like "Roboto-Regular". – Sulthan Jan 01 '19 at 22:16
  • I did think it may be a font issue, but as soon as I add a UITextView using IB then the code-generated version works. It's like IB need to load the font in order to have the code work. – Roo Jan 01 '19 at 22:30
  • @Roo The problem is that nibs contain only the font name therefore using IB and code should make no difference. There must be some problem with your project or build. – Sulthan Jan 01 '19 at 23:03