티스토리 뷰

iOS & swift

자간, 행간 (Extension UILabel)

ggasoon2 2023. 12. 9. 19:25

 

먼저 행간 설정입니다.

 

// 행간
extension UILabel {
    func setLineSpacing(ratio: Double) {
        let style = NSMutableParagraphStyle()
        let lineheight = self.font.pointSize * ratio  //font size * multiple
        style.minimumLineHeight = lineheight
        style.maximumLineHeight = lineheight

        self.attributedText = NSAttributedString(
            string: self.text ?? "", attributes: [
            .paragraphStyle: style
          ])
    }
}

extension UILabel {
    func setLineSpacing(spacing: CGFloat) {
        guard let text = text else { return }

        let attributeString = NSMutableAttributedString(string: text)
        let style = NSMutableParagraphStyle()
        style.lineSpacing = spacing
        attributeString.addAttribute(.paragraphStyle,
                                     value: style,
                                     range: NSRange(location: 0, length: attributeString.length))
        attributedText = attributeString
    }
}

 

 

첫번째는 extension은 비율로 행간을 설정하는 방법입니다. ratio 값으로 1.5 이렇게 설정할 수 있습니다

 

두번째는 extension은 행간을 수치로 주는 방법입니다

 

 

 

 

 

다음은

자간 설정입니다.

 

extension UILabel {
    // 자간
    func addCharacterSpacing(_ value: Double = -0.03) {
        let kernValue = self.font.pointSize * CGFloat(value)
        guard let text = text, !text.isEmpty else { return }
        let string = NSMutableAttributedString(string: text)
        string.addAttribute(NSAttributedString.Key.kern, value: kernValue, range: NSRange(location: 0, length: string.length - 1))
        attributedText = string
    }
}

 

 

default 값으로 -0.03 설정해뒀는데 수정해서 사용하면 되겠습니다.

 

 

 

 

 

추가적으로 

 

 

extension UITextView {
    // 자간
    func addCharacterSpacing(_ value: Double = -0.03) {
        let kernValue = self.font!.pointSize * CGFloat(value)
        guard let text = text, !text.isEmpty else { return }
        let string = NSMutableAttributedString(string: text)
        string.addAttribute(NSAttributedString.Key.kern, value: kernValue, range: NSRange(location: 0, length: string.length - 1))
        attributedText = string
    }
    // 행간
    func setLineSpacing(lineSpacing: CGFloat) {
        // Get the existing attributed text from the UITextView
        guard let existingAttributedString = self.attributedText else {
            return
        }
        // Create a mutable copy of the existing attributed text
        let mutableAttributedString = NSMutableAttributedString(attributedString: existingAttributedString)
        // Create a paragraph style with the desired line spacing
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = lineSpacing
        // Set the paragraph style for the entire text
        mutableAttributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, mutableAttributedString.length))
        // Set the updated attributed text to the UITextView
        self.attributedText = mutableAttributedString
    }
}

extension UITextField {
    func addCharacterSpacing(_ value: Double = -0.03) {
        let kernValue = self.font!.pointSize * CGFloat(value)
        guard let text = text, !text.isEmpty else { return }
        let string = NSMutableAttributedString(string: text)
        string.addAttribute(NSAttributedString.Key.kern, value: kernValue, range: NSRange(location: 0, length: string.length - 1))
        attributedText = string
    }
}

 

 

UITextField나 UITextView에서도 이렇게 사용가능합니다

 

 

 

 

감사합니다

 

 
댓글