【问题标题】:How can I use regular and bold in a single String using iText Android如何使用 iText Android 在单个字符串中使用常规和粗体
【发布时间】:2019-02-20 10:30:21
【问题描述】:

我有一个由常量部分和变量部分组成的字符串。我希望在文本段落中使用常规字体对变量进行格式化,而我希望常量部分为粗体。

这是我的代码:

    val boldFont = Font(Font.FontFamily.TIMES_ROMAN, 22f, Font.BOLD)
    val semiBoldFont = Font(Font.FontFamily.TIMES_ROMAN, 16f, Font.BOLD)
    val normalFont = Font(Font.FontFamily.TIMES_ROMAN, 16f, Font.NORMAL)
    val lineSeparator = LineSeparator()
    lineSeparator.lineColor = BaseColor(0, 0, 0, 68)


//      NAME OF THE STUDENT
    val paragraph = Paragraph(student?.Student_Name, boldFont)
    paragraph.alignment = Paragraph.ALIGN_CENTER

//      DOB
    val paragraphTwo = Paragraph("Date of Birth: ", semiBoldFont)
    paragraphTwo.add(Chunk(student?.Student_DOB , normalFont))
    paragraphTwo.alignment = Paragraph.ALIGN_CENTER

//      Place and Country of Birth
    val paragraphThree = Paragraph("Place and Country of Birth: ", semiBoldFont)
    paragraphThree.add(Chunk(student?.Student_City + ", " + student?.Student_Country, normalFont))
    paragraphThree.alignment = Paragraph.ALIGN_CENTER

//      Address
    val paragraphFour = Paragraph("Address: ", semiBoldFont)
    paragraphFour.add(Chunk(student?.Student_Address + ", " + student?.Student_City + ", " + student?.Student_Country, normalFont))
    paragraphFour.alignment = Paragraph.ALIGN_CENTER

//      Nationality
    val paragraphFive = Paragraph("Nationality: ", normalFont)
    paragraphFive.add(Chunk(student?.Student_Nationality_One + ", " + student?.Student_Nationality_Two, normalFont))
    paragraphFive.alignment = Paragraph.ALIGN_CENTER

try {
        document.add(paragraph)
        document.add(Chunk(lineSeparator))
        document.add(paragraphTwo)
        document.add(paragraphThree)
        document.add(paragraphFour)
        document.add(paragraphFive)

        if (educationList.size > 0) {
            document.add(Paragraph("Education", boldFont))
            document.add(Paragraph(" "))
        }

    } catch (e: DocumentException) {
        e.printStackTrace()
    }

输出:

【问题讨论】:

    标签: android kotlin itext


    【解决方案1】:

    如果您在Paragraph 构造函数中设置字体,则用于稍后添加的Chunk 对象的字体是该块的字体由段落字体中的数据补充在属性中未设置块字体

    字体的样式是一个位域,不幸的是,样式域中的这个补充是通过按位或运算来实现的。因此,段落字体中的 BOLD 标志与添加到段落中的所有块的样式进行或运算!

    您可以通过根本不设置段落级别的字体(或至少不设置带有样式位的字体)并将标签添加为单独的块来规避此问题,例如而不是

    // DOB
    val paragraphTwo = Paragraph("Date of Birth: ", semiBoldFont)
    paragraphTwo.add(Chunk(student?.Student_DOB , normalFont))
    paragraphTwo.alignment = Paragraph.ALIGN_CENTER
    

    // DOB
    val paragraphTwo = Paragraph()
    paragraphTwo.add(Chunk("Date of Birth: ", semiBoldFont))
    paragraphTwo.add(Chunk(student?.Student_DOB , normalFont))
    paragraphTwo.alignment = Paragraph.ALIGN_CENTER
    

    或许

    // DOB
    val paragraphTwo = Paragraph("", normalFont)
    paragraphTwo.add(Chunk("Date of Birth: ", semiBoldFont))
    paragraphTwo.add(Chunk(student?.Student_DOB , normalFont))
    paragraphTwo.alignment = Paragraph.ALIGN_CENTER
    

    (这些选项的区别在于根据段落字体信息在段落前添加了一些间距。)

    【讨论】:

      猜你喜欢
      • 2015-02-25
      • 1970-01-01
      • 2019-02-27
      • 2015-06-05
      • 1970-01-01
      • 1970-01-01
      • 2019-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多