gpt4 book ai didi

ios - NSString drawInRect :withAttributes: is not drawing text with greater width than the rect in iOS 7. 0.3

转载 作者:行者123 更新时间:2023-11-29 12:30:00 30 4
gpt4 key购买 nike

我正在尝试在我的应用程序中生成一个 pdf。为了绘制字符串,我使用 boundingRectWithSize 计算该字符串的边界矩形的大小,然后在该大小的矩形内绘制字符串。

该代码在 iOS 7.1 及更高版本中运行良好,但在 iOS 7.0.3 中,如果文本的宽度大于矩形的宽度 (400),则根本不会绘制文本。根据 Apple 的文档,如果字符串不适合矩形,则该字符串应该换行并剪裁,这在 iOS 7.1 及更高版本中发生,但在 iOS 7.0.3 中不会发生。

这是我的代码片段:

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

我无法弄清楚可能是什么问题。请帮忙。

最佳答案

试试这个:

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

如果您需要绘制大块文本,请考虑 Text Kit

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

关于ios - NSString drawInRect :withAttributes: is not drawing text with greater width than the rect in iOS 7. 0.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27980147/

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