具有多个字体和FontSizes的NSAttributedString的行距

时间:2018-10-23 14:02:35

标签: ios swift uilabel uitextview nsattributedstring

我有一个要在UILabel或UITextView中设置的字符串,并且没有行距或很小。我知道NSParagraph样式具有最大和最小lineHeight以及lineHeightMultiple。我已经尽力了,但仍然得到了奇怪的结果。可能还需要使用基线偏移量属性,但是如果是这样,您如何找到动态值以使行之间的行间距保持均匀和很小?
CurrentExample

这里是一个例子。为了节省任何人的工作,我有一个开关,以便每个字符串都可以使用其自己的段落样式。

//: A UIKit based Playground for presenting user interface

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white

        let label = UILabel()
        label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
        label.text = "Hello World!"
        label.textColor = .black
        label.numberOfLines = 0
        label.backgroundColor = UIColor.green
        view.addSubview(label)
        self.view = view


        let tryWithIndParagraphStyles = false
        if tryWithIndParagraphStyles{
            let mutAttr = NSMutableAttributedString()
            let para = NSMutableParagraphStyle()
            para.alignment = .center

            //WE COULD TRY LINE HEIGHT OR LINE HEIGHT MULTIPLE but the spacing is still wrong
            //        para.lineHeightMultiple = 0.75



            //we could manually set the line heights for each
            let firstFont = UIFont.systemFont(ofSize: 75.56262594898386)
            let firstPara = NSMutableParagraphStyle()
            firstPara.alignment = .center
            firstPara.minimumLineHeight = firstFont.pointSize - firstFont.ascender + firstFont.capHeight
            firstPara.maximumLineHeight = firstFont.pointSize - firstFont.ascender + firstFont.capHeight
            let firstStr = NSAttributedString(string: "HEY\r", attributes: [.font:firstFont,.paragraphStyle:firstPara])
            mutAttr.append(firstStr)

            let secondFont = UIFont.systemFont(ofSize: 24.099621199347652)
            let secondPara = NSMutableParagraphStyle()
            secondPara.alignment = .center

            secondPara.minimumLineHeight = secondFont.pointSize - secondFont.ascender + secondFont.capHeight
            secondPara.maximumLineHeight = secondFont.pointSize - secondFont.ascender + secondFont.capHeight
            let secondStr = NSAttributedString(string: "BEGGING TO\r", attributes: [.font:secondFont,.paragraphStyle:secondPara])
            mutAttr.append(secondStr)


            let thirdFont = UIFont.systemFont(ofSize: 59.53419014162365)
            let thirdPara = NSMutableParagraphStyle()
            thirdPara.alignment = .center

            thirdPara.minimumLineHeight = thirdFont.pointSize - thirdFont.ascender + thirdFont.capHeight
            thirdPara.maximumLineHeight = thirdFont.pointSize - thirdFont.ascender + thirdFont.capHeight
            let thirdStr = NSAttributedString(string: "TEST\r", attributes: [.font:thirdFont,.paragraphStyle:thirdPara])
            mutAttr.append(thirdStr)

            let fourthFont = UIFont.systemFont(ofSize: 20.96469640066495)

            let fourthPara = NSMutableParagraphStyle()
            fourthPara.alignment = .center

            fourthPara.minimumLineHeight = fourthFont.pointSize - fourthFont.ascender + fourthFont.capHeight
            thirdPara.maximumLineHeight = fourthFont.pointSize - fourthFont.ascender + fourthFont.capHeight
            let fourthStr = NSAttributedString(string: "HAMBURGERS", attributes: [.font:fourthFont,.paragraphStyle:fourthPara])
            mutAttr.append(fourthStr)

            label.attributedText = mutAttr
            label.sizeToFit()
        }else{
            let mutAttr = NSMutableAttributedString()
            let para = NSMutableParagraphStyle()
            para.alignment = .center

            //WE COULD TRY LINE HEIGHT OR LINE HEIGHT MULTIPLE but the spacing is still wrong
            para.lineHeightMultiple = 0.75



            //we could manually set the line heights for each
            let firstFont = UIFont.systemFont(ofSize: 75.56262594898386)
            let firstStr = NSAttributedString(string: "HEY\r", attributes: [.font:firstFont,.paragraphStyle:para])
            mutAttr.append(firstStr)

            let secondStr = NSAttributedString(string: "BEGGING TO\r", attributes: [.font:UIFont.systemFont(ofSize: 24.099621199347652),.paragraphStyle:para])
            mutAttr.append(secondStr)

            let thirdStr = NSAttributedString(string: "TEST\r", attributes: [.font:UIFont.systemFont(ofSize: 59.53419014162365),.paragraphStyle:para])
            mutAttr.append(thirdStr)

            let fourthStr = NSAttributedString(string: "HAMBURGERS", attributes: [.font:UIFont.systemFont(ofSize: 20.96469640066495),.paragraphStyle:para])
            mutAttr.append(fourthStr)

            label.attributedText = mutAttr
            label.sizeToFit()
        }
    }
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

0 个答案:

没有答案