別の画像の上に配置するテキストの画像を作成しようとしています。テキストには、白の塗りつぶしと黒のストロークのアウトラインが必要です。私はobjective-Cを使用していますが、これはiPhone用です。テキストを表示させることができます。ただし、ストロークと塗りつぶしの色を変更する方法を理解するために継ぎ目ができません。
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);
しかし、両方のスニペットを追加すると、黒いストロークが得られますが、フォントの色は透明です...
解決済み:「Brane」からの少しの助けのおかげで:
:の前にこのコードを追加する必要がありましたdrawAtPoint
:
CGFloat fontColor[4] = {255,255,255,1};
CGContextSetFillColor(UIGraphicsGetCurrentContext(), fontColor);
CGContextSetTextDrawingMode(UIGraphicsGetCurrentContext(), kCGTextFillStroke);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2);
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor blackColor] CGColor]);