Это не самая красивая, но функциональная. Он обрабатывает все встроенные iOS шрифты.
Расширение:
extension UIFont {
func italic() -> UIFont? {
if let italic = UIFont.init(name: "\(self.fontName)-Italic", size: self.pointSize) {
return italic
} else if let italic = UIFont.init(name: "\(self.fontName)Italic", size: self.pointSize) {
return italic
}
if let oblique = UIFont.init(name: "\(self.fontName)-Oblique", size: self.pointSize) {
return oblique
} else if let oblique = UIFont.init(name: "\(self.fontName)Oblique", size: self.pointSize) {
return oblique
}
var suffixFontName = self.fontName // for font names that have a 2 letter suffix such as ArialMT
suffixFontName.removeLast(2)
let suffix = self.fontName.suffix(2)
if let suffixFontName = UIFont.init(name: "\(suffixFontName)-Italic\(suffix)", size: self.pointSize) {
return suffixFontName
}
if let suffixFontName = UIFont.init(name: "\(suffixFontName)Italic\(suffix)", size: self.pointSize) {
return suffixFontName
}
// no italic, oblique, or mt italic font found
return nil
}
}
Использование:
let helveticaNeueLightItalic = UIFont(name: "HelveticaNeue-Light", size: 16.0)!.italic()
print(helveticaNeueLightItalic)
let helveticaItalic = UIFont(name: "Helvetica", size: 16.0)!.italic()
print(helveticaItalic)
let helveticaLightItalic = UIFont(name: "Helvetica-Light", size: 16.0)!.italic()
print(helveticaLightItalic)
let arialMtItalic = UIFont(name: "ArialMT", size: 16.0)!.italic()
print(arialMtItalic)
let arialMtBoldItalic = UIFont(name: "Arial-BoldMT", size: 16.0)!.italic()
print(arialMtBoldItalic)