我想要创建圆角文本字段的边框点线。我成功地创建了文本字段的边框,但边界文本字段的圆角存在一些问题,我需要带有虚线的圆角文本字段。这是我的密码。
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];发布于 2015-11-30 08:31:58
以下是删除gray color边界的UITextField解决方案。
问题

溶液

为此,您需要使用以下一行代码Just change the border style
_firstName.borderStyle = UITextBorderStyleNone;
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;
}使用下面的代码调用此函数。
_firstName.borderStyle = UITextBorderStyleNone;
[self drawDashedBorderAroundView:_firstName];希望这能帮上忙。
发布于 2015-11-30 10:12:10
尝试下面的编码
- (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;
}请看下面的资料
https://stackoverflow.com/questions/33993847
复制相似问题