iOS, Swift
Swift ) UILabel 줄 수 알아내기 - EEYatHo iOS
EEYatHo
2021. 3. 24. 13:47
반응형
개발을 하다보면 Label 의 줄 수에 따라서 조건이 변하는 경우가 생기는데,
대부분의 경우 AutoLayout으로 해결하지만, 줄 수에 따라 분기해야하는 경우도 발생합니다.
text와 font를 가지고 , 미리 줄의 수를 알 수 있는 방법이 있습니다.
class Utilities {
...
/*
UILabel의 줄 수를 return 합니다.
label : .text와 .font를 설정해준 채로 보내줘야합니다.
labelWidth : 한 줄의 넓이입니다.
*/
class func lineNumber(label: UILabel, labelWidth: CGFloat) -> Int {
let boundingRect = label.text!.boundingRect(with: .zero, options: [.usesFontLeading],
attributes: [.font: label.font!], context: nil)
return Int(boundingRect.width / labelWidth + 1)
}
...
}
감사합니다