-
-
Save darrensapalo/bd6dddab6a70ae0a2d6cf8ac5aeb6b1a to your computer and use it in GitHub Desktop.
UIFont extensions to be able to get bold and bold italic fonts directly from a UIFont object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Swift 2 | |
UIFont extensions to be able to get bold and bold italic fonts directly | |
from a UIFont object | |
*/ | |
import Foundation | |
import UIKit | |
extension UIFont { | |
func withTraits(traits:UIFontDescriptorSymbolicTraits...) -> UIFont { | |
let descriptor = self.fontDescriptor() | |
.fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits(traits)) | |
return UIFont(descriptor: descriptor, size: 0) | |
} | |
func boldItalic() -> UIFont { | |
return withTraits(.TraitBold, .TraitCondensed) | |
} | |
func bold() -> UIFont { | |
return withTraits(.TraitBold) | |
} | |
} | |
/* Swift 3 | |
* Provided it as a computed property instead of a function as a personal preference. | |
*/ | |
import Foundation | |
import UIKit | |
public extension UIFont { | |
public func withTraits(_ traits: UIFontDescriptorSymbolicTraits...) -> UIFont { | |
let descriptor = self.fontDescriptor | |
.withSymbolicTraits(UIFontDescriptorSymbolicTraits(traits)) | |
return UIFont(descriptor: descriptor!, size: 0) | |
} | |
public var italic : UIFont { | |
return withTraits(.traitItalic) | |
} | |
public var bold : UIFont { | |
return withTraits(.traitBold) | |
} | |
} | |
Review this: (working fine on my end)
extension UIFont {
class func italicSystemFont(ofSize size: CGFloat, weight: UIFont.Weight = .regular)-> UIFont {
let font = UIFont.systemFont(ofSize: size, weight: weight)
switch weight {
case .ultraLight, .light, .thin, .regular:
return font.withTraits(.traitItalic, ofSize: size)
case .medium, .semibold, .bold, .heavy, .black:
return font.withTraits(.traitBold, .traitItalic, ofSize: size)
default:
return UIFont.italicSystemFont(ofSize: size)
}
}
func withTraits(_ traits: UIFontDescriptor.SymbolicTraits..., ofSize size: CGFloat) -> UIFont {
let descriptor = self.fontDescriptor
.withSymbolicTraits(UIFontDescriptor.SymbolicTraits(traits))
return UIFont(descriptor: descriptor!, size: size)
}
}
Usage:
myLabel.font = UIFont.italicSystemFont(ofSize: 34, weight: .ultraLight)
myLabel2.font = UIFont.italicSystemFont(ofSize: 34, weight: .black)
Review this: (working fine on my end)
extension UIFont { class func italicSystemFont(ofSize size: CGFloat, weight: UIFont.Weight = .regular)-> UIFont { let font = UIFont.systemFont(ofSize: size, weight: weight) switch weight { case .ultraLight, .light, .thin, .regular: return font.withTraits(.traitItalic, ofSize: size) case .medium, .semibold, .bold, .heavy, .black: return font.withTraits(.traitBold, .traitItalic, ofSize: size) default: return UIFont.italicSystemFont(ofSize: size) } } func withTraits(_ traits: UIFontDescriptor.SymbolicTraits..., ofSize size: CGFloat) -> UIFont { let descriptor = self.fontDescriptor .withSymbolicTraits(UIFontDescriptor.SymbolicTraits(traits)) return UIFont(descriptor: descriptor!, size: size) } }
Usage:
myLabel.font = UIFont.italicSystemFont(ofSize: 34, weight: .ultraLight) myLabel2.font = UIFont.italicSystemFont(ofSize: 34, weight: .black)
You saved my life man ! Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can we have some more italic font guide for other Font Weights like ultraLight, medium, etc?