There are two reasons why your code is not compiled:
- The initializer for the
NSAttributedString that you want to use requires explicitly marking the string parameter - The
UIFont initializer that you use now returns an optional (i.e., UIFont? ) That needs to be deployed before passing it to the attribute dictionary.
Try this instead:
let font = UIFont(name: "Voyage", size: 20.0) ?? UIFont.systemFontOfSize(20.0) let attrs = [NSFontAttributeName : font] var attrString3 = NSAttributedString(string: "(Time)", attributes: attrs)
Pay attention to the use of the new coalescence operator ?? . This deploys the optional Voyage font, but reverts to the system font if Voyage is unavailable (which seems to be the case on the playground). This way you get your attribute string regardless of whether your preferred font is loaded.
source share