2013-05-24 7 views
2

UIView에 점선 테두리 스타일을 추가해야합니다. CGRectsetDash을 사용하고 싶지 않으므로 설정 후에 제거하지 마십시오. 이 경계 스타일로 전환 할 수 있어야합니다. 그것에 대해 어떻게 알 수 있습니까?점선으로 된 UIView

나는 방법 내가 오류 '잘못된 컨텍스트 0x0으로'

+0

현재 테두리를 추가하는 방법을 보여주기 위해 질문을 편집 할 수 있습니까? –

+0

여기에서 답변을 얻을 수도 있습니다. 비슷한 게시물 : http : //stackoverflow.com/questions/13679923/dottedline-border-around-uiview – Raj

+0

답변보기 http://stackoverflow.com/a/40512209/1757229 – sash

답변

0

이 프로젝트에 QuartzCore/QuartzCore.h 프레임 워크를 추가하고 .m 파일에 #import <QuartzCore/QuartzCore.h>을 가져올를 얻을

-(void) addBorder 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetStrokeColorWithColor(context, [[UIColor blueColor] CGColor]); 

    CGContextAddRect(context, self.bounds); 
    CGContextStrokePath(context); 
} 

을 추가해야합니다. 다음 코드

그리고 시도 :이 당신을 도와줍니다

[view.layer setBorderWidth:5.0]; 
[view.layer setBorderColor:[[UIColor colorWithPatternImage:[UIImage imageNamed:@"DottedImage.png"]] CGColor]]; 

희망.

+0

아니요 , 내보기 크기를 조정할 때 이미지가 나빠질 것입니다. –

1

다음 방법을 사용해보십시오. 내 앱 중 하나도 동일한 기능을 제공합니다. & 다음과 같은 방법으로 시도했습니다.

-(void)addDashedBorder 
{ 
    //border definitions 
    CGFloat cornerRadius = 0; 
    CGFloat borderWidth = 1; 
    NSInteger dashPattern1 = 4; 
    NSInteger dashPattern2 = 4; 
    UIColor *lineColor = [UIColor blackColor]; 

    //drawing 
    CGRect frame = view.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 
    [view.layer addSublayer:_shapeLayer]; 
    view.layer.cornerRadius = cornerRadius; 
} 
관련 문제