Skip to content
This repository has been archived by the owner on Oct 20, 2022. It is now read-only.

Latest commit

 

History

History

draw_unicode_text_with_coretext

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
id title brief samplecode sdk
42E062A0-761A-4C3F-00D0-D8A2E1D1BA0C
Draw Unicode Text with CoreText
This recipe shows how to draw text using Core Text.

Recipe

  1. In a UIView subclass override Draw and set graphics context state.
var gctx = UIGraphics.GetCurrentContext ();
gctx.TranslateCTM (10, 0.5f * Bounds.Height);
gctx.ScaleCTM (1, -1);
gctx.RotateCTM ((float)Math.PI * 315 / 180);
gctx.SetFillColor (UIColor.Green.CGColor);
  1. Create a Unicode text string.
string someText = "你好世界";
  1. Create an NSAttributedString.
var attributedString = new NSAttributedString (someText,
       new CTStringAttributes{
	      ForegroundColorFromContext =  true,
	      Font = new CTFont ("Arial", 24)
});
  1. Pass the NSAttributedString to a CTLine and draw the CTLine.
using (var textLine = new CTLine (attributedString)) {
       textLine.Draw (gctx);
}

Additional Information

You can draw text using the Core Text framework by creating an NSAttributedString instance; passing it to a CTLine instance and calling Draw on the CTLine. Otherwise, normal Core Graphics techniques such as obtaining a graphics context and manipulating the current transformation matrix apply. This example draws text of a given font and color and rotates it as shown above.