Ios 设置不同字体大小标签的顶部对齐,Swift 3

Ios 设置不同字体大小标签的顶部对齐,Swift 3,ios,swift,constraints,Ios,Swift,Constraints,我在Objective C代码中看到过类似的问题,但没有太多帮助 我有两个标签(currencyLabel,costLabel)不同的字体大小,我希望他们能对齐顶部,如下图所示。我尝试为两者设置相同的顶部间距(viewHeight/3),但这似乎不起作用 约束在代码的最后4行中设置 如果有更好的方法,请提供建议 代码如下: override func viewDidLoad() { super.viewDidLoad() let viewWidth = self.vi

我在Objective C代码中看到过类似的问题,但没有太多帮助

我有两个标签(currencyLabel,costLabel)不同的字体大小,我希望他们能对齐顶部,如下图所示。我尝试为两者设置相同的顶部间距(viewHeight/3),但这似乎不起作用

约束在代码的最后4行中设置

如果有更好的方法,请提供建议

代码如下:

override func viewDidLoad() {
    super.viewDidLoad()

    let viewWidth   =   self.view.bounds.width
    let viewHeight  =   self.view.bounds.height

    // 1. Creating Currency Label
    currencyLabel                 =   UILabel()
    currencyLabel.numberOfLines   =   1
    currencyLabel.text            =   "$"
    currencyLabel.textColor       =   Colors.curieBlue
    currencyLabel.font            =   UIFont.systemFont(ofSize: 50)

    // 1.1 Creating Cost Label
    costLabel                 =   UILabel()
    costLabel.numberOfLines   =   1
    costLabel.text            =   "15"
    costLabel.textColor       =   Colors.curieBlue
    costLabel.font            =   UIFont.boldSystemFont(ofSize: 150)

    // Disabling auto constraints
    currencyLabel.translatesAutoresizingMaskIntoConstraints =   false
    costLabel.translatesAutoresizingMaskIntoConstraints     =   false

    // Adding subviews to main view
    self.view.addSubview(currencyLabel)
    self.view.addSubview(costLabel)


    let views = [
        "currencyLabel"     :   currencyLabel,
        "costLabel"         :   costLabel
        ] as [String : Any]



    // Setting constraints for Cost Label
    view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-\(costLabelLeftSpacing)-[costLabel]", options: [], metrics: nil, views: views))
    view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-\(viewHeight / 3)-[costLabel]", options: [], metrics: nil, views: views))

    // Setting constraints for Currency Label
    view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[currencyLabel]-10-[costLabel]", options: [], metrics: nil, views: views))
    view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-\(viewHeight / 3)-[currencyLabel]", options: [], metrics: nil, views: views))

}

可以使用“定位”工具旁边的“对齐”工具在货币和值标签之间添加约束。对齐两个标签的上边缘。希望能有帮助

编程解决方案:

您可以使用UILabel的属性字符串属性来处理这种情况

属性选项有一个名为“NSBaselineOffsetAttributeName”的选项,它将使文本偏移默认基线。这个偏移量可以通过UIFont属性来测量

偏移=上升器-盖高度

代码示例:

 class CustomViewController: UIViewController {

        override func viewDidLoad() {
            super.viewDidLoad()    
            setupViews()
        }

        let currencyLabel: UILabel = {
            let label = UILabel()
            let font = UIFont.systemFont(ofSize: 50)

            // measure baseline offset
            let offset = font.ascender - font.capHeight

            label.attributedText = NSAttributedString(string: "$", attributes: [NSFontAttributeName: font, NSBaselineOffsetAttributeName: offset])
            label.translatesAutoresizingMaskIntoConstraints = false
            label.backgroundColor = UIColor.blue
            return label
        }()

        let costLabel: UILabel = {
            let label = UILabel()
            let font = UIFont.systemFont(ofSize: 150)
            let offset = font.ascender - font.capHeight
            label.attributedText = NSAttributedString(string: "15", attributes: [NSFontAttributeName: font, NSBaselineOffsetAttributeName: offset])
            label.translatesAutoresizingMaskIntoConstraints = false
            label.backgroundColor = UIColor.green
            return label
        }()

        func setupViews() {
            view.backgroundColor = UIColor.red

            view.addSubview(currencyLabel)
            view.addSubview(costLabel)

            view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[v0]-[v1]", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": currencyLabel, "v1": costLabel]))
            view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-20-[v0]", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": currencyLabel]))
            view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-20-[v0]", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": costLabel]))
        }

    }
结果:

我使用了以下代码:

let font:UIFont? = UIFont(name: "Helvetica", size:30)

let fontSuper:UIFont? = UIFont(name: "Helvetica", size:18)

let fontspace:UIFont? = UIFont(name: "Helvetica", size:8)

let attString:NSMutableAttributedString = NSMutableAttributedString(string: "₹ 6000.00", attributes: [.font:font!])

attString.setAttributes([.font:fontSuper!,.baselineOffset:8], range: NSRange(location:0,length:1))

attString.setAttributes([.font:fontspace!,.baselineOffset:8], range: NSRange(location:1,length:1))

attString.setAttributes([.font:fontSuper!,.baselineOffset:0], range: NSRange(location:attString.length-3,length:3))

lblPrice.attributedText = attString

我怎么能通过编程做到这一点呢?我从来不知道UI标签背后有这么多东西,谢谢!!