cao*_*gin 2 fonts objective-c uiimage stroke ios
我目前正在将文本绘制到UIImage上,该UIImage出现在iOS地图中的AnnotationView中 - 自定义图标出现在某些长/纬度坐标处.效果很好.但是,我想用白色笔划绘制这个文本(你可以称之为大纲).
+ (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;
}
Run Code Online (Sandbox Code Playgroud)
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];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10836 次 |
| 最近记录: |