![](/img/trans.png)
[英]How to create Gradient border with rounded corners at UIView in Swift 4
[英]How to add with to border UITextField Swift when when create Rounded corners
我想创建一个圆角“椭圆形”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 inspectere
您应该像这样创建圆角边框:
txtfEmail.layer.cornerRadius = txtfEmail.height / 2
txtfEmail.layer.borderWidth = 1
txtfEmail.layer.borderColor = UIColor.black.cgColor
看起来边框设置为文本字段下的视图
一种方法是创建具有嵌入文本字段的自定义UIView
类。
这是一个例子,使用@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
}
}
Storyboard / Interface Builder中的结果:
尝试这个它适用于我打开故事板拖放文本域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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.