TLDR:如果您打印包含使用right
/center
文本对齐的HTML元素内容的UIWebView
,生成的文档将具有意外大的底部边距。
我发现这个问题时很惊讶,因为搜索不到有类似问题的人。 我向苹果提交了一个雷达(#20760071),并在ResearchKit
的GitHub存储库上创建了一个问题,因为这会影响到他们的ORKHTMLPDFWriter
。
据我所知,这也会影响所有使用UIWebView
将HTML
转换为PDF
的库,我进行了测试:
我想知道是否有人能想出一些解决方法。
如何重现:
NSMutableString* html = [NSMutableString string];
[html appendString:@"<html><body>"];
for (int i=0; i<200; i++) {
[html appendString:@"<div align=\"right\">line</div>"];
}
[html appendString:@"</body></html>"];
UIPrintInteractionController* pc = [UIPrintInteractionController sharedPrintController];
UIPrintInfo* printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGrayscale;
pc.printInfo = printInfo;
pc.showsPaperSelectionForLoadedPapers = YES;
UIWebView* web = [UIWebView new];
[web loadHTMLString:html baseURL:nil];
pc.printFormatter = web.viewPrintFormatter;
[pc presentAnimated:YES completionHandler:^(UIPrintInteractionController *printInteractionController, BOOL completed, NSError *error) {
NSLog(@"%d -- %@",completed, error);
}];
您还可以克隆项目,该项目演示了在ResearchKit
中出现的此问题。
编辑 - 一些可用的解决方法:
知道内容的特定宽度(例如图像),可以在不指定文本对齐方式的情况下对齐它,例如:
使用自动边距:
<div>
<div style="width:200px; margin-left:auto; margin-right:0px;">
content
</div>
</div>
浮动列:
<div>
<div style="width:200px; float:right;">
content
</div>
</div>
然而,上述方法均无法对齐变长文本,而我关注的主要用例就是这种情况。
margin-left:auto; margin-right:0;
将一个div右对齐到另一个div中。 - Dalzhimdiv
的宽度,这通常不适用于文本对齐。或者我有什么遗漏吗? - Jakub Vano