我想创建一个放在另一个图像上的文本图像。文字需要有白色填充和黑色笔画轮廓。我使用的是Objective-C,它适用于iPhone。我可以让文字出现;然而,我不能缝合以找出如何获得笔画并填充颜色来改变。如何在iPhone的UIGraphicsImageContext中使用属性(特别是笔画和填充颜色)绘制文本
self.bigImage
是一个指向我xib中的UIImage的指针。
这里是我的代码:(此代码的工作......我希望它有一个黑色的中风但是白色填充)
- (IBAction)submitBtn:(id)sender {
UIFont *myfont = [UIFont fontWithName:@"myfont" size:32];
UIImage *textImage = [self imageFromText:@"teststring" font:myfont];
CGSize finalSize = [self.bigImage.image size];
CGSize textSize = [textImage size];
UIGraphicsBeginImageContext(finalSize);
[self.bigImage.image drawInRect:CGRectMake(0,0, finalSize.width, finalSize.height)];
[textImage drawInRect:CGRectMake(0, 0, textSize.width, textSize.height)];
UIImage *newImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.bigImage.image = newImg;
}
-(UIImage *)imageFromText:(NSString *)text font:(UIFont *)font {
CGSize size = [text sizeWithFont:font];
// check if UIGraphicsBeginImageContextWithOptions is available (iOS is 4.0+)
if (UIGraphicsBeginImageContextWithOptions != NULL)
UIGraphicsBeginImageContextWithOptions(size,NO,0.0);
else
// iOS is < 4.0
UIGraphicsBeginImageContext(size);
[text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font];
// transfer image
UIImage *Image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return Image;
}
我可以得到它来呈现一个或另一个。如果我添加此代码,我得到一个黑色的行程:
CGContextSetTextDrawingMode(UIGraphicsGetCurrentContext(), kCGTextStroke);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2);
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor blackColor] CGColor]);
如果我添加此代码,我得到一个白色字体
CGFloat fontColor[4] = {255,255,255,1};
CGContextSetFillColor(UIGraphicsGetCurrentContext(), fontColor);
,但如果我同时添加snipets我得到的黑色中风但字体颜色是透明...
解决:得益于“布朗康”一点帮助:
我需要drawAtPoint
之前添加的这段代码:
CGFloat fontColor[4] = {255,255,255,1};
CGContextSetFillColor(UIGraphicsGetCurrentContext(), fontColor);
CGContextSetTextDrawingMode(UIGraphicsGetCurrentContext(), kCGTextFillStroke);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2);
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor blackColor] CGColor]);
我不是在寻找使背景颜色为白色,但字体本身白加黑中风。我希望背景的颜色透明。 – werdsackjon 2013-02-25 20:06:13