【问题标题】:UITextField Standard Style programmaticallyUITextField 标准样式以编程方式
【发布时间】:2019-01-23 03:31:25
【问题描述】:

当我使用故事板创建UITextField 时,它看起来像下图。但是,当我以编程方式创建 UITextField 时,它绝对没有样式。我知道我可以将自定义样式应用到文本字段,但是在以编程方式创建文本字段时,有没有一种简单的方法来获得这种标准样式?

【问题讨论】:

  • 当您使用 Storyboard 创建 UITextField 时,它会自动添加到 view,并带有您在属性检查器中设置的属性。如果您在代码中创建它,您还需要从代码中添加它的样式。

标签: ios iphone swift uitextfield uikit


【解决方案1】:

类似这样的:

let t = UITextField()
t.frame = CGRect(x: 10, y: 20, width: self.view.frame.width - 20, height: 40)
t.layer.cornerRadius = 5
t.layer.borderColor = UIColor.lightGray.cgColor
t.layer.borderWidth = 1
t.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: t.frame.height))
t.leftViewMode = .always
t.rightView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: t.frame.height))
t.rightViewMode = .always
t.clearButtonMode = .whileEditing
self.view.addSubview(t)

编辑:

在下面的某个地方添加这个类,以便它易于/全局访问。

class StyledTextField: UITextField { 
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.layer.cornerRadius = 5
        self.layer.borderColor = UIColor.lightGray.cgColor
        self.layer.borderWidth = 1
        self.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: t.frame.height))
        self.leftViewMode = .always
        self.rightView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: t.frame.height))
        self.rightViewMode = .always
        self.clearButtonMode = .whileEditing
    }
}

然后你可以从任何你想要的地方调用UITextField,如下所示

let t = StyledTextField()
t.frame = CGRect(x: 10, y: 20, width: self.view.frame.width - 20, height: 40)
self.view.addSubview(t)

编辑 2:

使用UIEdgeInsets 获得所有四个边的填充。

class StyledTextField: UITextField { 
    let insetConstant = UIEdgeInsets(top: 4, left: 10, bottom: 4, right: 10)

    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return UIEdgeInsetsInsetRect(bounds, insetConstant)
    }

    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return UIEdgeInsetsInsetRect(bounds, insetConstant)
    }

    override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
        return UIEdgeInsetsInsetRect(bounds, insetConstant)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)

        self.layer.cornerRadius = 5
        self.layer.borderColor = UIColor(white: 2/3, alpha: 0.5).cgColor
        self.layer.borderWidth = 1
        self.clearButtonMode = .whileEditing
        self.keyboardType = UIKeyboardType.decimalPad
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

【讨论】:

  • 谢谢,这行得通!但是是否有可能以某种方式创建一个只使用默认样式的 UITextField,而不尝试复制它?
  • 嗨@hoan,是的,你绝对可以做到!人们建议的其他答案使您可以轻松地做到这一点。我会更新我的,告诉你如何
  • @hoan 你有没有读过我的答案,这是在编辑这个答案之前发布的,以复制我的答案并说了完全相同的内容以及更多内容?
  • @RakeshaShastri 我做到了,您的子类化解释很棒。我认为我在 cmets 中提出的问题被误解了。我不想知道如何继承UITextField,因为你已经解释过了。我想知道是否有办法获得 Apple 提供的默认样式,而不自己创建任何样式代码。因为在使用 Storyboard 时,您也不必设置 UITextField 的样式即可获得默认样式。
  • @hoan 我明白了。我现在知道了。您想要完全不同的参数来重现故事板创建的效果吗?如果答案再次被稍微编辑,我会删除我的反对票,因为除非它被编辑,否则我不能再次投票。 我的错 对不起
【解决方案2】:

您可以将 borderStyle 添加为 .roundedRect 并使用 白色背景。像这样的:

class MyCustmUITextField: UITextField { 
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.borderStyle = .roundedRect
        self.backgroundColor = .white
    } 
}

【讨论】:

    【解决方案3】:

    您可以从 UITextField 创建一个子类作为您的自定义文本字段。 在此之后,您只需要实例化您的自定义类,然后您就可以开始了。 像这样的:

    class MyCustmUITextField: UITextField { 
        override init(frame: CGRect) {
            super.init(frame: frame)
            self.layer.cornerRadius = 5
            self.layer.borderColor = UIColor.black
            self.font = UIFont.systemFont(ofSize: 20)
            self.adjustsFontSizeToFitWidth = true
            // then add whatever styles you wish
        } 
    }
    

    从那里你只需要创建一个 MyCustomUITextField 的新实例

    【讨论】:

      猜你喜欢
      • 2011-03-10
      • 2011-10-22
      • 2014-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多