Core Text - NSAttributedString line height done right?

前端 未结 10 927
Happy的楠姐
Happy的楠姐 2020-12-23 09:21

I\'m completely in the dark with Core Text\'s line spacing. I\'m using NSAttributedString and I specify the following attributes on it: - kCTFontAttributeName - kCTParagraph

10条回答
  •  清歌不尽
    2020-12-23 09:45

    I tried all these answers, but to really get the EXACT line height that usually comes in design files from Sketch or Zeplin then you need to:

    let ps = NSMutableParagraphStyle()
    ps.minimumLineHeight = 34
    ps.maximumLineHeight = 34
    let attrText = NSAttributedString(
        string: "Your long multiline text that will have exact line height spacing",
        attributes: [
            .paragraphStyle: ps
        ]
    )
    someLabel.attributedText = attrText
    someLabel.numberOfLines = 2
    ...
    

提交回复
热议问题