iOS, dibuja text con trazo en UIImage

Actualmente estoy dibujando text en un UIImage que aparece en un AnnotationView en un Mapa iOS: icons personalizados que aparecen en ciertas coorderadas largas / Lat. Funciona bien. Sin embargo, me gustaría dibujar este text con un trazo blanco (podría llamarlo un esbozo).

// Dibujar text y agregarlo a un UIImage iOS 5/6

+ (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; } 

NSAttributedString es definitivamente el path a seguir, especialmente si quieres que funcione en iOS7. Para su ejemplo, simplemente puede replace los dos bloques de text con lo siguiente:

 // 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]; 

Tomó más time y esfuerzo de lo que pensaba, pero en caso de que alguien por ahí esté buscando la misma solución … Aparentemente no funciona en iOS7 y se actualizará cuando encuentre la respuesta

 - (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; }