Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the font optional?

Tags:

ios

swift

uifont

I have created a struct to have my fonts pre cached available globally:

struct Fonts {
    static let avenirRegular = UIFont(name: "AvenirNextCondensed-Regular", size: 14.0)
}

Usage...:

xy.font = Fonts.avenirRegular

It tells me, that my constant is an Optional.

Value of optional type 'UIFont?' not unwrapped; did you mean to use '!' or '?'?

Why is it an optional? Is there a chance that AvenirNextCondensed-Regular is not available on every iOS devices? Help is very appreciated.

like image 285
David Seek Avatar asked Nov 07 '16 08:11

David Seek


People also ask

What is font optional?

optional. Gives the font face an extremely small block period and no swap period.

Why do we use the font size option?

The font size is a number that indicates how many points are in the height of the text, or high tall the text is in points. A point is 1/72 of an inch, so a 12-point font would be 12/72 of an inch.

Can I use font-display optional?

You can! That's what font-display: optional; does. It still gives the ~100ms font block period (giving the font a fighting chance to show up on first page view), but after that, the fallback is shown and will not swap. Chances are, the font did ultimately get downloaded, and next page view it will be cached and used.

What does font-display do?

The font-display property defines how font files are loaded and displayed by the browser. It is applied to the @font-face rule which defines custom fonts in a stylesheet.


2 Answers

the "optional" here means that the problem is the font might not exist(even though we know it does) and you have to recognize it, by making it optional you are telling the compiler that I know the font is not part of your library BUT I am sure it is there.

like image 97
MiladiuM Avatar answered Oct 06 '22 23:10

MiladiuM


The initializer you are using for UIFont is return optional UIFont? object ie the reason you are getting that suggestion for wrapping optional.

init?(name fontName: String, size fontSize: CGFloat)

Check Apple Documentation of UIFont for more details.

like image 42
Nirav D Avatar answered Oct 07 '22 01:10

Nirav D