iOS:NSString 格式
我有这段代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.