6

別の画像の上に配置するテキストの画像を作成しようとしています。テキストには、白の塗りつぶしと黒のストロークのアウトラインが必要です。私は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]);
4

3 に答える 3

3

解決すべき2つの問題。

まず、背景を描くために、背景を描かないので頼りにできませんdrawAtPoint:withFont:

CGContextFillRectを使用して、背景色を描画します。

[[UIColor whiteColor] set];
CGContextFillRect( UIGraphicsGetCurrentContext(), CGRectMake( 0, 0, size.width, size.height )

次に、を使用してストロークの色を設定する必要があります

[[UIColor blackColor] set];

電話する前に[text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font];

于 2013-02-25T19:36:43.193 に答える
3

Brane の投稿を修正してこれを修正しました...前にこのコードのスニペットを追加する必要がありましたdrawAtPoint...

CGFloat fontColor[4] = {255,255,255,1};
CGContextSetFillColor(UIGraphicsGetCurrentContext(), fontColor);
CGContextSetTextDrawingMode(UIGraphicsGetCurrentContext(), kCGTextFillStroke);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2);
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor blackColor] CGColor]);
于 2013-02-25T21:54:16.357 に答える