2013-02-09 2 views
2

현재 iOS Map의 AnnotationView에 나타나는 UIImage에 텍스트를 그리는 중입니다. 특정 long/lat 좌표에 나타나는 맞춤 아이콘입니다. 잘 작동합니다. 그러나이 텍스트를 흰색 선으로 그립니다 (윤곽선이라고도 함). // Draw text and add to a UIImage iOS 5/6iOS, UIImage에서 획이있는 텍스트 그리기

+ (UIImage*)drawText:(NSString*)string inImage:(UIImage*)image atPoint:(CGPoint)point { 

    UIFont *font = [UIFont boldSystemFontOfSize:12]; 
    UIGraphicsBeginImageContext(image.size); 
    [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)]; 

    CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height); 
    [[UIColor whiteColor] set]; 
    [string drawInRect:CGRectIntegral(rect) withFont:font]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return newImage; 

} 
+0

이것은 실제 질문이 아니므로 개선하시기 바랍니다. – Pascal

+0

그럼, 질문을 적절하게 업데이트하면 가까운 투표를 취소 할 수 있습니다. ;) – Pascal

답변

8

NSAttributedString은 확실히 iOS7에서 작동하고 싶다면 갈 수있는 방법입니다. 예를 들어 두 텍스트 블록을 다음으로 바꿀 수 있습니다.

// draw stroke 
NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init]; 
[attributes setObject:font forKey:NSFontAttributeName]; 
[attributes setObject:[NSNumber numberWithFloat:4] forKey:NSStrokeWidthAttributeName]; 
[attributes setObject:[UIColor whiteColor] forKey:NSStrokeColorAttributeName]; 
[self.text drawInRect:rect withAttributes:attributes]; 

// draw fill 
[attributes removeObjectForKey:NSStrokeWidthAttributeName]; 
[attributes removeObjectForKey:NSStrokeColorAttributeName]; 
[attributes setObject:[UIColor blackColor] forKey:NSForegroundColorAttributeName]; 
[self.text drawInRect:rect withAttributes:attributes]; 
+0

Thx, 정말 멋진 작품! – sabiland

+0

둘 다 drawInRect 호출에서 둘 다 그릴 수 없습니까? – jjxtra

3

내가 생각했던 것보다 더 많은 시간과 노력을했다지만 경우에 거기에 사람이 분명히 iOS7에에서 일하고 난 찾을 때 업데이트하지 않습니다 ... 같은 솔루션을 찾고있다 답변

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    UIImage *backgroundImage = [UIImage imageNamed:@"abstract_art_masterpiece_b.jpg"]; 
    UIImage *textWithStrokeImage = [RJViewController drawTextWithStroke:@"unit 111"]; 
    UIImage *image = [RJViewController placeImage:textWithStrokeImage onImage:backgroundImage]; 

    self.imageView.image = image; 
} 

+ (UIImage*)placeImage:(UIImage*)image1 onImage:(UIImage*)image2 { 

    CGSize size = image2.size; 

    if ([UIScreen instancesRespondToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2.0f) { 
     UIGraphicsBeginImageContextWithOptions(size, NO, 2.0f); 
    } else { 
     UIGraphicsBeginImageContext(size); 
    } 

    [image2 drawAtPoint:CGPointMake(0, 0)]; 
    [image1 drawAtPoint:CGPointMake(0, 0)]; 

    UIImage* result = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return result; 
} 

+ (UIImage*)drawTextWithStroke:(NSString*)string { 

    // set rect, size, font 

    CGRect rect = CGRectMake(0, 0, 88, 24); 
    CGSize size = CGSizeMake(rect.size.width, rect.size.height); 
    UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:12]; 

    // retina display, double resolution 

    if ([UIScreen instancesRespondToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2.0f) { 
     UIGraphicsBeginImageContextWithOptions(size, NO, 2.0f); 
    } else { 
     UIGraphicsBeginImageContext(size); 
    } 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // draw stroke 

    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor); 
    CGContextSetLineWidth(context, 4.0); 
    CGContextSetTextDrawingMode(context, kCGTextStroke); 
    [string drawInRect:CGRectIntegral(rect) withFont:font]; 

    // draw fill 

    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor); 
    CGContextSetTextDrawingMode(context, kCGTextFill); 
    [string drawInRect:CGRectIntegral(rect) withFont:font]; 

    // convert to image and return 

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 

} 
+0

은 iOS 7에 대한 답변을 추가했습니다. – MobileVet

+1

이것은 정말로 훌륭합니다. 이 문제에 대한 여러 가지 다른 해결책을 찾았지만 이처럼 이미지 품질을 유지하는 것은 없습니다. 소품이 caoimghgin 또는 MobileVet에 속하는지 잘 모르겠지만 잘하고 잘해 줘서 고맙습니다. 이것은 100x를 멋진 텍스트 - 이미지 솔루션으로 기권해야합니다. – David

+0

,이 대답이나 받아 들여지는 것이 가장 잘 작동 했습니까? – ChuckKelly