본문 바로가기

swift공부

swift label 행간 늘리기

반응형

프로젝트를 하다 보면 디자이너의 요청대로 행간(줄 사이의 공간)을 조절해야 할 때가 있다. 

 

피그마에 다음과 같이 행간을 조절하라는 예시 코드가 나와있다. 이걸 완성해 보도록 하겠다. 

 

        let paragraghStyle = NSMutableParagraphStyle()
        paragraghStyle.lineHeightMultiple = 1.23
        let titleAttrString = NSMutableAttributedString(string: diaryTitleLabel.text ?? "")
        titleAttrString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraghStyle, range: NSMakeRange(0, titleAttrString.length))
        diaryTitleLabel.attributedText = titleAttrString

먼저 원하는 행간을 완성하기 위한 코드이다. 

 

이것들을 하나씩 알아보면

먼저

NSMutableParagraphStyle

An object for changing the values of the subattributes in a paragraph style attribute

문단 유형 속성의 하위 특성 값을 변경하기 위한 개체

라고 되어있다. 

 

lineHeightMultiple

The line height multiple

The natural line height of the receiver is multiplied by this factor (if positive) before being constrained by minimum and maximum line height. The default value of this property is 0.0.

이 프로퍼티의 기본값은 0.0이고 receiver의 natural 라인 높이가 제한되기 전에 양수일 경우 곱해야한다고 나와있다.

 

NSMutableAttributedString

A mutable string with associated attributes (such as visual style, hyperlinks, or accessibility data) for portions of its text.

텍스트의 일부에 대해 연관된 특성(시각적 스타일, 하이퍼링크, 접근 데이터)을 가진 변환 가능 문자열

사용하고자 한다면 init(string: attributes:)를 사용하여 기본 속성이 아닌 문자열을 만들 수 있다. 

 

addAttribute(_:value:range:)

Adds an attribute with the given name and value to the characters in the specified range
지정된 범위의 문자에 지정된 이름과 값을 가진 속성을 추가할 수 있는 메서드이다. 

 

 

현재 이렇게 코드를 먼저 찾고 그 코드에 대한 설명을 하나씩 찾아보고 있지만 아직은 많이 어려운 느낌인 것 같다. 

반응형

'swift공부' 카테고리의 다른 글

Swift Realm 알아보기  (0) 2022.09.07
swift uikit preview(미리보기) 띄우기  (0) 2022.08.09
swift UITextView inset 제거  (0) 2022.07.11
swift 주석(comments), 퀵헬프  (0) 2022.05.18
swift) TextView text 글자수 세기  (0) 2022.02.06