gpt4 book ai didi

ios - 更改 UIImage 或 UIGraphics 的文本颜色

转载 作者:行者123 更新时间:2023-11-29 10:40:45 26 4
gpt4 key购买 nike

我将实现用于将白色数字添加到正在绘制到 ios 应用程序上的 UIImage 的箭头上的模块。执行时,显示数字上只设置了黑色。我已经添加了

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

但无济于事。你能告诉我这是更好的方法吗?

下面是我的作品

-(UIImage*) drawText:(NSString*) text
inImage:(UIImage*) image
atPoint:(CGPoint) point
{
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height);
[[UIColor whiteColor] set];

UIFont *font = [UIFont boldSystemFontOfSize:24];

if([text respondsToSelector:@selector(drawInRect:withAttributes:)])
{
//IOS 7
NSDictionary *att = @{NSFontAttributeName:font};
[text drawInRect:rect withAttributes:att];
}
else
{
[text drawInRect:CGRectIntegral(rect) withFont:font];

}
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [UIColor whiteColor].CGColor);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;
}

最佳答案

根据您想回到多远的时间,选择这 3 种解决方案中的任何一种。6+ 和 7+ 版本使用 NSForegroundColorAttributeName 来选择颜色。这些是首选方法。

iOS 7+

// (add the color to the attribute)
NSDictionary *att = @{NSFontAttributeName :font,
NSForegroundColorAttributeName:[UIColor whiteColor]};
[@"test" drawInRect:rect withAttributes:att];

iOS 6+(也适用于 iOS 7)

// (add the color to the attribute, using NSAttributedString)
NSDictionary *att = @{NSFontAttributeName :font,
NSForegroundColorAttributeName:[UIColor whiteColor]};
NSAttributedString * text = [[NSAttributedString alloc] initWithString:@"test"
attributes:att];
[text drawInRect:rect];

iOS 5(不适用于 iOS 7)

// No attributes involved
[[UIColor whiteColor] set];
[@"test" drawInRect:CGRectIntegral(rect) withFont:font];

关于ios - 更改 UIImage 或 UIGraphics 的文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24665806/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com