首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >边界TextField

边界TextField
EN

Stack Overflow用户
提问于 2015-11-30 07:57:54
回答 2查看 643关注 0票数 3

我想要创建圆角文本字段的边框点线。我成功地创建了文本字段的边框,但边界文本字段的圆角存在一些问题,我需要带有虚线的圆角文本字段。这是我的密码。

代码语言:javascript
复制
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];
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-30 08:31:58

以下是删除gray color边界的UITextField解决方案。

问题

溶液

为此,您需要使用以下一行代码Just change the border style

_firstName.borderStyle = UITextBorderStyleNone;

代码语言:javascript
复制
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;

更新

圆角的使用以下函数.

代码语言:javascript
复制
- (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;
}

使用下面的代码调用此函数。

代码语言:javascript
复制
_firstName.borderStyle = UITextBorderStyleNone;
[self drawDashedBorderAroundView:_firstName];

希望这能帮上忙。

票数 1
EN

Stack Overflow用户

发布于 2015-11-30 10:12:10

尝试下面的编码

代码语言:javascript
复制
- (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;
}

请看下面的资料

UITextField Dotted Border with Rounded Corner

Dotted Line UITextField

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33993847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档