Skip to main content

BestSizeEstimator.GetBoundsToFitText(XRLabel, String) Method

Gets boundaries that fit the specified text within the specified control.

Namespace: DevExpress.XtraReports.UI

Assembly: DevExpress.XtraReports.v24.1.dll

NuGet Package: DevExpress.Reporting.Core

Declaration

public static RectangleF GetBoundsToFitText(
    XRLabel label,
    string text
)

Parameters

Name Type Description
label XRLabel

An XRLabel control whose style and other settings are used in calculations.

text String

A string that specifies the text that should be written to the label in calculations.

Returns

Type Description
RectangleF

A RectangleF structure specifying the control boundaries.

Remarks

The GetBoundsToFitText method allows you to calculate the size that the specified control will have to fit the specified text. You can then apply the resulting rectangle and the specified text to this control or other required controls.

You can also use another GetBoundsToFitText overload to find optimal boundaries for an actual control text.

The code snippet below demonstrates how to use these methods to change the control text if the resulting control height is less than the height with an actual text (the XRControl.Text property value).

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;
    }
}

If a control is bound to data, call this method 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, XRControl.TextAlignment and RightToLeft options.

Note

This method is not supported when the XRLabel.Angle property is specified.

To perform the opposite operation (that is get the font size to fit the control’s boundaries), call the BestSizeEstimator.GetFontToFitBounds method.

See Also