2

我目前正在将文本绘制到 UIImage 上,该 UIImage 出现在 iOS 地图的 AnnotationView 中 - 自定义图标出现在某些长/纬度坐标处。效果很好。但是,我想用白色笔划来绘制这个文本(你可以称之为轮廓)。

//绘制文本并添加到 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;

}
4

2 回答 2

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];
于 2013-10-25T15:09:21.953 回答
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;

}
于 2013-02-09T22:30:39.843 回答