ThinkGeo.com    |     Documentation    |     Premium Support

Centre Align Multiline Label

Hi,

We often have labels that contain newline characters and would like to be able to present these on screen centre aligned. There was an old discussion on this subject here:

and the solution reads like it should be pretty much what we need, but unfortunately it appears that the text is being trimmed to remove leading whitespace after the reformatting, meaning any label where the first line needs padding is getting that padding removed somewhere else.

What would be the correct way of achieving the desired effect now? or is there a way to suppress removing leading whitespace?

Thanks,
Jonathan

Hi Jonathan,

The best solution is override the FormatCore function of text style, in our default FormatCore it will trim the label, you can modify the logic by override it.

And if you want to implement that by event you can did it like this(I removed the center align logic here):

multiLineTextStyle.Formatted += MultiLineTextStyle_Formatted;

event:

        private void MultiLineTextStyle_Formatted(object sender, FormattedPositionStyleEventArgs e)
    {
        string tmpLabel = "  " + e.Text;


        string[] subTexts = tmpLabel.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
        int maxLength = subTexts[0].Length;
        foreach (var item in subTexts)
        {
            if (maxLength < item.Length)
                maxLength = item.Length;
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < subTexts.Length; i++)
        {
            sb.Append(subTexts[i]);
            if (i != (subTexts.Length - 1))
                sb.AppendLine();
        }
        e.Text = sb.ToString();
    }

Regards,

Ethan

Ethan,

Thanks, I tried the Formatted event and it worked really well. I haven’t tried override FormatCore yet, are there any things done in the normal version that I would need to make sure I do in the replacement?

Regards,
Jonathan

Hi Jonathan,

If Formatted works, you can just ignore override FormatCore.

In standard process it only handle the text to make it render better, I think you don’t need do other things now. Because you can do anything for the label format follow your requirement, so if you found the result is not you need please just override FormatCore to remove the standard process logic.

Regards,

Ethan

Ethan,

Thanks. I tried out the FormatCore method as well and that also works nicely.

Regards,
Jonathan

Hi Jonathan,

Thanks for your update.

Any question please let us know.

Regards,

Ethan