创建一个新对象并同时调用方法
Creating a new object and calling the method at the same time
我有简单的代码(iTextShar 库)生成带有 Phrase 的 PdfCell:
//I create new Phrase
Phrase p = new("Lorem ipsum", myFont);
//I set leading for Phrase
p.setFixedLeading(10f);
//I create new Table Cell end insert Phrase
PdfPCell cell = new(p);
为什么我不能直接在 PdfPCell 上生成 Phrase 并同时 运行 方法 .setFixedLeading。是这样想的吗?
PdfPCell cell = new(Phrase p("Lorem ipsum", myFont).setFixedLeading(10f));
在创建新的 Phrase 对象时,有什么方法可以使用 .setFixedLeading 方法设置 Leading 吗?
您可以编写自己的方法:
private Phrase CreatePhraseWithFixedLeading(string text, Font font, float fixedLeading)
{
Phrase p = new(text, font);
p.setFixedLeading(fixedLeading);
return p;
}
用法是
Phrase p = CreatePhraseWithFixedLeading("lorem ipsum", myFont, 10f);
PdfPCell cell = new(p);
或者,如果您在创建单元格后不需要该短语
PdfPCell cell = new(CreatePhraseWithFixedLeading("lorem ipsum", myFont, 10f));
如果你在创建短语后有更多的方法可以调用,你可以看看构建器模式。
但另一方面,你应该问问你从中得到了什么。您提供的代码非常好。不要试图把尽可能多的东西放在一行中。
我有简单的代码(iTextShar 库)生成带有 Phrase 的 PdfCell:
//I create new Phrase
Phrase p = new("Lorem ipsum", myFont);
//I set leading for Phrase
p.setFixedLeading(10f);
//I create new Table Cell end insert Phrase
PdfPCell cell = new(p);
为什么我不能直接在 PdfPCell 上生成 Phrase 并同时 运行 方法 .setFixedLeading。是这样想的吗?
PdfPCell cell = new(Phrase p("Lorem ipsum", myFont).setFixedLeading(10f));
在创建新的 Phrase 对象时,有什么方法可以使用 .setFixedLeading 方法设置 Leading 吗?
您可以编写自己的方法:
private Phrase CreatePhraseWithFixedLeading(string text, Font font, float fixedLeading)
{
Phrase p = new(text, font);
p.setFixedLeading(fixedLeading);
return p;
}
用法是
Phrase p = CreatePhraseWithFixedLeading("lorem ipsum", myFont, 10f);
PdfPCell cell = new(p);
或者,如果您在创建单元格后不需要该短语
PdfPCell cell = new(CreatePhraseWithFixedLeading("lorem ipsum", myFont, 10f));
如果你在创建短语后有更多的方法可以调用,你可以看看构建器模式。
但另一方面,你应该问问你从中得到了什么。您提供的代码非常好。不要试图把尽可能多的东西放在一行中。