5,693,936 members and growing! (16,958 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Presentation Foundation » Controls     Intermediate License: The Code Project Open License (CPOL)

Text On A Path in WPF

By lneir

A control that will display a text string on a specified geometry
C# 3.0, C#, Windows, XAML, WPF, Dev

Posted: 12 Oct 2008
Updated: 30 Oct 2008
Views: 5,342
Bookmarked: 34 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
20 votes for this Article.
Popularity: 5.98 Rating: 4.59 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
2 votes, 10.0%
3
4 votes, 20.0%
4
14 votes, 70.0%
5

Introduction

Currently, with Window Presentation Foundation (WPF), it is not easy to display a text string that will follow the path of any given geometry. WPF’s TextEffects provides transformation on individual characters, but trying to use this capability to get the text to follow an arbitrary geometry requires some additional work. Also, TextEffects is not available in Silverlight. With the control provided here, a given text string can be made to display on any 2D WPF geometry. The control can, for example, display text on an EllipseGeometry:

ellipse.PNG

A sample application and source code has been provided.

Background

To display text on a path, the fundamental problem that must be solved is to figure out how to translate and rotate each character of the string to fit the specified geometry. A big help in solving the problem is the GetFlattenedPathGeometry() method provided by the Geometry base class. This method returns a PathGeometry that approximates the geometry by a series of lines (PolyLineSegment and LineSegment). For example, the red lines below depict the flattened path of the path geometry (black):

flattenedpath.PNG

For a good overview of the WPF geometry classes, see MSDN.

The next step in the problem is to take the flattened path geometry and figure out how to transform the characters of the string to follow the flattened path. The figure below depicts the angle that must be calculated so the letter “A” will follow the flattened path. In TextOnAPath, the method ‘GetIntersectionPoints’ in the GeometryHelper class calculates the points along the flattened path where the characters intersect with the path. Knowing the intersection points, the angle (and translation) values can then be calculated.

flattenedpathWithText.PNG

Using the Code

The TextOnAPath control is used just like any normal UIElement in WPF. The XAML below will display the text string along the curved path, centered in a grid:

<Window x:Class="TextOnAPathTestApp.Window1"
    xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
    xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
    xmlns:TextOnAPath="clr-namespace:TextOnAPath;assembly=TextOnAPath"
    Title="Test App" Height="500" Width="500">
    <Grid>
    <TextOnAPath:TextOnAPath FontSize="30" DrawPath="True" 
        Text="The quick brown fox jumped over the lazy hen.">
            <TextOnAPath:TextOnAPath.TextPath>
                <PathGeometry Figures="M0,0 C120,361 230.5,276.5 230.5,276.5 
                      L308.5,237.50001 C308.5,237.50001 419.5,179.5002 367.5,265.49993 
                      315.5,351.49966 238.50028,399.49924 238.50028,399.49924 L61.500017,
                      420.49911"/>
            </TextOnAPath:TextOnAPath.TextPath>
        </TextOnAPath:TextOnAPath>
    </Grid>
</Window>

ExampleWindow.PNG

Parameters for the TextOnAPath control:

  • Text (string): The string to be displayed. If the text string is longer than the geometry path, then text will be truncated.
  • TextPath (Geometry): The geometry for the text to follow.
  • DrawPath (Boolean): If true, draws the geometry below the text string.
  • DrawLinePath (Boolean): If true, draws the flattened path geometry below the text string.
  • ScaleTextPath (Boolean): If true, the geometry will be scaled to fit the size of the control.

Points of Interest

History

  • 13th October, 2008
    • Initial post
  • 29th October, 2008
    • Update to fix code that draws geometry
    • Provided link to Silverlight version of code

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

lneir


I work in the Bay Area developing software using .NET/C#, WPF, and Silverlight.
Occupation: Software Developer (Senior)
Company: Pinnacle Systems/Avid
Location: United States United States

Other popular Windows Presentation Foundation articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
General'hen' -> 'dog'memberNigelHome22:26 20 Oct '08  
GeneralRe: 'hen' -> 'dog'memberlneir6:26 21 Oct '08  
GeneralRe: 'hen' -> 'dog'memberRichard Deeming5:06 22 Oct '08  
GeneralRe: 'hen' -> 'dog'memberkalme9:16 22 Oct '08  
GeneralRe: 'hen' -> 'dog'memberMember 46471620:35 5 Nov '08  
GeneralPath Flatteningmemberkevingay15:42 20 Oct '08  
GeneralEasier waymemberDaniel Larocque3:56 14 Oct '08  
GeneralRe: Easier waymemberlneir6:41 14 Oct '08  
GeneralRe: Easier waymembermike castillo7:11 21 Oct '08  
GeneralRe: Easier waymemberlneir7:32 21 Oct '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 30 Oct 2008
Editor: Deeksha Shenoy
Copyright 2008 by lneir
Everything else Copyright © CodeProject, 1999-2008
Web09 | Advertise on the Code Project