0

(これまでのところ)単純なボタン内にテキストを描画するために、次のコードがあります。

UIImage *buttonImage(CGRect bounds, UIColor *fillColor, NSString *title) {

    UIGraphicsBeginImageContextWithOptions(bounds.size, NO, 0);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, fillColor.CGColor);
    CGContextFillRect(context, bounds);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);

    [title drawInRect:bounds withFont:PanelButtonFont];

    UIGraphicsEndImageContext();

    return image;
}

問題の部分はdrawInRect. それは単に私のテキストを描画していません。フォントは正しく、通常のフォント コードでこれをテストしたので、そうではありません。制作している画像に描いてもらいたいのに気付いていないのではないでしょうか?

4

1 に答える 1

2

テキストを描画した後、順序を変更して画像を取得するだけです。

UIImage *buttonImage(CGRect bounds, UIColor *fillColor, NSString *title) {

UIGraphicsBeginImageContextWithOptions(bounds.size, NO, 0);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, fillColor.CGColor);
CGContextFillRect(context, bounds);

CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);

[title drawInRect:bounds withFont:PanelButtonFont];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return image;

}

于 2013-05-03T10:02:27.073 に答える