简体   繁体   English

Swift:动态调整标签大小的宽度限制

[英]Swift: Width constraints for dynamically resizable label

Question regarding Auto Contraints and a dynamically resizable label. 有关自动约束和可动态调整大小的标签的问题。

I have a timer label that initally displays 00:00.00 (mins, seconds, milliseconds). 我有一个计时器标签,最初显示00:00.00(分钟,秒,毫秒)。 When this timer reaches one hour or more I wish to resize the label to 00:00:00.00 while preserving the .Centerx constraint. 当此计时器达到一小时或更长时间时,我希望将标签的大小调整为00:00:00.00,同时保留.Centerx约束。 Text justification on the label is set to Left so that the counting of numbers does not 'jitter' the text as it re-justifies to the different sizes of text. 标签上的文本对齐方式设置为“左”,以便数字计数不会“抖动”文本,因为它会重新调整为不同大小的文本。

I have my constraints set as the following: 我的约束条件如下:

初始约束

Heres how the timer looks initially or when only counting minutes (up to 59:59:59) 这是计时器最初或仅计时分钟时的样子(最多59:59:59) 分钟视图

And heres how it should look when Hours are involved in the count. 当计数涉及小时数时,它的外观应该如此。

小时视图

Now the issue is that when hours are involved the width is dynamically resized (due to greater than constraint) and this causes the timer label to jitter as the different milliseconds are counted. 现在的问题是,当涉及小时数时,宽度会动态调整大小(由于大于约束),这会导致计时器标签在计数不同毫秒时发生抖动。

I desire to resize the label when counting hours to a width of 150 and back to 110 when hours are not counting - all while preserving the CenterX constant. 我希望在将小时数计数时将标签的大小调整为150的宽度,而在不计算小时数时调整为110的宽度-所有这些都保留了CenterX常数。

I have tried to programatically re-size the label when the count is in hours as so: 我尝试以编程方式以小时为单位重新调整标签大小:

let widthConstraint = NSLayoutConstraint(item: TimerLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.GreaterThanOrEqual, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 150)

 if Double(hours) != 0{
      TimerLabel.text = "\(strHours):\(strMinutes):\(strSeconds).\(strFraction)"

      TimerLabel.addConstraint(widthConstraint)
 }

Now in my IBAction func for the reset button I also have: 现在,在我的IBAction函数中,有重置按钮,我也有:

let widthConstraint2 = NSLayoutConstraint(item: TimerLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.GreaterThanOrEqual, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 110)

 TimerLabel.removeConstraint(widthConstraint)
 TimerLabel.addConstraint(widthConstraint2)

However once the timer resets the label appears as follows: 但是,一旦计时器重置,标签将显示如下:

在此处输入图片说明

The text is no longer centre in the view. 文本不再在视图中居中。 I have also tried programming a width constraint for .CenterX in the reset function and that also produces no change in the reset label position. 我也尝试过在reset函数中为.CenterX设置宽度约束,并且该约束在重置标签位置上也不会产生任何变化。

Is there an easier way to do this Via Interface Builder? 是否有通过Interface Builder进行此操作的更简单方法? Any ideas? 有任何想法吗?

Thanks 谢谢

Try making the text label wider than it is and then centering the text. 尝试使文本标签宽于其宽度,然后将文本居中。 When more characters are added it will preserve the centre positioning. 添加更多字符时,它将保留中心位置。

As Fonix suggested the solution was to use a wixed width font which is easy to do in iOS9. 正如Fonix所建议的那样,解决方案是使用在iOS9中易于实现的粗细字体。

    let originalFont = UIFont.systemFontOfSize(26)
    let originalFontDescriptor = originalFont.fontDescriptor()

    let fontDescriptorFeatureSettings = [
        [
            UIFontFeatureTypeIdentifierKey: kNumberSpacingType,
            UIFontFeatureSelectorIdentifierKey: kMonospacedNumbersSelector
        ]
    ]

    let fontDescriptorAttributes = [UIFontDescriptorFeatureSettingsAttribute: fontDescriptorFeatureSettings]
    let fontDescriptor = originalFontDescriptor.fontDescriptorByAddingAttributes(fontDescriptorAttributes)
    let font = UIFont(descriptor: fontDescriptor, size: 0)

    TimerLabel.font = font

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM