In this recipe, we will learn how to draw styled text with an outline on a view.
Create a new Single View Application in Xamarin Studio and name it DrawTextApp
. Add a custom view to the project, similar to the one we created in the previous recipe, and name it DrawingView
.
Perform the following steps:
Implement the following
Draw
method override in theDrawingView
class:CGContext context = UIGraphics.GetCurrentContext(); PointF location = new PointF(10f, 100f); UIFont font = UIFont.FromName("Verdana-Bold", 28f); NSString drawText = new NSString("This text is drawn!"); context.SetTextDrawingMode(CGTextDrawingMode.Stroke); context.SetStrokeColorWithColor(UIColor.Black.CGColor); context.SetLineWidth(4f); drawText.DrawString(location, font); context.SetTextDrawingMode(CGTextDrawingMode.Fill); context.SetFillColorWithColor(UIColor.Yellow.CGColor); drawText.DrawString(location, font);
In the
ViewDidLoad
method of the controller, initialize and display theDrawingView...