<style name="blueStyle" >
<conditionalStyle>
<conditionExpression><![CDATA[($P{INDIRIZZO}).length()>30 ? Boolean.TRUE : Boolean.FALSE]]></conditionExpression>
<style style="blueStyle" fontSize="3"/>
</conditionalStyle>
</style>
<parameter name="INDIRIZZO" class="java.lang.String"/>
[...]
<textField>
<reportElement x="178" y="94" width="157" height="17"/>
<textElement>
<font fontName="Arial" size="9"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$P{INDIRIZZO}]]></textFieldExpression>
</textField>
Run Code Online (Sandbox Code Playgroud)
当INDIRIZZO长度> 30 时,我想缩小字体大小 ...
但这不起作用....
Ale*_*x K 12
您忘记将自定义样式应用于textField.
正确的片段将是:
<textField>
<reportElement style="blueStyle" x="178" y="94" width="157" height="17"/>
<textElement>
<font fontName="Arial" size="9"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$P{INDIRIZZO}]]></textFieldExpression>
</textField>
Run Code Online (Sandbox Code Playgroud)
我的工作样本:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="conditional_styl" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<style name="style1" forecolor="#66FF66" backcolor="#009966">
<conditionalStyle>
<conditionExpression><![CDATA[$P{parameter1}.length() < 2]]></conditionExpression>
<style forecolor="#FFCC00"/>
</conditionalStyle>
</style>
<parameter name="parameter1" class="java.lang.String"/>
<queryString>
<![CDATA[SELECT DOCUMENTID FROM POSITIONS]]>
</queryString>
<field name="DOCUMENTID" class="java.lang.Integer"/>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement style="style1" x="0" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{DOCUMENTID}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
Run Code Online (Sandbox Code Playgroud)
另一个使用fontSize修改的工作示例:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="conditional_styl" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<style name="style1" fontSize="6">
<conditionalStyle>
<conditionExpression><![CDATA[$F{DOCUMENTID} % 2 == 0]]></conditionExpression>
<style fontSize="8"/>
</conditionalStyle>
<conditionalStyle>
<conditionExpression><![CDATA[$F{DOCUMENTID} % 3 == 0]]></conditionExpression>
<style fontSize="10"/>
</conditionalStyle>
<conditionalStyle>
<conditionExpression><![CDATA[$F{DOCUMENTID} % 5 ==0]]></conditionExpression>
<style fontSize="12"/>
</conditionalStyle>
<conditionalStyle>
<conditionExpression><![CDATA[$F{DOCUMENTID} % 7 ==0]]></conditionExpression>
<style fontSize="14"/>
</conditionalStyle>
<conditionalStyle>
<conditionExpression><![CDATA[$F{DOCUMENTID} % 11 ==0]]></conditionExpression>
<style fontSize="16"/>
</conditionalStyle>
<conditionalStyle>
<conditionExpression><![CDATA[$F{DOCUMENTID} % 13 ==0]]></conditionExpression>
<style fontSize="18"/>
</conditionalStyle>
</style>
<queryString>
<![CDATA[SELECT distinct DOCUMENTID FROM POSITIONS]]>
</queryString>
<field name="DOCUMENTID" class="java.lang.Integer"/>
<detail>
<band height="34" splitType="Stretch">
<textField>
<reportElement style="style1" x="0" y="0" width="100" height="34"/>
<textElement/>
<textFieldExpression><![CDATA[$F{DOCUMENTID}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
Run Code Online (Sandbox Code Playgroud)
您还可以使用样式文本来允许动态更改文本大小。将 textElement 标记设置为styled,然后<style>在 textFieldExpression 的内容周围添加一个标签。
<textField>
<reportElement x="10" y="10" width="150" height="13" />
<textElement markup="styled"/>
<textFieldExpression>
<![CDATA["<style size=\"" + $V{fontSize} + "\">" + $F{name} + "</style>"]]>
</textFieldExpression>
</textField>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,文本大小由Integer报告中定义的变量指定,但它同样可以来自另一个字段或报告参数。
<variable name="fontSize" class="java.lang.Integer">
<variableExpression><![CDATA[12]]></variableExpression>
</variable>
Run Code Online (Sandbox Code Playgroud)
这允许你做一些聪明的事情,比如根据字段的长度改变文本大小:
<variable name="fontSize" class="java.lang.Integer">
<variableExpression><![CDATA[$F{firstName}.length()]]></variableExpression>
</variable>
Run Code Online (Sandbox Code Playgroud)
可以看到图片下方名称的字体大小是根据名称的长度来决定的: