GithubHelp home page GithubHelp logo

martinthesmith / skiasharp.textblocks Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wouterst79/skiasharp.textblocks

0.0 0.0 0.0 654 KB

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

License: MIT License

C# 100.00%

skiasharp.textblocks's Introduction

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

skiasharp.textblocks's People

Contributors

wouterattoptal avatar wouterst79 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.