如何使某些角变圆的文本字段?
How get textfield with some corners rounded?
我使用下一个代码:
- (void)setMaskByRoundingCorners:(UIRectCorner)corners withCornerRadius:(float)radius
{
UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer* shape = [[CAShapeLayer alloc] init];
[shape setPath:rounded.CGPath];
shape.frame = self.bounds;
self.layer.mask = shape;
}
但是现在我看到了这个奇怪的效果。
我在 didlayoutsubviews 之后从 viewcontroller 调用它。我这样做是为了更新主线程。
- (void)viewDidLayoutSubviews
{
[self initUIfeatures];
}
- (void)initUIfeatures
{
[authTextField setMaskByRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight|UIRectCornerBottomLeft) withCornerRadius:8.0f];
}
问题是圆角被截断。
我已经为它定制了 class。您可以使用下面的代码,任何可以根据需要在情节提要中更改的代码和您想要的角半径。
import UIKit
@IBDesignable
class CustomRoundedTextField: UITextField {
@IBInspectable var lColor: UIColor = UIColor(red: (37.0/255.0), green: (252.0/255), blue: (244.0/255.0), alpha: 1.0)
@IBInspectable var lWidth: CGFloat = 1
@IBInspectable var lCornerRadius: CGFloat = 8
@IBInspectable var sColor:UIColor = UIColor(red: (37.0/255.0), green: (252.0/255), blue: (244.0/255.0), alpha: 1.0)
@IBInspectable var TLRCorner:Bool = false
@IBInspectable var TRRCorner:Bool = false
@IBInspectable var BLRCorner:Bool = false
@IBInspectable var BRRCorner:Bool = false
@IBInspectable var XInset:CGFloat = 10
@IBInspectable var YInset:CGFloat = 10
required internal init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
}
override internal func drawRect(rect: CGRect) {
addBorderFieldRect()
}
override func textRectForBounds(bounds: CGRect) -> CGRect {
return CGRectInset(bounds, XInset, 0)
}
override func editingRectForBounds(bounds: CGRect) -> CGRect {
return CGRectInset(bounds, YInset, 0)
}
func addBorderFieldRect() {
let rectanglePath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: [
TLRCorner ? .TopLeft : [],
TRRCorner ? .TopRight : [],
BLRCorner ? .BottomLeft : [],
BRRCorner ? .BottomRight : []
], cornerRadii: CGSizeMake(lCornerRadius, lCornerRadius))
rectanglePath.closePath()
self.lColor.setFill()
rectanglePath.fill()
self.sColor.setStroke()
rectanglePath.lineWidth = lWidth
rectanglePath.stroke()
}
}
添加这些功能,
-(void)roundCorners:(UIRectCorner)corners radius:(CGFloat)radius
{
CGRect bounds = _IBtxtField.bounds;
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds
byRoundingCorners:corners
cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = bounds;
maskLayer.path = maskPath.CGPath;
_IBtxtField.layer.mask = maskLayer;
CAShapeLayer* frameLayer = [CAShapeLayer layer];
frameLayer.frame = bounds;
frameLayer.path = maskPath.CGPath;
frameLayer.strokeColor = [UIColor blackColor].CGColor;
frameLayer.fillColor = nil;
[_IBtxtField.layer addSublayer:frameLayer];
}
-(void)roundCornersRadius:(CGFloat)radius
{
[self roundCorners:(UIRectCornerTopLeft|UIRectCornerTopRight | UIRectCornerBottomLeft) radius:radius];
}
这样使用,
[self roundCornersRadius:10];
Ref
我使用下一个代码:
- (void)setMaskByRoundingCorners:(UIRectCorner)corners withCornerRadius:(float)radius
{
UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer* shape = [[CAShapeLayer alloc] init];
[shape setPath:rounded.CGPath];
shape.frame = self.bounds;
self.layer.mask = shape;
}
但是现在我看到了这个奇怪的效果。
我在 didlayoutsubviews 之后从 viewcontroller 调用它。我这样做是为了更新主线程。
- (void)viewDidLayoutSubviews
{
[self initUIfeatures];
}
- (void)initUIfeatures
{
[authTextField setMaskByRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight|UIRectCornerBottomLeft) withCornerRadius:8.0f];
}
问题是圆角被截断。
我已经为它定制了 class。您可以使用下面的代码,任何可以根据需要在情节提要中更改的代码和您想要的角半径。
import UIKit
@IBDesignable
class CustomRoundedTextField: UITextField {
@IBInspectable var lColor: UIColor = UIColor(red: (37.0/255.0), green: (252.0/255), blue: (244.0/255.0), alpha: 1.0)
@IBInspectable var lWidth: CGFloat = 1
@IBInspectable var lCornerRadius: CGFloat = 8
@IBInspectable var sColor:UIColor = UIColor(red: (37.0/255.0), green: (252.0/255), blue: (244.0/255.0), alpha: 1.0)
@IBInspectable var TLRCorner:Bool = false
@IBInspectable var TRRCorner:Bool = false
@IBInspectable var BLRCorner:Bool = false
@IBInspectable var BRRCorner:Bool = false
@IBInspectable var XInset:CGFloat = 10
@IBInspectable var YInset:CGFloat = 10
required internal init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
}
override internal func drawRect(rect: CGRect) {
addBorderFieldRect()
}
override func textRectForBounds(bounds: CGRect) -> CGRect {
return CGRectInset(bounds, XInset, 0)
}
override func editingRectForBounds(bounds: CGRect) -> CGRect {
return CGRectInset(bounds, YInset, 0)
}
func addBorderFieldRect() {
let rectanglePath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: [
TLRCorner ? .TopLeft : [],
TRRCorner ? .TopRight : [],
BLRCorner ? .BottomLeft : [],
BRRCorner ? .BottomRight : []
], cornerRadii: CGSizeMake(lCornerRadius, lCornerRadius))
rectanglePath.closePath()
self.lColor.setFill()
rectanglePath.fill()
self.sColor.setStroke()
rectanglePath.lineWidth = lWidth
rectanglePath.stroke()
}
}
添加这些功能,
-(void)roundCorners:(UIRectCorner)corners radius:(CGFloat)radius
{
CGRect bounds = _IBtxtField.bounds;
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds
byRoundingCorners:corners
cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = bounds;
maskLayer.path = maskPath.CGPath;
_IBtxtField.layer.mask = maskLayer;
CAShapeLayer* frameLayer = [CAShapeLayer layer];
frameLayer.frame = bounds;
frameLayer.path = maskPath.CGPath;
frameLayer.strokeColor = [UIColor blackColor].CGColor;
frameLayer.fillColor = nil;
[_IBtxtField.layer addSublayer:frameLayer];
}
-(void)roundCornersRadius:(CGFloat)radius
{
[self roundCorners:(UIRectCornerTopLeft|UIRectCornerTopRight | UIRectCornerBottomLeft) radius:radius];
}
这样使用,
[self roundCornersRadius:10];
Ref