Back to Devexpress

BestSizeEstimator Class

xtrareports-devexpress-dot-xtrareports-dot-ui-be0163fd.md

latest4.3 KB
Original Source

BestSizeEstimator Class

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

Declaration

csharp
public static class BestSizeEstimator
vb
Public Module BestSizeEstimator

Remarks

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.

csharp
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;
    }
}
vb
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.

csharp
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);
}
vb
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):

csharp
// ...
// Fit the picture box to the parent container (table cell or panel).
pictureBox.BoundsF = BestSizeEstimator.GetBoundsToFitContainer(pictureBox);
// ...
vb
' ...
' Fit the picture box to the parent container (table cell or panel).
pictureBox.BoundsF = BestSizeEstimator.GetBoundsToFitContainer(pictureBox)
' ...

Inheritance

Object BestSizeEstimator

See Also

BestSizeEstimator Members

DevExpress.XtraReports.UI Namespace