SharpLeaf Tutorials > Text Flow Tutorials > Advanced Text Control

Advanced Text Control

SharpLeaf allows fine character-level control of the output, at the cost of a slightly more complex API than simply SharpLeaf.AddParagraphs.

Character-level control

Text and Paragraphs settings can be changed in-flight in several ways:

Style modifications can also be passed as string specifications that match the result produced by ParagraphStyle.ToString method (see ParagraphStyle constructor, ParagraphStyle.Set, SharpLeaf.PushStyle, SharpLeaf.AddParagraphs

   lf.Style = style = new ParagraphStyle();  // Keep a handle to the current paragraph style
   style.SpaceBefore = -0.5;
   style.SpaceAfter = -0.5;  // Half a line pitch around each paragraph
   style.Font = font = new FontType("Times",24);

   lf.AddText("We can put normal text, ");
   newfont = new FontType("Arial",36,FontStyle.Italic|FontStyle.Bold|
   style.Font = newfont;  // Replace the ParagraphStyle's property
   lf.AddText("then change the font");
   style.Font = font;  // Replace again back to previous
   lf.AddText(" then back up the change to normal font.");

// Without the NextLine call, in-flight paragraph style modification would affect the 
            // previous (unfinished) line.
   lf.PushStyle(newstyle = style.Clone());  // Non-destructive, temporary style modification
   newstyle.Alignment = Alignment.Center;
   newstyle.Set("IndentLeft=72 IndentRight=72");  // Specify modification as string
   lf.AddText("We can change other paragraph settings in-flight too.");
   lf.PopStyle();  // Back up the temporary style modification

   lf.PushStyle("FontName=\"Arial\" FontColor=DarkGreen");  // Specify modification as string
   lf.AddText("Text can be ");
   lf.AddText(" and ");
   lf.AddText(" and");
   lf.AddSubscript("at once");

   lf.CapFont = new FontType("Old English Text MT",12,FontStyle.Regular,Color.Navy);
   lf.SetCap(2);  // Set up a dropped capital on two lines
   box = new BoxStyle(2,Color.Firebrick,LineStyle.Dash,24);
   lf.PushStyle();  // Temporarily copy current style
   lf.Style.HorizontalGap = 24; 
            // Use horizontal gutter of 24 points between text and box - Vertical 
            // gutter of the box is provided by the paragraph's SpaceBefore and 
            // SpaceAfter because we start/stop boxing on paragraph boundaries;
   lf.SetBox(box);  // Start boxing
   lf.AddParagraphs("And finally, we can set up a dropped capital to show how the text "+
            "nicely wraps around it, and set up boxing around the paragraph.");
   lf.SetBox();  // Stop boxing

HTML tags

HTML-formatted text can be input into SharpLeaf through the SharpLeaf.AddHtml method. See that entry for list of supported tags.

   lf.AddHtml("<p> HTML &amp;entities; and basic tags are supported: ");
   lf.AddHtml("<b><i><u><strike><pre>font "+
            "styles</pre></strike></u></i></b>, ");
   lf.AddHtml("and<sup>superscripts</sup><sub>subscripts</sub>, ");
   lf.AddHtml("<a href=\"\">hyperlinks</a>, ");
   style.BulletIndent = 72;  // HTML bulleted lists uses that - we don't want the default 0
   lf.AddHtml("and even bulleted lists: <ol> <li> Item 1 <li> Item 2 "+
   lf.AddHtml("</p> <hr> <p> Along with paragraph separators, "+
            "horizontal rules, and line <br> breaks. </p>");

Soft Hyphens

Soft Hyphens can be used when paragraph wrapping is turned on, to specify that hyphenated wrapping is acceptable at those points. The default character is ‘­’ (Unicode 0xAD – codepoint 173), but can be changed through the SharpLeaf.SoftHyphen property.

   for (int ct = 1; ct <= 13; ct++) {
     lf.AddText("a sample paragraph ");  // No soft hyphen
   for (int ct = 1; ct <= 13; ct++) {
     lf.AddText("a sam­ple pa­ra­gr­aph ");  // Soft hyphens

As you can see, the paragraph is less ragged and take smaller a space.

Non-breakable Spaces

Non-breakable space can be used when paragraph wrapping is turned on, to specify that wrapping should not occur at those points. The standard character is ‘ ’ (Unicode 0xA0 – codepoint 160).

   for (int ct = 1; ct <= 7; ct++) {
    // Normal spaces
     lf.AddText("Mr. Doe has 10 oz of «play dough». ");
   for (int ct = 1; ct <= 7; ct++) {
    // Non-breakable space after title, before unit, and within quotes
     lf.AddText("Mr. Doe has 10 oz of «play dough». ");

As you can see, using it produces more ragged paragraphs.

See also ...

Text Flow Tutorials | Paragraph Glossary | Font Glossary | SharpLeaf Members

Send comments on this topic
© Dyalog Ltd 2016