none
Graphics.MeasureString - how to handle advanced settings, how to get breaks RRS feed

  • Question

  • Hi all;

    I've been using Graphics.MeasureString to measure line lengths. However, I now need to take into account:

    1. Small Caps
    2. Scaling (making each character thinner/wider).
    3. Spacing (adding/subtracting space after each character).
    4. Hyphenation, where line breaks can occur at specific places within words.

    Is there a way to do this using MeasureString? The two fundamental things I need to get is:

    1. How long is a passed in string.
    2. For a passed in string and length, how many characters of the string fits in that length.

    I can do this calling it character by character. But is there a better way?

    Also, is there a better means of finding the break point in a string for a given length other than calling MeasureString and keep adding a letter to the string until I pass the length limit?

    thanks - dave


    Who will win The Windward International Collegiate Programming Championships?

    Thursday, February 27, 2014 1:08 AM

All replies

  • Hi Dave,

    >Is there a way to do this using MeasureString?

    I think it’s impossible. Because Graphics.MeasureString is a method to get the size of the input string.

    >I can do this calling it character by character. But is there a better way?

    What’s the passed in string you have mentioned? I cannot understand, could you please post more information.

    >Also, is there a better means of finding the break point in a string for a given length other than calling MeasureString and keep adding a letter to the string until I pass the length limit?

    Did you want to automatically wrap text in WinForm controls, such as Label? Here are some useful link contains some code sample. See the following link for details.

    http://www.codeproject.com/Articles/51488/Implementing-Word-Wrap-in-C.

    http://social.msdn.microsoft.com/Forums/windows/en-US/97c18a1d-729e-4a68-8223-0fcc9ab9012b/automatically-wrap-text-in-label?forum=winforms.

    http://stackoverflow.com/questions/3961278/word-wrap-a-string-in-multiple-lines.

    Hope useful.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, February 27, 2014 7:08 AM
    Moderator
  • Hi Hetro;

    Yes I need to figure out where to wrap text (put line breaks). However, this is not for a Form or WPF control. I am building up pages for PDF output so I need a programming call to determine where to break the line.

    As you said MeasureString is not designed for this. However, it's the only method I've found so far where I keep adding a letter to the string and calling it until the length is greater than the line length. Is there an equivilent to the Java LineBreakMeasurer class in .NET? (The links you provided are for breaking every N characters which won't work with proportional fonts.)

    And my second question, which I don't think you addressed, is there a way with MeasureString or any other .NET method to measure with the ability to set:

    1. Small Caps
    2. Scaling (making each character thinner/wider).
    3. Spacing (adding/subtracting space after each character).
    4. Hyphenation, where line breaks can occur at specific places within words.

    thanks - dave


    Who will win The Windward International Collegiate Programming Championships?

    Thursday, February 27, 2014 2:12 PM
  • Hi Dave,

    >Is there an equivilent to the Java LineBreakMeasurer class in .NET?

    I haven’t heard something about LineBreakMeasurer class or features you want in WinForm or ASP.NET. They are not good at this. I recommend you have a look at Flow Document in WPF, it is designed for optimize viewing and readability. For more information, see the following link. http://msdn.microsoft.com/en-us/library/aa970909(v=vs.110).aspx.

    Flow Document can also solve your second question.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, February 28, 2014 5:49 AM
    Moderator
  • Flow Document can also solve your second question.

    Hi;

    Are there calls to just get the measurements using the flow documents API. I couldn't find any, just a means to have it render to the screen. I need the measurements.

    ??? - thanks - dave


    Who will win The Windward International Collegiate Programming Championships?

    Friday, February 28, 2014 12:27 PM