Skip to content

Instantly share code, notes, and snippets.

@darrensapalo
Forked from ptsakyrellis/UIFont+.swift
Last active June 2, 2021 09:42
Show Gist options
  • Save darrensapalo/bd6dddab6a70ae0a2d6cf8ac5aeb6b1a to your computer and use it in GitHub Desktop.
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
/**
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)
}
}
@ablyGurjitSingh
Copy link

ablyGurjitSingh commented Mar 31, 2021

Can we have some more italic font guide for other Font Weights like ultraLight, medium, etc?

@ablyGurjitSingh
Copy link

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)

@SenNavy
Copy link

SenNavy commented Jun 2, 2021

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