Skip to content

Instantly share code, notes, and snippets.

@audrl1010
Last active March 27, 2019 10:09
Show Gist options
  • Save audrl1010/47d960b43f7663e28832a91a5ca72e71 to your computer and use it in GitHub Desktop.
Save audrl1010/47d960b43f7663e28832a91a5ca72e71 to your computer and use it in GitHub Desktop.
SwiftyFont

Usage

class MessageCell: UICollectionViewCell {
  enum Font {
    static let titleLabel = 14.systemFont.semibold
    static let dateLabel = 14.systemFont.regular
    static let bodyLabel = 16.systemFont.light
  }
  let titleLabel = UILabel().then {
    $0.numberOfLines = 1
    $0.font = Font.titleLabel
    $0.textColor = Color.titleLabel
  }
  let dateLabel = UILabel().then {
    $0.numberOfLines = 1
    $0.font = Font.dateLabel
    $0.textColor = Color.dateLabel
  }
  let bodyLabel = UILabel().then {
    $0.numberOfLines = 0
    $0.font = Font.bodyLabel
    $0.textColor = Color.bodyLabel
  }
  ...
}

Implementation

extension Int {
  var systemFont: SystemFont {
    return SystemFont(size: self)
  }
  var nanumSquareRoundFont: NanumSquareRoundFont {
    return NanumSquareRoundFont(size: self)
  }
}

struct SystemFont {
  let size: CGFloat
  
  init(size: Int) {
    self.size = CGFloat(size)
  }
  
  var italic: UIFont {
    return UIFont.italicSystemFont(ofSize: self.size)
  }
  var regular: UIFont {
    return UIFont.systemFont(ofSize: self.size, weight: .regular)
  }
  var ultraLight: UIFont {
    return UIFont.systemFont(ofSize: self.size, weight: .ultraLight)
  }
  var thin: UIFont {
    return UIFont.systemFont(ofSize: self.size, weight: .thin)
  }
  var light: UIFont {
    return UIFont.systemFont(ofSize: self.size, weight: .light)
  }
  var medium: UIFont {
    return UIFont.systemFont(ofSize: self.size, weight: .medium)
  }
  var semibold: UIFont {
    return UIFont.systemFont(ofSize: self.size, weight: .semibold)
  }
  var bold: UIFont {
    return UIFont.systemFont(ofSize: self.size, weight: .bold)
  }
  var heavy: UIFont {
    return UIFont.systemFont(ofSize: self.size, weight: .heavy)
  }
  var black: UIFont {
    return UIFont.systemFont(ofSize: self.size, weight: .black)
  }
}


// How to extends Custom Font.
struct NanumSquareRoundFont {
  let size: CGFloat
  
  init(size: Int) {
    self.size = CGFloat(size)
  }
  
  public var regular: UIFont {
    return UIFont(name: "NanumSquareRoundR", size: self.size)!
  }
  public var light: UIFont {
    return UIFont(name: "NanumSquareRoundL", size: self.size)!
  }
  public var bold: UIFont {
    return UIFont(name: "NanumSquareRoundB", size: self.size)!
  }
  public var extraBold: UIFont {
    return UIFont(name: "NanumSquareRoundEB", size: self.size)!
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment