3.7 KiB
title | description | type | page_title | slug | position | tags | res_type |
---|---|---|---|---|---|---|---|
Create Custom Text Measurer in .NET Standard | How to implement custom text measuring functionality in SpreadProcessing for .NET Standard. | how-to | Create Custom Text Measurer .NET Standard | create-custom-text-measurer-net-standard | 0 | spreadsheet, custom, text, measurer, netstandard | kb |
Product Version | Product | Author |
---|---|---|
2021.1.212 | RadSpreadProcessing | Martin Velikov |
Description
Due to .NET Standard APIs limitations, the SimpleTextMeasurer provides basic functionality for text measuring and it is not expected to be an all-purpose measurer. So in order to measure the specific text in a more precise way, you will need to create a custom implementation of a text measure and set it to the TextMeasurer property of the SpreadExtensibilityManager.
Solution
In the example below, we are demonstrating how to create a custom TextMeasurer inheriting the SpreadTextMeasurerBase abstract class and set it to the TextMeasurer property of the SpreadExtensibilityManager.
[C#] Creating a CustomTextMeasurer
{{region kb-create-custom-text-measurer-net-standard1}}
public class CustomTextMeasurer : SpreadTextMeasurerBase
{
private static readonly double ratioX = 1.035;
private static readonly double ratioY = 1;
private static readonly double ratioBaseline = 1;
private readonly SpreadTextMeasurerBase originalMeasurer;
public CustomTextMeasurer(SpreadTextMeasurerBase originalMeasurer)
{
this.originalMeasurer = originalMeasurer;
}
public override TextMeasurementInfo MeasureText(TextProperties textProperties, FontProperties fontProperties)
{
TextMeasurementInfo info = originalMeasurer.MeasureText(textProperties, fontProperties);
Size size = info.Size;
return new TextMeasurementInfo()
{
BaselineOffset = info.BaselineOffset * ratioBaseline,
Size = new Size(
size.Width * ratioX,
size.Height * ratioY),
};
}
public override TextMeasurementInfo MeasureTextWithWrapping(TextProperties textProperties, FontProperties fontProperties, double wrappingWidth)
{
TextMeasurementInfo info = originalMeasurer.MeasureText(textProperties, fontProperties);
Size size = info.Size;
return new TextMeasurementInfo()
{
BaselineOffset = info.BaselineOffset * ratioBaseline,
Size = new Size(
size.Width * ratioX,
size.Height * ratioY),
};
}
}
{{endregion}}
The following example shows how to set the custom implementation inheriting the SpreadTextMeasurerBase abstract class to the TextMeasurer property of the SpreadExtensibilityManager.
[C#] Setting the CustomTextMeasurer
{{region kb-create-custom-text-measurer-net-standard2}}
SpreadTextMeasurerBase customTextMeasurer = new CustomTextMeasurer(SpreadExtensibilityManager.TextMeasurer);
SpreadExtensibilityManager.TextMeasurer = customTextMeasurer;
{{endregion}}
See Also
- [How to Measure Text in WordsProcessing .NET Framework]({%slug wordsprocessing-measure-text-netframework%})
- [How to Measure Text in WordsProcessing .NET Standard]({%slug wordsprocessing-measure-text-netstandard%})