Automatic text hyphenation for Windows Phone

Recently the topic of text hyphenation for Windows Phone has cropped up. So I thought I’d give it a quick go. Note, this is an initial stab at a solution, I’m not suggesting it’s fully functional but certainly could be built upon.

The problem

image

 <TextBox> 
      FontSize="40"                     
      Width="170"
      TextWrapping="Wrap"
      Text="Hello world combinations can be extremely worrying" />                

Possible Solution

The design is pretty simple (although is it Right-To-Left compatible?), check when the text moves from one line to the next. When it does, check to see if the last character is part of a word. It it is, the add a hyphen.

using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;

namespace Pauliom.Behaviors
{
    public class HyphenationBehavior : Behavior<TextBox>
    {
        private int lastLength = 0;
        protected override void OnAttached()
        {

            var textBox = this.AssociatedObject;
            textBox.TextChanged += textBox_TextChanged;
            textBox.Loaded += textBox_Loaded;
            base.OnAttached();
        }

        void textBox_Loaded(object sender, RoutedEventArgs e)
        {
            Hyphenate(sender);
        }

        void textBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            Hyphenate(sender);
        }

        private void Hyphenate(object sender)
        {
            TextBox textBox = sender as TextBox;
            if (this.lastLength != textBox.Text.Length)
            {
                this.lastLength = textBox.Text.Length;
                Rect lastRectange = textBox.GetRectFromCharacterIndex(1);
                int x = 0;
                while (x < textBox.Text.Length)
                {
                    Rect r = textBox.GetRectFromCharacterIndex(x);
                    if (lastRectange.Top != r.Top)
                    {
                        lastRectange = r;
                        char endChar = textBox.Text[x - 1];
                        if (endChar != ' ' && endChar != '-')
                        {
                            textBox.Text = textBox.Text.Substring(0, x - 1) + "-" + textBox.Text.Substring(x - 1);
                        }
                    }                    
                    x++;
                }
            }
        }

        protected override void OnDetaching()
        {
            var textBox = this.AssociatedObject;
            textBox.TextChanged -= textBox_TextChanged;
            base.OnDetaching();
        }
    }
}

image

 <TextBox> 
      FontSize="40"                     
      Width="170"
      TextWrapping="Wrap"
      Text="Hello world combinations can be extremely worrying">    
      <i:Interaction.Behaviors>
         <pauliom:HyphenationBehavior />
      </i:Interaction.Behaviors>                
 </TextBox>            

As I said, it probably is not production ready code but could be built upon.

Leave a Reply

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s