简体   繁体   English

NSString drawInRect:withAttributes:绘制的文本宽度不大于iOS 7.0.3中的rect

[英]NSString drawInRect:withAttributes: is not drawing text with greater width than the rect in iOS 7.0.3

I am trying to generate a pdf inside my app. 我正在尝试在我的应用程序内部生成一个pdf文件。 To draw a string I'm calculating the size of the bounding rect for that string using boundingRectWithSize and then drawing the string inside a rect of that size. 为了绘制一个字符串,我正在使用boundingRectWithSize计算该字符串的边界rect的大小,然后在该大小的rect内部绘制该字符串。

The code works fine in iOS 7.1 and above but in iOS 7.0.3 the text is not drawn at all if it's width is more than the rectangle's width (400). 该代码在iOS 7.1及更高版本中工作正常,但在iOS 7.0.3中,如果宽度大于矩形的宽度(400),则根本不会绘制文本。 According to Apple's docs the string should have been wrapped to a new line and clipped if it cannot fit the rect, which is happening in iOS 7.1 and above, but not in iOS 7.0.3. 根据Apple的文档,该字符串应该被换行换行并在不适合rect的情况下被剪切,这在iOS 7.1及更高版本中发生,但在iOS 7.0.3中不存在。

Here is my code snippet: 这是我的代码段:

-(void)drawText:(NSString *)string
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);

    NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
 myFontForContentBold, NSFontAttributeName,
 [NSNumber numberWithFloat:1.0], NSBaselineOffsetAttributeName, nil];

    CGRect textRect = [string boundingRectWithSize:CGSizeMake(400, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:attributes context:nil]
textRect = CGRectMake(130, 80, 400, textRect.size.height);

    [string drawInRect:textRect withAttributes:attrsDictionary];
}

I am not able to figure out what the problem might be. 我不知道可能是什么问题。 Please help. 请帮忙。

Try this: 尝试这个:

-(void)drawText:(NSString *)string {
// this method must be called after a valid graphic context 
// is configured, it can be called in drawRect:, or a bitmap or pdf context is configured.
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);

    NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                     myFontForContentBold, NSFontAttributeName,
                                     [NSNumber numberWithFloat:1.0], NSBaselineOffsetAttributeName, nil];
    NSAttributedString *richText = [[NSAttributedString alloc] initWithString:string attributes:attrsDictionary];

    CGRect textRect = [richText boundingRectWithSize:CGSizeMake(400, CGFLOAT_MAX)
                                      options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                      context:nil];

    textRect = CGRectMake(130, 80, 400, textRect.size.height);

    [richText drawInRect:textRect];
}

If you need to draw large chunk of text, consider Text Kit . 如果需要绘制大量文本,请考虑使用Text Kit

The Text Kit approach: Text Kit方法:

- (void)drawAddressList:(NSArray *)list atPoint:(CGPoint)point {
    NSTextStorage *textStorage = [[NSTextStorage alloc] init];
    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
    NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:CGSizeMake(400.0f, FLT_MAX)];
    [textStorage addLayoutManager:layoutManager];
    [layoutManager addTextContainer:textContainer];
    textContainer.lineFragmentPadding = 0.0f;

    NSDictionary *attributes = @{NSFontAttributeName:[UIFont preferredFontForTextStyle:UIFontTextStyleBody]};
    // it's a simple attributes for illustration
    for (NSString *address in list) {
        NSString *buffer = [NSString stringWithFormat:@"%@\n", address];
        [textStorage appendAttributedString:[[NSAttributedString alloc] initWithString:buffer attributes:attributes]];
    }

    [layoutManager ensureLayoutForTextContainer:textContainer];
    CGRect rect = [layoutManager usedRectForTextContainer:textContainer];
    rect.size.width = 400.0f;
    rect.size.height = ceilf(rect.size.height);
    rect.origin = point; // this is the frame needed to draw the address list

    [layoutManager drawGlyphsForGlyphRange:NSMakeRange(0, textStorage.length) atPoint:point];
}

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

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