【问题标题】:Swift. UILabel text alignment迅速。 UILabel 文本对齐
【发布时间】:2014-07-24 22:02:18
【问题描述】:

我在 swift 中创建了我的 UILabel:

let label = UILabel(frame: CGRect( x: 50, y: 50, width: 100, height: 50))

设置属性似乎很容易:

label.textColor = UIColor.redColor()

如何实现像textAlignment这样的枚举类型? 在 Objective C 中是

label.textAlignment = NSTextAlignmentCenter;

但很快它似乎不起作用。

【问题讨论】:

    标签: enums uilabel swift


    【解决方案1】:

    现在是enums。

    你可以这样做:

    label.textAlignment = NSTextAlignment.center;
    

    或者,简写:

    label.textAlignment = .center;
    

    斯威夫特 3

    label.textAlignment = .center
    

    【讨论】:

    • 知道了!我的错误是我写的是 .center
    • 你能链接到文档吗?因为正如我所见,他们不得不从 ObjC 重写这些枚举。
    • 这个答案有帮助。我尝试了 .Center ,但它看起来不正确。检查枚举并使用 .Justified,它看起来更好 enum { NSTextAlignmentLeft = 0, NSTextAlignmentCenter = 1, NSTextAlignmentRight = 2, NSTextAlignmentJustified = 3, NSTextAlignmentNatural = 4, };
    【解决方案2】:

    Swift 中的枚举与 Objective-C 中的不同。

    Objective-C 中的 NSTextAlignmentCenter 在 Swift 中是 NSTextAlignment.Center

    【讨论】:

      【解决方案3】:

      供中心使用,myLabel.textAlignment = NSTextAlignment.Center

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-29
        • 2012-09-16
        • 1970-01-01
        • 2011-10-13
        • 2011-08-09
        • 1970-01-01
        • 1970-01-01
        • 2019-07-23
        相关资源
        最近更新 更多