簡體   English   中英

最大線高切斷文字

[英]Maximum lineheight cutting off text

[ 破碎[1]

我正在尋找一個pixelPerfect UI。 我的設計師給了我一些特定字體的要求。 我正在嘗試實現它們,但我遇到了一些與paragraphStyle有關的問題。 所有文本的第一個基線必須位於網格上。 當我的標題的maximumLineHeight設置為24(2x網格)時,一切都很好。 但是,我的bodyText需要一個16的lineHeight。這會切斷第一行的頂部。

我已經嘗試設置標簽高度,這只會增加布局沖突。 我已經嘗試將firstBaseline約束設置為遠離它自己的頂部的網格度量。 無濟於事。 我還重寫了UILabel的draw方法,以添加一些額外的頂部填充。 但是,這會通過我添加的填充來拋出代碼中的fristBaseline約束用法。 很臟,只是最后的最后手段。

import Foundation

public class UILabelBody: UILabel  {


    override public var text: String? {
        didSet {
            let attributedString = NSMutableAttributedString(string: self.text!)
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.maximumLineHeight = 16

            attributedString.addAttributes([
                NSAttributedString.Key.kern: 0.4,
                NSAttributedString.Key.paragraphStyle : paragraphStyle],
                                           range: NSMakeRange(0, attributedString.length))
            self.attributedText = attributedString
        }
    }

    public init() {
        super.init(frame: CGRect.zero)
        self.translatesAutoresizingMaskIntoConstraints = false
        setup()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setup() {
        self.font = UIFont(name: "Oxygen", size: 16)
        self.textColor = UIColor(hexString: "1F1F1F")
    }
}

這是我設置約束的類:private func setup(){

    self.addSubview(contentBox)
    self.addSubview(imageView)
    self.addSubview(title)
    self.addSubview(body)


    imageView.backgroundColor = UIColor.yellow.withAlphaComponent(0.8)
    contentBox.backgroundColor = UIColor.red.withAlphaComponent(0.1)
    title.backgroundColor = UIColor.orange.withAlphaComponent(0.2)

    let padding = Layout.grid(1.5)

    let highPriority = UILayoutPriority(1000)
    let lowPrioririty = UILayoutPriority(100)

    let titleFirstBaseline = title.firstBaselineAnchor.constraint(equalTo: contentBox.topAnchor, constant: Layout.grid(2.5))
    let bodyFirstBaseLine =  body.firstBaselineAnchor.constraint(equalTo: title.lastBaselineAnchor, constant: Layout.grid(2))
    let selfHeight = self.heightAnchor.constraint(equalToConstant: Layout.grid(28))
    let selfWidth = self.widthAnchor.constraint(equalToConstant: Layout.grid(30))
    selfWidth.priority = highPriority
    selfHeight.priority = lowPrioririty

    titleFirstBaseline.priority = highPriority
    bodyFirstBaseLine.priority = highPriority


    NSLayoutConstraint.activate([


            selfWidth,
            selfHeight,

            contentBox.topAnchor.constraint(equalTo: self.topAnchor, constant: padding),
            contentBox.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: padding),
            contentBox.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -padding),
            contentBox.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -padding),

            imageView.leadingAnchor.constraint(equalTo: contentBox.leadingAnchor),
            imageView.topAnchor.constraint(equalTo: contentBox.topAnchor),
            imageView.widthAnchor.constraint(equalToConstant: Layout.grid(4)),
            imageView.heightAnchor.constraint(equalToConstant: Layout.grid(4)),

            title.leadingAnchor.constraint(equalTo: imageView.trailingAnchor, constant: padding),
            titleFirstBaseline,
            title.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -padding),

            bodyFirstBaseLine,
            body.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: padding),
            body.bottomAnchor.constraint(equalTo: self.contentBox.bottomAnchor),
            body.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -padding),
    ])
}

在酷刑室里待了幾個小時后,我終於明白了! 問題是字體本身。 這是一個.otf而不是.ttf。 因為這個快速不明白字體的內部線高,只是切斷它。 切換到.ttf后問題得到解決

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM