gpt4 book ai didi

ios - 无法为 drawInRect 设置字体

转载 作者:行者123 更新时间:2023-11-29 10:25:53 24 4
gpt4 key购买 nike

我正在尝试在上下文中绘制文本,它起作用了。

我正在尝试为该文本设置一个 UIfont,但它不起作用。文字大小保持不变。

这是我的代码

- (UIImage *)imageByDrawingCircleOnImage:(UIImage *)image
{

UIGraphicsBeginImageContextWithOptions((image.size), NO, [[UIScreen mainScreen]scale] );

[image drawAtPoint:CGPointZero];
CGContextRef context = UIGraphicsGetCurrentContext();

UIFont *font = [UIFont systemFontOfSize:30];;//[UIFont fontWithName: @"Helvetica" size:45.0];

CGContextSetRGBStrokeColor(context, 210.0/255.0f, 0.0f, 0.0f, 1.0f);
CGContextSetRGBFillColor(context, 192.0/255.0, 0.0/255.0 , 13.0/255.0, 1.0);

NSMutableParagraphStyle *textStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
textStyle.lineBreakMode = NSLineBreakByWordWrapping;
textStyle.alignment = NSTextAlignmentRight;

NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:textStyle forKey:@"NSParagraphStyleAttributeName"];
[attributes setObject:font forKey:@"NSFontAttributeName"];

NSString *string = @"Tour de poitrine : 95 cm";

CGSize string_len =[string sizeWithAttributes:attributes];

[string drawInRect:CGRectMake(245 - string_len.width, 100.0, string_len.width , 100) withAttributes:[NSDictionary dictionaryWithDictionary:attributes]];



UIImage *retImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
return retImage;
}

谢谢亚历山大

最佳答案

不要使用字符串作为属性字典键,使用框架提供的常量。

@"NSFontAttributeName" 替换为 NSFontAttributeName 并将 @"NSParagraphStyleAttributeName" 替换为 NSParagraphStyleAttributeName

关于ios - 无法为 drawInRect 设置字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32354553/

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