Posted by: beatkiener | August 5, 2009

Label Control with TextTrimming

Just a while ago, I’ve implemented a label control with a text trim behavior. If the text is longer than the available size of the label the text is truncated like this: “Text is too lo…”

label_texttrimming

Brad Cunningham and Robby Ingebretsen did a similar implementation. Both trimming the text during the measure/arrange call.

The implementation of my trimming functionality is similar to the solutions of Brad or Robby. Additionally, I’ve implemented a calculator class to get the text-width with an integrated cache. This makes the trimming function very fast.

As I saw the great examples of Brad and Robby, I decided to share my code too.

You can find the source code and a live example here:

Source Code / Live Demo

Please note: the source code is an extract of another project and I did not change the namespace.

 

Here are some main decisions for the label control:

  • It derived from Control, because one should not be able to add a child control to it (just like the normal TextBlock control)
  • The template is hardcoded, because nobody should be able to change it.
  • The text trimming functionality is implemented in the MeasureOverride method, because it’s the only place I get the available space from the parent element (including infinity)
  • Each char-width of the text is calculated and cached until the text or some font property changes. This makes the calculation very fast.
  • The class FontDefinitions provides an optimized char-width calculator with an integrated cache.
  • The class FontDefinitions uses a TextBlock to retrieve the text length, because a TextBlock immediately returns the width via the property ActualWidth (without any layout cycle).
  • The memory consumption of the font width cache is very small.
Advertisement

Responses

  1. You might be interested in the following trimming algorithm, especially for Silverlight:

    http://stackoverflow.com/questions/612774/right-side-ellipsis/2290045#2290045

    • Hi,

      Yes, thank you. I tried a similar algorithm as I wrote this text trimming control. Back then with Silverlight-2 I got an exception with the message “layout cycle detected”, but only in some circumstances. This was because of a limitation in the Silverlight engine how many times a control can get measured in the same layout pass. Today I’m happy that Silverlight 4 provides this functionally natively on the TextBlock control with TextTrimming=”WordEllipsis”.

      Thank you for your link!
      Cheers

  2. I’m not sure the place you are getting your information, however great topic. I must spend some time learning more or understanding more. Thank you for magnificent information I was in search of this info for my mission.


Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

Please log in to WordPress.com to post a comment to your blog.

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Categories

Follow

Get every new post delivered to your Inbox.