代码之家  ›  专栏  ›  技术社区  ›  agibson007

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

  •  1
  • agibson007  · 技术社区  · 8 年前

    我有一个字符串,我想在UILabel或UITextView中设置,没有行距或很少。我知道NSParagraph样式具有最大和最小线宽以及线宽倍数。我已经尝试了所有的方法,但仍然得到了奇怪的结果。可能还需要使用基线偏移属性,但如果是这样,如何找到动态值以保持线间距均匀且线间距非常小?
    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 回复  |  直到 8 年前