iOS:NSString 格式

发布于 2024-11-27 06:52:52 字数 1983 浏览 1 评论 0原文

我有这段代码:

NSString *firstString = [[NSString stringWithFormat:@"%@",stringToWrite] stringByPaddingToLength:40 withString:@" " startingAtIndex:0];
    NSString *finallyString = @"";
    finallyString = [firstString stringByAppendingString:secondString];

使用这段代码,我的字符串有两列的文本格式,但是当我使用 airPrint 方法打印这些字符串时,它们丢失了此文本格式。 为什么在 NSLog 中一切正常,但当我在纸张上打印时却丢失了文本格式?

打印代码:

NSMutableString *printHead = [NSMutableString stringWithFormat:@"%@", @"words"];
for (int i = 0; i<myArray.count; i++)
{
    [printHead appendFormat:@"\n\n\n"];
    NSString *stringToWrite = [[myArray objectAtIndex:i]objectAtIndex:0];
    NSString *firstString = [[NSString stringWithFormat:@"%@",stringToWrite] stringByPaddingToLength:40 withString:@" " startingAtIndex:0];
    NSString *finallyString = @"";
    finallyString = [firstString stringByAppendingString:[[myArray objectAtIndex:i]objectAtIndex:1]];
    [printHead appendString:finallyString];
    }
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.delegate = self;

UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
pic.printInfo = printInfo;

UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] initWithText:printHead];
textFormatter.startPage = 0;
//textFormatter.color = [UIColor redColor];
textFormatter.contentInsets = UIEdgeInsetsMake(30.0, 30.0, 72.0, 72.0); // 1 inch margins
textFormatter.maximumContentWidth = 8 * 72.0;
pic.printFormatter = textFormatter;
[textFormatter release];


pic.showsPageRange = YES;

void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
    if (!completed && error) {
        NSLog(@"Printing could not complete because of error: %@", error);
    }
};

[pic presentAnimated:YES completionHandler:completionHandler];

I have this code:

NSString *firstString = [[NSString stringWithFormat:@"%@",stringToWrite] stringByPaddingToLength:40 withString:@" " startingAtIndex:0];
    NSString *finallyString = @"";
    finallyString = [firstString stringByAppendingString:secondString];

with this code I have a text formatting of my string with two columns, but when I print these string with airPrint method they lost this text formatting.
Why in NSLog it's all ok and when I print in a paper it lost the text formatting?

code of print:

NSMutableString *printHead = [NSMutableString stringWithFormat:@"%@", @"words"];
for (int i = 0; i<myArray.count; i++)
{
    [printHead appendFormat:@"\n\n\n"];
    NSString *stringToWrite = [[myArray objectAtIndex:i]objectAtIndex:0];
    NSString *firstString = [[NSString stringWithFormat:@"%@",stringToWrite] stringByPaddingToLength:40 withString:@" " startingAtIndex:0];
    NSString *finallyString = @"";
    finallyString = [firstString stringByAppendingString:[[myArray objectAtIndex:i]objectAtIndex:1]];
    [printHead appendString:finallyString];
    }
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.delegate = self;

UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
pic.printInfo = printInfo;

UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] initWithText:printHead];
textFormatter.startPage = 0;
//textFormatter.color = [UIColor redColor];
textFormatter.contentInsets = UIEdgeInsetsMake(30.0, 30.0, 72.0, 72.0); // 1 inch margins
textFormatter.maximumContentWidth = 8 * 72.0;
pic.printFormatter = textFormatter;
[textFormatter release];


pic.showsPageRange = YES;

void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
    if (!completed && error) {
        NSLog(@"Printing could not complete because of error: %@", error);
    }
};

[pic presentAnimated:YES completionHandler:completionHandler];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离笑几人歌 2024-12-04 06:52:52

NSLog() 不是您应该依赖的格式化工具。如果您希望文本在打印时以某种方式显示,您需要将其绘制到视图中。如果您希望视图在打印时看起来与在屏幕上看起来不同,请实现 -drawRect:forViewPrintFormatter: 来绘制打印版本。

NSLog() isn't something you should rely on for formatting. If you want your text to appear a certain way when printed, you'll want to draw it into a view. If you want the view to look different when printed compared to how it looks on the screen, implement -drawRect:forViewPrintFormatter: to draw the printed version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文