【问题标题】:Creating Universal Padding/Inset for UITextField in xCode 6.1 (Swift)在 xCode 6.1 (Swift) 中为 UITextField 创建通用填充/插入
【发布时间】:2015-02-17 07:39:49
【问题描述】:

如何在 swift 中为文本字段 (UITextField) 创建填充?我希望它是全局范围,所以当 UIView 加载每个 Text 字段时都会应用此填充。

当前的 Swift 代码:

class MyCustomTextField: UITextField {
 // Custom Code that i can't figure out.
}

我尝试采取的步骤:

  • Apple 开发者文档,它没有讨论 inset 或 padding 覆盖
  • 搜索 Stackoverflow 并查看它们是否与 Objective-c 或 当 Xcode 变成 6.1 时,hack 被删除
  • Google,关于 Swift 填充没有任何问题(仅限 Objective-c)

谢谢你的高级。
斯威夫特初学者:)

【问题讨论】:

    标签: ios objective-c iphone xcode swift


    【解决方案1】:
    class MyCustomTextField : UITextField {
        var leftMargin : CGFloat = 10.0
    
        override func textRectForBounds(bounds: CGRect) -> CGRect {
            var newBounds = bounds
            newBounds.origin.x += leftMargin
            return newBounds
        }
    
        override func editingRectForBounds(bounds: CGRect) -> CGRect {
            var newBounds = bounds
            newBounds.origin.x += leftMargin
            return newBounds
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-27
      • 2016-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-13
      • 1970-01-01
      相关资源
      最近更新 更多