如何在创建圆角时添加到边框 UITextField Swift
How to add with to border UITextField Swift when when create Rounded corners
我想创建一个圆角 "Oval shape" UITextField。当 UITextField 的背景与视图
相同时,我有一个内边框没有圆角的问题,并且给 UITextField 提供了奇怪的日志
边框的外观:
图片来自MockAUp 我想达到的效果:
Swift代码:
txtfEmail.layer.cornerRadius = 26
txtfEmail.clipsToBounds = true
txtfEmail.attributedPlaceholder = NSAttributedString(string: "Email",
attributes: [NSAttributedString.Key.foregroundColor: UIColor.white.withAlphaComponent(0.5)])
电子邮件 UITextfield 检查
您应该像这样创建圆角边框:
txtfEmail.layer.cornerRadius = txtfEmail.height / 2
txtfEmail.layer.borderWidth = 1
txtfEmail.layer.borderColor = UIColor.black.cgColor
看起来边框设置为文本字段下方的视图
一种方法是创建带有嵌入式文本字段的自定义 UIView
class。
这里有一个例子,使用@IBInspectable
和@IBDesignable
让你在Storyboard设计时看到它:
import UIKit
@IBDesignable
class RoundedTextField: UIView {
@IBInspectable var placeholder: String = "" {
didSet {
textField.attributedPlaceholder = NSAttributedString(string: placeholder,
attributes: [NSAttributedString.Key.foregroundColor: UIColor.white.withAlphaComponent(0.5)])
}
}
let textField: UITextField = {
let v = UITextField()
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override func prepareForInterfaceBuilder() {
commonInit()
}
func commonInit() -> Void {
layer.borderColor = UIColor.white.cgColor
layer.borderWidth = 2
layer.masksToBounds = true
addSubview(textField)
NSLayoutConstraint.activate([
textField.topAnchor.constraint(equalTo: topAnchor, constant: 12.0),
textField.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -12.0),
textField.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20.0),
textField.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20.0),
])
}
override func layoutSubviews() {
super.layoutSubviews()
layer.cornerRadius = bounds.size.height * 0.5
}
}
情节提要/界面生成器中的结果:
试试这个对我有用
打开故事板
拖放文本字段
ctrl 单击并在视图控制器中创建一个出口并将其命名为 emailfield
然后写这段代码
@iboultet weak var emailfield: uitextfield!{
didset{
emailfield.layer.masktobounds = true
emailfield.layer.cornerradius = 26
emailfield.layer.borderwidth = 1
emailfield.backgroundcolor = whatever color you want
我想创建一个圆角 "Oval shape" UITextField。当 UITextField 的背景与视图
相同时,我有一个内边框没有圆角的问题,并且给 UITextField 提供了奇怪的日志边框的外观:
图片来自MockAUp 我想达到的效果:
Swift代码:
txtfEmail.layer.cornerRadius = 26
txtfEmail.clipsToBounds = true
txtfEmail.attributedPlaceholder = NSAttributedString(string: "Email",
attributes: [NSAttributedString.Key.foregroundColor: UIColor.white.withAlphaComponent(0.5)])
电子邮件 UITextfield 检查
您应该像这样创建圆角边框:
txtfEmail.layer.cornerRadius = txtfEmail.height / 2
txtfEmail.layer.borderWidth = 1
txtfEmail.layer.borderColor = UIColor.black.cgColor
看起来边框设置为文本字段下方的视图
一种方法是创建带有嵌入式文本字段的自定义 UIView
class。
这里有一个例子,使用@IBInspectable
和@IBDesignable
让你在Storyboard设计时看到它:
import UIKit
@IBDesignable
class RoundedTextField: UIView {
@IBInspectable var placeholder: String = "" {
didSet {
textField.attributedPlaceholder = NSAttributedString(string: placeholder,
attributes: [NSAttributedString.Key.foregroundColor: UIColor.white.withAlphaComponent(0.5)])
}
}
let textField: UITextField = {
let v = UITextField()
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override func prepareForInterfaceBuilder() {
commonInit()
}
func commonInit() -> Void {
layer.borderColor = UIColor.white.cgColor
layer.borderWidth = 2
layer.masksToBounds = true
addSubview(textField)
NSLayoutConstraint.activate([
textField.topAnchor.constraint(equalTo: topAnchor, constant: 12.0),
textField.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -12.0),
textField.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20.0),
textField.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20.0),
])
}
override func layoutSubviews() {
super.layoutSubviews()
layer.cornerRadius = bounds.size.height * 0.5
}
}
情节提要/界面生成器中的结果:
试试这个对我有用 打开故事板 拖放文本字段 ctrl 单击并在视图控制器中创建一个出口并将其命名为 emailfield 然后写这段代码
@iboultet weak var emailfield: uitextfield!{
didset{
emailfield.layer.masktobounds = true
emailfield.layer.cornerradius = 26
emailfield.layer.borderwidth = 1
emailfield.backgroundcolor = whatever color you want