代码之家  ›  专栏  ›  技术社区  ›  Kanwarpreet Singh

如何使用iText Android在单个字符串中使用常规和粗体

  •  0
  • Kanwarpreet Singh  · 技术社区  · 7 年前

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

    这是我的代码:

        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()
        }
    

    enter image description here

    0 回复  |  直到 7 年前
        1
  •  1
  •   mkl    7 年前

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

    字体的样式是一个位字段,不幸的是 补充 大胆的 标记从段落字体是或ed到所有添加到段落的块的样式!

    您可以通过完全不在段落级别设置字体(或至少不设置具有样式位的字体)以及将标签作为单独的块添加(例如,代替

    // 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
    

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