Skip to content

SkiaSharp.TextBlock adds text block and rich text layout to SkiaSharp

License

Notifications You must be signed in to change notification settings

wouterst79/SkiaSharp.TextBlocks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SkiaSharp.TextBlocks

SkiaSharp.TextBlocks adds text block and rich text layout to SkiaSharp. Tested on iOS, Android, and Windows, and likely to perform on other SkiaSharp supported platforms.

Using SkiaSharp.TextBlocks

SkiaSharp.TextBlocks is available as a convenient NuGet package, to use install the package like this:

nuget install SkiaSharp.TextBlocks

Sample uses:

NOTE: DrawTextBlock returns the SKRect that contains the text. The sample project draws a green box around this rect. See the source for details.

Basic Samples

Hello World:

Basic_Samples-Hello_World

canvas.DrawTextBlock("Hello world!", new SKRect(0, 0, 100, 0), new Font(14), SKColors.Black);

FlowDirection.RightToLeft:

Basic_Samples-FlowDirection.RightToLeft

var text = new TextBlock(new Font(14), SKColors.Black, "Hello world!");
canvas.DrawTextBlock(text, new SKRect(0, 0, 100, 0), null, FlowDirection.RightToLeft);

Word Wrap:

Basic_Samples-Word_Wrap

var text = new TextBlock(new Font(14), SKColors.Black, "Hello world!");
return canvas.DrawTextBlock(text, new SKRect(0, 0, 50, 0));

LineBreakMode.Center:

Basic_Samples-LineBreakMode.Center

var text = new TextBlock(new Font(14), SKColors.Black, "Hello world!", LineBreakMode.Center);
return canvas.DrawTextBlock(text, new SKRect(0, 0, 50, 0));

LineBreakMode.MiddleTruncation:

Basic_Samples-LineBreakMode.MiddleTruncation

var text = new TextBlock(new Font(14), SKColors.Black, "Hello world!", LineBreakMode.MiddleTruncation);
return canvas.DrawTextBlock(text, new SKRect(0, 0, 50, 0));

Word Wrap

animated animated

Basic Samples 2

Word Wrap - Tight:

Basic_Samples_2-Word_Wrap_-_Tight

var text = new TextBlock(new Font(14), SKColors.Black, "Hello world!");
return canvas.DrawTextBlock(text, new SKRect(0, 0, 20, 0));

Courier New:

Basic_Samples_2-Courier_New

var text = new TextBlock(new Font("Courier New", 14), SKColors.Black, "Hello world!");
return canvas.DrawTextBlock(text, new SKRect(0, 0, 100, 0));

Color and Size:

Basic_Samples_2-Color_and_Size

var text = new TextBlock(new Font(20), SKColors.Red, "Hello world!");
return canvas.DrawTextBlock(text, new SKRect(0, 0, 100, 0)); 

New line:

Basic_Samples_2-New_line

var text = new TextBlock(new Font(14), SKColors.Black, @"
(leading) new- line support...

Hello World!
SkiaSharp Rocks!");
return canvas.DrawTextBlock(text, new SKRect(0, 0, 150, 0));

New Line - Trailing:

Basic_Samples_2-New_Line_-_Trailing

var text = new TextBlock(new Font(14), SKColors.Black, @"Trailing new- line support:

");
return canvas.DrawTextBlock(text, new SKRect(0, 0, 150, 0));

Typeface Detection

Non-latin:

Typeface_Detection-Non-latin

var text = new TextBlock(new Font(14), SKColors.Black, "");
return canvas.DrawTextBlock(text, new SKRect(0, 0, 100, 0));

Cyrillic:

Typeface_Detection-Cyrillic

var text = new TextBlock(new Font(14), SKColors.Black, "yči");
return canvas.DrawTextBlock(text, new SKRect(0, 0, 100, 0));

Symbols:

Typeface_Detection-Symbols

var text = new TextBlock(new Font(14), SKColors.Black, "");
return canvas.DrawTextBlock(text, new SKRect(0, 0, 100, 0));

Unicode:

Typeface_Detection-Unicode

var text = new TextBlock(new Font(14), SKColors.Black, "🌐🍪🍕🚀");
return canvas.DrawTextBlock(text, new SKRect(0, 0, 100, 0));

Mixed:

Typeface_Detection-Mixed

var text = new TextBlock(new Font(14), SKColors.Black, "年či↺🚀مر");
return canvas.DrawTextBlock(text, new SKRect(0, 0, 100, 0));

Rtl Support:

Typeface_Detection-Rtl_Support

var text = new TextBlock(new Font(14), SKColors.Black, "مرحبا بالعالم");
return canvas.DrawTextBlock(text, new SKRect(0, 0, 100, 0), null, FlowDirection.RightToLeft);

Multi glyph:

Typeface_Detection-Multi_glyph

var text = new TextBlock(new Font(20), SKColors.Black, "น้ำ");
return canvas.DrawTextBlock(text, new SKRect(0, 0, 100, 0));

Rtl Word Wrap:

Typeface_Detection-Rtl_Word_Wrap

var text = new TextBlock(new Font(14), SKColors.Black, "مرحبا بالعالم");
return canvas.DrawTextBlock(text, new SKRect(50, 0, 100, 0), null, FlowDirection.RightToLeft);

Rich Text

Shorter:

Rich_Text-Shorter

var text = new RichTextBlock(
    new TextBlock(new Font(10), SKColors.Black, "Hello "),
    new TextBlock(new Font(20, true), SKColors.Black, "world! (bold)"),
    new TextBlock(new Font(16), SKColors.Green, "SkiaSharp Rocks!")
);
return canvas.DrawRichTextBlock(text, new SKRect(0, 0, 200, 0));

Longer:

Rich_Text-Longer

var text = new RichTextBlock(
    new TextBlock(new Font(16), SKColors.Green, "SkiaSharp Rocks!"),
    new TextBlock(new Font(10), SKColors.Black, "Hello "),
    new TextBlock(new Font(20, true), SKColors.Black, "world! "),
    new TextBlock(new Font(14), SKColors.Black, @"Trailing new-line support:

"),
    new TextBlock(new Font(16), SKColors.Green, "SkiaSharp Rocks!"),
    new TextBlock(new Font(14), SKColors.Black, "مرحبا بالعالم"),
    new TextBlock(new Font(16), SKColors.Green, "SkiaSharp Rocks!"),
    new TextBlock(new Font(14), SKColors.Black, ""),
    new TextBlock(new Font(16), SKColors.Green, "SkiaSharp Rocks!"),
    new TextBlock(new Font(14), SKColors.Black, "")
);
return canvas.DrawRichTextBlock(text, new SKRect(0, 0, 200, 0));

Lorum ipsum

default line spacing:

Lorum_ipsum-default_line_spacing

1.5x line spacing:

Lorum_ipsum-1.5x_line_spacing

About

SkiaSharp.TextBlock adds text block and rich text layout to SkiaSharp

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages