Created
March 23, 2019 20:49
-
-
Save ElonPark/9578285410fca4c009e1c5bbbd42cbfa to your computer and use it in GitHub Desktop.
어트리뷰트 스트링을 편하게 사용하기 위한 익스텐션
This file contains 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
extension String { | |
/** | |
## bold | |
- Parameter size: 폰트 크기 | |
- Parameter color: 폰트 컬러 | |
*/ | |
func bold(size fontSize: CGFloat, color: UIColor? = nil) -> NSMutableAttributedString { | |
var attributes: [NSAttributedString.Key : Any] = [ | |
.font: UIFont.boldSystemFont(ofSize: fontSize), | |
] | |
if let fontColor = color { | |
attributes[.foregroundColor] = fontColor | |
} | |
let attributeFont = NSMutableAttributedString(string: self, attributes: attributes) | |
return attributeFont | |
} | |
/** | |
## light | |
- Parameter size: 폰트 크기 | |
- Parameter color: 폰트 컬러 | |
*/ | |
func light(size fontSize: CGFloat, color: UIColor? = nil) -> NSMutableAttributedString { | |
var attributes: [NSAttributedString.Key : Any] = [ | |
.font: UIFont.systemFont(ofSize: fontSize, weight: .light), | |
] | |
if let fontColor = color { | |
attributes[.foregroundColor] = fontColor | |
} | |
let attributeFont = NSMutableAttributedString(string: self, attributes: attributes) | |
return attributeFont | |
} | |
/** | |
## 문자열 폰트 속성 | |
- Parameter size: 폰트 크기 | |
- Parameter weight: 폰트 패밀리는 폰트 크기를 반드시 넣어야 적용됨. | |
- Parameter color: 폰트 컬러 | |
*/ | |
func attribute(size: CGFloat? = nil, weight: UIFont.Weight? = nil, color: UIColor? = nil) -> NSMutableAttributedString { | |
var attributes = [NSAttributedString.Key : Any]() | |
if let fontSize = size { | |
attributes[.font] = UIFont.systemFont(ofSize: fontSize) | |
} | |
if let fontSize = size, let fontWeight = weight { | |
attributes[.font] = UIFont.systemFont(ofSize: fontSize, weight: fontWeight) | |
} | |
if let fontColor = color { | |
attributes[.foregroundColor] = fontColor | |
} | |
let attributeFont = NSMutableAttributedString(string: self, attributes: attributes) | |
return attributeFont | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment