简体   繁体   English

无法为drawInRect设置字体

[英]Can't set font for drawInRect

I'm trying to draw text on a context, and it works. 我试图在上下文上绘制文本,并且它可以工作。

I'm trying to set an UIfont to this text but it doesn't work. 我正在尝试为此文本设置UIfont,但是它不起作用。 The text size remain the same. 文字大小保持不变。

Here my code 这是我的代码

- (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;
}

Thanks Alexandre 谢谢亚历山大

Do not use strings for your attributes dictionary keys, use the constants provided by the framework. 不要将字符串用作属性字典键,而应使用框架提供的常量。

Replace @"NSFontAttributeName" by NSFontAttributeName and @"NSParagraphStyleAttributeName" by NSParagraphStyleAttributeName . 更换@"NSFontAttributeName"NSFontAttributeName@"NSParagraphStyleAttributeName"NSParagraphStyleAttributeName

Replace your code by this. 以此替换您的代码。 It's working for me. 它为我工作。

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

{ {

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

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

UIFont *font = [UIFont fontWithName:  @"Chalkduster" size:20.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;


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

NSDictionary* attributes = @{NSFontAttributeName: font,
                             NSForegroundColorAttributeName: [UIColor redColor],
                             NSParagraphStyleAttributeName: textStyle
                             };


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;
}

Try this: 尝试这个:

//// General Declarations
CGContextRef context = UIGraphicsGetCurrentContext();

//// Text Drawing
CGRect textRect = CGRectMake(CGRectGetMinX(frame) + 23, CGRectGetMinY(frame) + 28, 101, 27);
{
    NSString* textContent = @"Hello, World!";
    NSMutableParagraphStyle* textStyle = NSMutableParagraphStyle.defaultParagraphStyle.mutableCopy;
    textStyle.alignment = NSTextAlignmentLeft;

    NSDictionary* textFontAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize: UIFont.labelFontSize], NSForegroundColorAttributeName: UIColor.blackColor, NSParagraphStyleAttributeName: textStyle};

    CGFloat textTextHeight = [textContent boundingRectWithSize: CGSizeMake(textRect.size.width, INFINITY)  options: NSStringDrawingUsesLineFragmentOrigin attributes: textFontAttributes context: nil].size.height;
    CGContextSaveGState(context);
    CGContextClipToRect(context, textRect);
    [textContent drawInRect: CGRectMake(CGRectGetMinX(textRect), CGRectGetMinY(textRect) + (CGRectGetHeight(textRect) - textTextHeight) / 2, CGRectGetWidth(textRect), textTextHeight) withAttributes: textFontAttributes];
    CGContextRestoreGState(context);
}

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

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