开发者

IOS: NSString format

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜