xtrareports-devexpress-dot-xtrareports-dot-ui-dot-bestsizeestimator-dot-getfonttofitbounds-x28-devexpress-dot-xtrareports-dot-ui-dot-xrlabel-system-dot-string-x29.md
Gets the font with which the specified text fits the specified label’s boundaries.
Namespace : DevExpress.XtraReports.UI
Assembly : DevExpress.XtraReports.v25.2.dll
NuGet Package : DevExpress.Reporting.Core
public static DXFont GetFontToFitBounds(
XRLabel label,
string text
)
Public Shared Function GetFontToFitBounds(
label As XRLabel,
text As String
) As DXFont
| Name | Type | Description |
|---|---|---|
| label | XRLabel |
An XRLabel control whose boundaries will be used in calculations.
| | text | String |
A string that specifies the text used in calculations.
|
| Type | Description |
|---|---|
| DXFont |
An optimal font.
|
The GetFontToFitBounds method allows you to calculate the font for the specified text to fit the specified control’s boundaries. You can then apply the resulting font and the specified text to this control or other required controls.
You can also use another GetFontToFitBounds overload to find an optimal font for an actual control text (the XRControl.Text property value).
The code snippet below demonstrates how to use these methods to change the control text if the resulting font size suits a specific condition.
using DevExpress.XtraReports.UI;
using DevExpress.Drawing;
private void xrLabel1_BeforePrint(object sender, System.ComponentModel.EventArgs e) {
XRLabel myLabel = sender as XRLabel;
DXFont myFont = BestSizeEstimator.GetFontToFitBounds(myLabel, "Product: " + myLabel.Text);
if (myFont.Size > 12) {
myLabel.Text = "Product: " + myLabel.Text;
myLabel.Font = myFont;
}
else myLabel.Font = BestSizeEstimator.GetFontToFitBounds(myLabel);
}
Imports DevExpress.XtraReports.UI
Imports DevExpress.Drawing;
Private Sub xrLabel1_BeforePrint(ByVal sender As Object, ByVal e As System.ComponentModel.EventArgs)
Dim myLabel As XRLabel = TryCast(sender, XRLabel)
Dim myFont As DXFont = BestSizeEstimator.GetFontToFitBounds(myLabel, "Product: " & myLabel.Text)
If myFont.Size > 12 Then
myLabel.Text = "Product: " & myLabel.Text
myLabel.Font = myFont
Else
myLabel.Font = BestSizeEstimator.GetFontToFitBounds(myLabel)
End If
End Sub
If a control is bound to data, call these methods when the corresponding binding has already been evaluated (for instance, in the XRControl.BeforePrint event handler).
This method’s result depends on the control’s XRControl.WordWrap option.
Note
This method is not supported when the XRLabel.Angle property is specified.
To perform the opposite operation (that is get the boundaries to fit the control’s text), call the BestSizeEstimator.GetBoundsToFitText method.
See Also