xtrareports-devexpress-dot-xtrareports-dot-ui-be0163fd.md
Enables you to find optimal boundaries to fit the text, and vice versa, get a font to fit the specified boundaries.
Namespace : DevExpress.XtraReports.UI
Assembly : DevExpress.XtraReports.v25.2.dll
NuGet Package : DevExpress.Reporting.Core
public static class BestSizeEstimator
Public Module BestSizeEstimator
The BestSizeEstimator.GetBoundsToFitText method overloads allow you to calculate a control’s size to fit its actual text or any specified text as well as find optimal boundaries for the text with the specified settings.
using DevExpress.XtraReports.UI;
private void xrLabel1_BeforePrint(object sender, System.ComponentModel.EventArgs e) {
XRLabel label = sender as XRLabel;
string myText = "Some lengthy content";
float myHeight = BestSizeEstimator.GetBoundsToFitText(label, myText).Height;
if (myHeight < BestSizeEstimator.GetBoundsToFitText(label).Height) {
label.Text = myText;
label.HeightF = myHeight;
}
}
Imports DevExpress.XtraReports.UI
Private Sub xrLabel1_BeforePrint(ByVal sender As Object, ByVal e As System.ComponentModel.EventArgs)
Dim label As XRLabel = TryCast(sender, XRLabel)
Dim myText As String = "Some lengthy content"
Dim myHeight As Single = BestSizeEstimator.GetBoundsToFitText(label, myText).Height
If myHeight < BestSizeEstimator.GetBoundsToFitText(label).Height Then
label.Text = myText
label.HeightF = myHeight
End If
End Sub
The BestSizeEstimator.GetFontToFitBounds method performs the opposite operation, that is gets the best font size to fit specific boundaries in the maximum possible way.
using DevExpress.XtraReports.UI;
private void xrLabel1_BeforePrint(object sender, System.ComponentModel.EventArgs e) {
XRLabel myLabel = sender as XRLabel;
Font 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
Private Sub xrLabel1_BeforePrint(ByVal sender As Object, ByVal e As System.ComponentModel.EventArgs)
Dim myLabel As XRLabel = TryCast(sender, XRLabel)
Dim myFont As Font = 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
The GetBoundsToFitContainer method enables you to calculate boundaries for the specified control to occupy the entire space of its parent container (accessed using the Parent property):
// ...
// Fit the picture box to the parent container (table cell or panel).
pictureBox.BoundsF = BestSizeEstimator.GetBoundsToFitContainer(pictureBox);
// ...
' ...
' Fit the picture box to the parent container (table cell or panel).
pictureBox.BoundsF = BestSizeEstimator.GetBoundsToFitContainer(pictureBox)
' ...
Object BestSizeEstimator
See Also