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. |
|
|
- In a
UIView
subclass overrideDraw
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);
- Create a Unicode text string.
string someText = "你好世界";
- Create an
NSAttributedString
.
var attributedString = new NSAttributedString (someText,
new CTStringAttributes{
ForegroundColorFromContext = true,
Font = new CTFont ("Arial", 24)
});
- Pass the
NSAttributedString
to aCTLine
and draw the CTLine.
using (var textLine = new CTLine (attributedString)) {
textLine.Draw (gctx);
}
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.