let attributes: [NSAttributedString.Key: Any] = [
.kern: letterSpacingPercent
]
let mutableAttributedString = NSMutableAttributedString(attributedString: self)
mutableAttributedString.addAttributes(attributes, range: NSRange(location: 0, length: self.length))
func setLineHeight(lineHeightMultiple: CGFloat) -> NSAttributedString {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineHeightMultiple = lineHeightMultiple
let attributes: [NSAttributedString.Key: Any] = [
.paragraphStyle: paragraphStyle
]
let mutableAttributedString = NSMutableAttributedString(attributedString: self)
mutableAttributedString.addAttributes(attributes, range: NSRange(location: 0, length: self.length))
return NSAttributedString(attributedString: mutableAttributedString)
}
func setLetterSpacing(letterSpacingPercent: CGFloat) -> NSAttributedString {
let attributes: [NSAttributedString.Key: Any] = [
.kern: letterSpacingPercent
]
let mutableAttributedString = NSMutableAttributedString(attributedString: self)
mutableAttributedString.addAttributes(attributes, range: NSRange(location: 0, length: self.length))
return NSAttributedString(attributedString: mutableAttributedString)
}
sizeToFit
을 통해서 가운데정렬을 하면 되지않을까했지만 해결X여러 레퍼런스를 뒤져보다가 나와 같은 문제를 겪은 블로그를 발견
기존 아무런 lineHeight를 적용하지않은 텍스트같은 경우는 딱 달라붙지만 lineHeight를 적용하면 밑단에 딱붙은 상태로 높이가 커지게 됨
아하 그렇다면 baselineOffset값을 이용하여 밑단에 떨어진 정도를 올려 가운데에 정렬되게끔 하면 되겠구나 !!
paragraphStyle.lineHeightMultiple = lineHeightMultiple
기존에 lineHeight를 배율로 받았기때문에 lineHeightMultiple로 넣었지만 baselineOffset를 이용하면서 max, min lineHeight값을 넣어야할 일이 생겨 self.font.lineHeight * font.lineHeightMultiple
공식을 통해 lineHeight를 구할 수 있음을 암
단순히 attribute를 적용하는것에 그치지않고 어떻게하면 잘게 쪼개어 팀원과 쓸 수 있는지를 고민해야함을 뼈저리게 느낌...