繁体   English   中英

使用drawInRect里面的ac方法不起作用

[英]Using drawInRect inside a c method not working

我有这个代码,在(到目前为止)简单的按钮内绘制一些文本:

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 它根本就不是我的文字。 字体是正确的,我用那里的普通字体代码进行了测试,所以不是这样。 我想也许它没有意识到我希望它把它画到它正在产生的图像上?

只需更改顺序并在绘制文本后获取图像:

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;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM