简体   繁体   English

UILabel 文本属性设置为 nil 或 "" 使 UILabel 从视图中消失(Swift / Autolayout / iOS9.1)

[英]UILabel text property when set to nil or "" makes UILabel disappear from view (Swift / Autolayout/ iOS9.1)

I am going through the Stanford Winter 2015 Swift/iOS course and while doing the assignments I run into a behavior I'd like to change.我正在学习斯坦福大学 2015 年冬季 Swift/iOS 课程,在做作业时我遇到了我想改变的行为。

I use Autolayout as described in the videos (making the display pin to leading and trailing view edges) and the Calculator app "Display" UILabel is fine with an initial value of 0 and whenever the value used to set it (a String) is non-nil and non "".我使用视频中描述的自动布局(使显示引脚指向前视图和后视图边缘)并且计算器应用程序“显示”UILabel 很好,初始值为 0 并且每当用于设置它的值(字符串)是非-nil 和非“”。

If it is either nil or "", the entire UILabel disappears.如果为 nil 或 "",则整个 UILabel 消失。 What I am trying to do is to "clear" the display whenever there is no value to display or an incorrect calculation resulted in nil.我想要做的是在没有值显示或计算不正确导致 nil 时“清除”显示。

Any tips on who to deal with this in general?关于一般谁来处理这个问题的任何提示? "Clearing" a UILabel without changing it's on-screen dimensions? “清除” UILabel 而不改变它的屏幕尺寸?


Edit (thanks Rob) The UILabel has the following constraints 1. Option-click drag-left to containing UIView, selected "leading" something (on commute to work can't check yet for exact wording. 2. Same method as (1) except that the drag is to the right edge and selecting "trailing" 3. Option click-drag up to top of view, select "vertical" menu option. 4. Same as (3) except that drag is to a UIButton underneath the UILabel on the GUI.编辑(感谢 Rob) UILabel 具有以下约束 1. Option-click 向左拖动以包含 UIView,选择“领先”的东西(上班途中无法检查确切的措辞。2. 与(1)相同的方法除了拖动到右边缘并选择“拖尾” 3. 选项单击并拖动到视图顶部,选择“垂直”菜单选项。 4. 与(3)相同,只是拖动到 UILabel 下方的 UIButton在图形用户界面上。

With those settings, the label when it contains a number is always visible and (if understand, will color it to verify) stretches across the screen even if the text doesn't.使用这些设置,标签在包含数字时始终可见,并且(如果理解,将对其着色以验证)即使文本没有在屏幕上延伸。

The layout looks correct in profile and landscape as long as content of UILabel is not empty.只要 UILabel 的内容不为空,布局在配置文件和横向中看起来是正确的。 If empty, it seems to "shrink to fit" so much that the buttons below get moved up towards the top.如果为空,它似乎“缩小以适应”太多以至于下面的按钮向上移动到顶部。

I'm a C++ dev since mid 90s but I have little UI experience and no more than a couple weeks experience in iOS/Swift development.我是 90 年代中期以来的 C++ 开发人员,但我几乎没有 UI 经验,而且在 iOS/Swift 开发方面的经验不超过几周。

Thanks!谢谢!

You can always give the UILabel a min width and min height or constraints that holds the left and right side of the label.你总是可以给 UILabel 一个最小宽度和最小高度或包含标签左侧和右侧的约束。 That should keep the label from changing it's dimensions to zero.这应该可以防止标签将其尺寸更改为零。

就我而言,我最终把它变成了一个空格,所以“”而不是“”

Use a custom UILabel class assigned in Interface Builder >> Identity inspector >> Custom Class >> Class to override UILabel intrinsic content size.使用在Interface Builder >> Identity inspector >> Custom Class >> Class分配的自定义UILabel类来覆盖UILabel内在内容大小。

No need to create any superfluous auto-layout constraints.无需创建任何多余的自动布局约束。

Swift:迅速:

class UILabelNonCompressible: UILabel
{
    private static let NonCompressibleInvisibleContent = " "

    override var intrinsicContentSize: CGSize
    {
        if /* zero-width */ text == nil ? true : text!.isEmpty
        {
            // prefer mirror-and-calculate over modify-calculate-restore due to KVO
            let doppelganger = createCopy()

            // calculate for any non-zero -height content
            doppelganger.text = UILabelNonCompressible.NonCompressibleInvisibleContent

            // override
            return doppelganger.intrinsicContentSize
        }
        else
        {
            return super.intrinsicContentSize
        }
    }
}

You will also need "How do copy for UILabel?"您还需要“如何复制 UILabel?” :

extension UILabel
{
    func createCopy() -> UILabel
    {
        let archivedData = NSKeyedArchiver.archivedData(withRootObject: self)
        return NSKeyedUnarchiver.unarchiveObject(with: archivedData) as! UILabel
    }
}

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

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