我目前正在UIImage上绘制文字,该UIImage出现在iOS地图的AnnotationView中 - 自定义图标出现在某些长/纬度坐标上。效果很好。但是,我想用白色笔划绘制这个文本(你可以称之为大纲)。
// Draw text and add to a 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;
}
答案 0 :(得分: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];
答案 1 :(得分: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;
}