Need to truncate text in Text3D with fixed Width and Height

snkarsnkar Member ✭✭

I need to be able to render some text in a Text3D component(using UrhoSharp and C#) which will have a defined max width and max height. So with a predefined font type and font size, if the rendered text exceeds the assigned width and height, it should truncate the text and render with ellipsis. Something like a windows forms Graphics.MeasureString custom algorithm would be the target.

I tried with setting the Width and WordWrap property and then examining the NumChars, NumRows, RowHeight and RowSpacing to come up with some custom logic to find the number of characters that can be fit in the assigned width and then using the NumRows, RowHeight and RowSpacing to calculate the height requirement and truncate as required. But the issue is that the Width property is an integer and the max width will always be in float resulting in the logic to fail(0.019 results in width being set as 0). Why is Width for Text3D in Integer?

Does anyone has any ideas around this? Maybe I am over complicating things and it would be as simple as setting some property but I cannot figure out how.

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    0.019 is a very small value, why do you want to set this value to your text's width?
    You could share your code to specify what effect you want to achieve.

  • snkarsnkar Member ✭✭

    0.019 was just an arbitrarily chosen float value to show as an example of a width value that is the desired max width for the text3D component. In the actual application, it will not be so small. My apologies for not choosing the value more realistically.

    I will try to show using some figures of what is the target for this exercise.

    Sample 1: A panel with two buttons and the middle portion is the text label which has a fixed width and height and the text assigned fits nicely inside the dimensions:

    Sample 2: The same panel with a text of larger length assigned and since Text3D will resize freely to accommodate the text, it expands beyond the specified max width and max height:

    Sample 3: This is the target solution where the larger length text is split across multiple lines to fit the max allowed width and since it still does not fit the max height, the text is truncated at the appropriate position and indicated by specifying ellipsis.

    I hope the above three samples and the images will make the issue easier to understand.

    I wanted to come up with a solution using the Width and WordWrap property of Text3D and using the other properties of Text3D (NumChars, NumRows, RowHeight and RowSpacing) to come up with an algorithm which can get me the max number of characters per line respecting the width and come up with the max number of rows in multiline mode respecting the height and finally allow me to truncate the string to fit the maximum allowed area.

    But the primary issue that I am facing is how do I set the Width property of the Text3D component based on the Maximum Width that is allowed when these values are in different units and data types. The max width that this text component can have based on my application logic is in floats while the Width property is itself in Integers.

    So either some way to convert the float value to the equivalent pixel coordinate based integer value is required or maybe some completely different approach. Any ideas?

  • utekaiutekai USMember ✭✭✭

    Here's the c++ code where the resizing is occurring. It does show what it is doing.

    That code is where you'll find comments like:

    // Set minimum and current size according to the text size, but respect fixed width if set
    

    and

    // If wordwrap is on, parent may need layout update to correct for overshoot in size. However, do not do this when the
    // update is a response to resize, as that could cause infinite recursion
    

    A different approach is what I'd recommend. You're trying to fit a square into a round hole, if you get the analogy.

Sign In or Register to comment.