iOS/코드조각

[iOS, Swift] UILabel 텍스트 양쪽 정렬(Justify Text)

검은참깨두유vm 2022. 8. 20. 19:34
반응형

iOS 15.5, Xcode 13.31, Swift 5, UIKit 환경에서 진행했습니다.

 

let loremText = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."

label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.justified

let attributes = [NSAttributedString.Key.paragraphStyle: paragraphStyle, NSAttributedString.Key.baselineOffset: NSNumber(floatLiteral: 0)]
let attributesString = NSAttributedString(string: loremText, attributes: attributes)
label.attributedText = attributesString

 

텍스트 양쪽 정렬을 사용할 때, NSTextAlignment.justified를 쓴다.

영어에서는 단어 단위로 양쪽 정렬이 잘 되지만, 한글은 한 글자 단위로 양쪽 정렬이 되어 큰 차이가 없다.

 

 

반응형