2011-10-18 4 views
3

NSString과 테두리를 이미 가지고있는 UIImage에 그려야합니다. NSString을 UIImage로 그리는 메소드를 찾았지만 제공하는 이미지를 그려야합니다.iOS : UIImage에서 NSString 및 테두리 그리기

-(UIImage *)imageFromText:(NSString *)text 
{ 
    // set the font type and size 
    UIFont *font = [UIFont systemFontOfSize:20.0]; 
    CGSize size = [text sizeWithFont:font]; 

    // check if UIGraphicsBeginImageContextWithOptions is available (iOS is 4.0+) 
    if (UIGraphicsBeginImageContextWithOptions != NULL) 
     UIGraphicsBeginImageContextWithOptions(size,NO,0.0); 
    else 
     // iOS is < 4.0 
     UIGraphicsBeginImageContext(size); 

    // optional: add a shadow, to avoid clipping the shadow you should make the context size bigger 
    // 
    // CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    // CGContextSetShadowWithColor(ctx, CGSizeMake(1.0, 1.0), 5.0, [[UIColor grayColor] CGColor]); 

    // draw in context, you can use also drawInRect:withFont: 
    [text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font]; 

    // transfer image 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext();  

    return image; 
} 

이 방법을 수정하여 배경 이미지를 제공하고 테두리를 추가하는 방법은 무엇입니까?

답변

5

당신은 UIImageView.layer.delegate을 설정하고 같은 것을 사용할 수 있습니다 Add text to CALayer

경계에서

- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx { 
    CGContextSetFillColorWithColor(ctx, [[UIColor darkTextColor] CGColor]); 

    UIGraphicsPushContext(ctx); 

    [word drawAtPoint:CGPointMake(30.0f, 30.0f) 
      forWidth:200.0f 
      withFont:[UIFont boldSystemFontOfSize:32] 
     lineBreakMode:UILineBreakModeClip]; 

    UIGraphicsPopContext(); 
} 

코드 CALayer 속성을 사용하면됩니다.

imageview.layer.borderColor = [UIColor blackColor].CGColor; 
imageview.sublayer.borderWidth = 2.0; 
+0

큰를 확인 테두리있는 UIImage
에있는 NSString 및 테두리 그리기,하지만 결국 내가있는 UIImage를 대체 할있는 UIButton를 사용, 그것은 배경 Image를 가지고 있으며 Label.text를 추가 할 수 있습니다. – meadlai

1

이 기능을 사용 CGContextSetRGBStrokeColor

-(UIImage *)imageFromText:(NSString *)text 
{ 
// set the font type and size 
UIFont *font = [UIFont systemFontOfSize:20.0]; 
CGSize size = [text sizeWithFont:font]; 

// check if UIGraphicsBeginImageContextWithOptions is available (iOS is 4.0+) 
if (UIGraphicsBeginImageContextWithOptions != NULL) 
    UIGraphicsBeginImageContextWithOptions(size,NO,0.0); 
else 
    // iOS is < 4.0 
    UIGraphicsBeginImageContext(size); 

// optional: add a shadow, to avoid clipping the shadow you should make the context size bigger 
// 
CGContextRef ctx = UIGraphicsGetCurrentContext(); 
CGContextSetShadowWithColor(ctx, CGSizeMake(1.0, 1.0), 5.0, [[UIColor brownColor] CGColor]); 

// draw in context, you can use also drawInRect:withFont: 
[text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font]; 

//CGImageRef cimg = UIGraphicsGetCurrentContext();  

// transfer image 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height); 
[image drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0]; 

//CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetRGBStrokeColor(ctx, 2.0, 3.5, 5.0, 1.0); 
CGContextStrokeRect(ctx, rect); 
UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext();  

return testImg; 
} 

+0

더 나은 질문을 올리면 더 나은 답변을받을 수 있습니다. http://stackoverflow.com/questions/how-to-ask –