我想创建圆角文本字段的边框点线。我成功创建了一个文本字段的边框,但我有一些边框文本字体圆角的问题。我需要带有虚线的圆角文本字段。这是我的代码。
firstName.layer.borderColor =[UIColor clearColor].CGColor;
border = [CAShapeLayer layer];
border.strokeColor = [UIColor blueColor].CGColor;
border.fillColor = nil;
border.lineDashPattern = @[@4, @2];
border.path = [UIBezierPath bezierPathWithRect:firstName.bounds].CGPath;
border.frame = firstName.bounds;
[firstName.layer addSublayer:border];
答案 0 :(得分:1)
以下是移除gray color
的{{1}}边框的解决方案。
<强>问题强>
<强>解决方案强>
为此,您需要使用以下一行代码UITextField
_firstName.borderStyle = UITextBorderStyleNone;
Just change the border style
<强>更新强>
对于圆角使用以下功能。
CAShapeLayer *border = [CAShapeLayer layer];
border.strokeColor = [UIColor gray].CGColor;
border.fillColor = nil;
border.lineDashPattern = @[@4, @2];
border.path = [UIBezierPath bezierPathWithRect:_firstName.bounds].CGPath;
border.frame = _firstName.bounds;
[_firstName.layer addSublayer:border];
_firstName.borderStyle = UITextBorderStyleNone;
使用以下代码调用此函数。
- (void)drawDashedBorderAroundView:(UIView *)v
{
//border definitions
CGFloat cornerRadius = 10;
CGFloat borderWidth = 2;
NSInteger dashPattern1 = 8;
NSInteger dashPattern2 = 8;
UIColor *lineColor = [UIColor orangeColor];
//drawing
CGRect frame = v.bounds;
CAShapeLayer *_shapeLayer = [CAShapeLayer layer];
//creating a path
CGMutablePathRef path = CGPathCreateMutable();
//drawing a border around a view
CGPathMoveToPoint(path, NULL, 0, frame.size.height - cornerRadius);
CGPathAddLineToPoint(path, NULL, 0, cornerRadius);
CGPathAddArc(path, NULL, cornerRadius, cornerRadius, cornerRadius, M_PI, -M_PI_2, NO);
CGPathAddLineToPoint(path, NULL, frame.size.width - cornerRadius, 0);
CGPathAddArc(path, NULL, frame.size.width - cornerRadius, cornerRadius, cornerRadius, -M_PI_2, 0, NO);
CGPathAddLineToPoint(path, NULL, frame.size.width, frame.size.height - cornerRadius);
CGPathAddArc(path, NULL, frame.size.width - cornerRadius, frame.size.height - cornerRadius, cornerRadius, 0, M_PI_2, NO);
CGPathAddLineToPoint(path, NULL, cornerRadius, frame.size.height);
CGPathAddArc(path, NULL, cornerRadius, frame.size.height - cornerRadius, cornerRadius, M_PI_2, M_PI, NO);
//path is set as the _shapeLayer object's path
_shapeLayer.path = path;
CGPathRelease(path);
_shapeLayer.backgroundColor = [[UIColor clearColor] CGColor];
_shapeLayer.frame = frame;
_shapeLayer.masksToBounds = NO;
[_shapeLayer setValue:[NSNumber numberWithBool:NO] forKey:@"isCircle"];
_shapeLayer.fillColor = [[UIColor clearColor] CGColor];
_shapeLayer.strokeColor = [lineColor CGColor];
_shapeLayer.lineWidth = borderWidth;
_shapeLayer.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:dashPattern1], [NSNumber numberWithInt:dashPattern2], nil];
_shapeLayer.lineCap = kCALineCapRound;
//_shapeLayer is added as a sublayer of the view, the border is visible
[v.layer addSublayer:_shapeLayer];
v.layer.cornerRadius = cornerRadius;
}
愿这个有用了。
答案 1 :(得分:0)
尝试以下编码
- (void)viewDidLoad
{
[super viewDidLoad];
border = [CAShapeLayer layer];
border.strokeColor = [UIColor colorWithRed:67/255.0f green:37/255.0f blue:83/255.0f alpha:1].CGColor;
border.fillColor = nil;
border.lineDashPattern = @[@4, @2];
[firstName.layer addSublayer:border];
}
-(void)viewDidLayoutSubviews
{
border.path = [UIBezierPath bezierPathWithRect:firstName.bounds].CGPath;
border.frame = firstName.bounds;
}
请查看以下来源