UITextField 的圆角 Xcode
rounded corner to UITextField Xcode
我有两个文本字段(用户名和密码),我想根据附件为用户名制作顶部圆角,为密码制作底部圆角。
只需创建一个 UIView
.
将您的两个文本字段放入 UIView
。
去掉UITextField
.
的边框样式
yourView.layer.cornerRadius = 10.0
yourView.clipsToBounds = true
您可以使用:
(UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii
示例:
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:textField.bounds byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.view.bounds;
maskLayer.path = maskPath.CGPath;
textField.layer.mask = maskLayer;
只需按照以下步骤操作
STEP 1:Import 你的 QuartzCore 框架 class:
#import <QuartzCore/QuartzCore.h>
STEP 2:Apply 编码
textField.layer.cornerRadius=6.0f;
textField.layer.masksToBounds=YES;
textField.layer.borderColor=[[UIColor blueColor]CGColor];
textField.layer.borderWidth= 1.0f;
or
[textField.layer setBorderColor:[[UIColor blueColor] CGColor]];
[textField.layer setBorderWidth:2.o];
[textField.layer setCornerRadius:5.0];
我认为你应该编写 CALayer 扩展。 Swift 3.x - 4.x
extension CALayer {
func addRadius(_ corners: UIRectCorner, radius: CGFloat, view: UIView) {
let mask = CAShapeLayer()
mask.bounds = view.frame
mask.position = view.center
mask.path = UIBezierPath(roundedRect: view.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)).cgPath
view.layer.mask = mask
}
func addRadius(radius: CGFloat) {
self.cornerRadius = radius
}
}
使用
class ViewController: UIViewController {
@IBOutlet var messageTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
messageTextField.layer.addRadius(radius: 8.0)
}
}
希望得到您的帮助:)
这是完整的代码示例:
导入 UIKit
class RoundedTextField: UITextField {
override func awakeFromNib() {
self.layer.cornerRadius = 10.0 //self.frame.size.height/2
self.clipsToBounds = false
super.awakeFromNib()
}
}
我有两个文本字段(用户名和密码),我想根据附件为用户名制作顶部圆角,为密码制作底部圆角。
只需创建一个 UIView
.
将您的两个文本字段放入 UIView
。
去掉UITextField
.
yourView.layer.cornerRadius = 10.0
yourView.clipsToBounds = true
您可以使用:
(UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii
示例:
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:textField.bounds byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.view.bounds;
maskLayer.path = maskPath.CGPath;
textField.layer.mask = maskLayer;
只需按照以下步骤操作
STEP 1:Import 你的 QuartzCore 框架 class:
#import <QuartzCore/QuartzCore.h>
STEP 2:Apply 编码
textField.layer.cornerRadius=6.0f;
textField.layer.masksToBounds=YES;
textField.layer.borderColor=[[UIColor blueColor]CGColor];
textField.layer.borderWidth= 1.0f;
or
[textField.layer setBorderColor:[[UIColor blueColor] CGColor]];
[textField.layer setBorderWidth:2.o];
[textField.layer setCornerRadius:5.0];
我认为你应该编写 CALayer 扩展。 Swift 3.x - 4.x
extension CALayer {
func addRadius(_ corners: UIRectCorner, radius: CGFloat, view: UIView) {
let mask = CAShapeLayer()
mask.bounds = view.frame
mask.position = view.center
mask.path = UIBezierPath(roundedRect: view.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)).cgPath
view.layer.mask = mask
}
func addRadius(radius: CGFloat) {
self.cornerRadius = radius
}
}
使用
class ViewController: UIViewController {
@IBOutlet var messageTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
messageTextField.layer.addRadius(radius: 8.0)
}
}
希望得到您的帮助:)
这是完整的代码示例:
导入 UIKit
class RoundedTextField: UITextField {
override func awakeFromNib() {
self.layer.cornerRadius = 10.0 //self.frame.size.height/2
self.clipsToBounds = false
super.awakeFromNib()
}
}