Ios 具有多种字体和字体大小的NSAttribute字符串的行距

Ios 具有多种字体和字体大小的NSAttribute字符串的行距,ios,swift,uilabel,uitextview,nsattributedstring,Ios,Swift,Uilabel,Uitextview,Nsattributedstring,我有一个字符串,我想在UILabel或UITextView中设置,并且没有行距或很少。我知道NSParagraph样式具有最大和最小线宽以及线宽倍数。我尝试了所有方法,但仍然得到奇怪的结果。可能还需要使用“基线偏移”属性,但如果需要,如何找到动态值以保持线间距均匀且线间距非常小? 这里有一个例子。为了节省任何人的工作,我有一个开关,使每个字符串可以使用自己的段落风格 //: A UIKit based Playground for presenting user interface impo

我有一个字符串,我想在UILabel或UITextView中设置,并且没有行距或很少。我知道NSParagraph样式具有最大和最小线宽以及线宽倍数。我尝试了所有方法,但仍然得到奇怪的结果。可能还需要使用“基线偏移”属性,但如果需要,如何找到动态值以保持线间距均匀且线间距非常小?

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

//: 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()

你使用了错误的属性。在之前考虑<代码>段落空间。如何找到动态值来调整该值。这是否需要知道前几行字体或当前几行字体?我很确定段落间距之前不会完成,因为它不能是负数,以便使行更接近。可能与简单的
段落间距
结合使用?我认为它不可能是负数。我认为基线偏移可能会很小,但可能是马车把事情弄糟了。我已经在这上面挖了很长时间了。我没有发现任何令人惊讶的合理例子。