ios - 带自动布局的尺寸标签

标签 ios swift autolayout uikit

问题描述

我在 iOS 应用程序的绘图 View 左侧有一个标签。 我已经成功地将标签旋转 90 度,如下图所示,使用以下部分中复制的代码:

enter image description here

但是,一旦我将文本更改为“Speed (Km/h):”,标签就会变宽,如下所示:

enter image description here

布局

Interface Builder 中没有设置相关的约束。此处设置的唯一约束是: - 标签的垂直居中 - 绘图 View 的右侧、顶部和底部边缘粘附到 View 的右侧、顶部和底部边缘

剩下的在代码中设置

enter image description here

代码

func setup(speedArray: [Float32]) {

    //Convert speed to Km/h from m/s
    let speedArrayKMH = speedArray.map({$0*3.6})

    //Draw Chart
    //This only styles the chart (colors, user interaction, etc...
    //no code in this fuction that affects layout)
    setupSpeedChart(speedArray: speedArrayKMH)

    //
    //  Customise speed label
    //
    //Label text
    chartTitleLabel.textAlignment = .center
    //Text color
    self.chartTitleLabel.textColor = BoxTextColor
    //Rotate
    chartTitleLabel.transform = CGAffineTransform(rotationAngle: CGFloat(-Double.pi/2))
    //Constrain
    let C1 = NSLayoutConstraint(item: chartTitleLabel, attribute: .leadingMargin, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 0)
    let C2 = NSLayoutConstraint(item: chartTitleLabel, attribute: .trailingMargin, relatedBy: .equal, toItem: speedLineChart, attribute: .leading, multiplier: 1, constant: 0)

    NSLayoutConstraint.activate([C1, C2])

}

我尝试过的

我尝试了多种构造和尺寸组合,包括:

  • 修复标签的框架属性(高度和宽度)
  • 为标签的宽度和高度添加约束

似乎没有任何效果

例如,为标签宽度添加以下约束会产生此结果:

let C3 = NSLayoutConstraint(item: chartTitleLabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 45)

enter image description here

最佳答案

缺少一些关键的东西 首先你忘了使用 translateAutoReaizingMaskIntoConstraints

转到 Storyboard选择你的标签转到标签下的检查器有自动收缩将其设置为最小字体大小将大小设置为 11

然后在 View didload中做如下操作

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    myLabel.text = "speed in km"
    myLabel.textAlignment = .center
    myLabel.transform = CGAffineTransform(rotationAngle: CGFloat(-Double.pi/2))
    myLabel.translatesAutoresizingMaskIntoConstraints = false
    let c1 = NSLayoutConstraint(item: myLabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 200)
    let c2 = NSLayoutConstraint(item: myLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 60)

    let c3 = NSLayoutConstraint(item: myLabel, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: -60)
    let c4 = NSLayoutConstraint(item: myLabel, attribute: .centerY, relatedBy: .equal, toItem: myImageView, attribute: .centerY, multiplier: 1, constant: 0)

    NSLayoutConstraint.activate([c1, c2, c3, c4])

}

我尝试将文本更新为字符数比以前更多的新文本,我工作得很好

this is before changing text this is after changing the text

关于ios - 带自动布局的尺寸标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55717268/

相关文章:

ios - 使用通过 batchImport (iOS) 获得的 FCM token

ios - 在后台线程中加载 UIImage

iOS:实现简单的拖放以重新排序

ios - 数组到数据 Swift

ios - 自动布局愚蠢(我的)?

ios - 带有categoryBitMask的Sprite仍然返回 'nil'

arrays - 如何在 Swift 中生成给定大小的 1 位和 0 位的所有排列

ios - 使用 performSegue 显示导航 Controller

ios - UIScrollView 中的 UILayoutPriority 不影响任何东西

ios - 添加什么约束使 UIView 占用屏幕大小的一半