2012-04-21 73 views
0

我想在MonoMac中绘制一些文本,但没有成功。 在提供的示例中,绘制了圆,但文本未显示。在MonoMac中绘制文本

var context = NSGraphicsContext.CurrentContext.GraphicsPort; 
context.SetStrokeColor (new CGColor(1.0f, 0f, 0f)); // red 
context.SetLineWidth (1.0F); 
context.StrokeEllipseInRect (new RectangleF(5, 5, 10, 10)); 
context.SetTextDrawingMode(CGTextDrawingMode.Stroke); 
context.TextPosition = new PointF(0f, 0f); 
context.ShowText("My text"); // is not shown 

感谢

回答

0

你只需要指定要使用的字体。

public override void DrawRect (RectangleF dirtyRect) 
{ 
    var context = NSGraphicsContext.CurrentContext.GraphicsPort; 

    context.SetStrokeColor (new CGColor(1.0f, 0f, 0f)); // red 
    context.SetLineWidth (1.0F); 
    context.StrokeEllipseInRect (new RectangleF(5, 5, 10, 10)); 
    context.SetTextDrawingMode(CGTextDrawingMode.Stroke); 
    context.TextPosition = new PointF(0f, 0f); 
    context.SelectFont ("Arial", 5, CGTextEncoding.MacRoman); 
    context.ShowText("My text"); 
} 
0

你只需要重写drawRect。

public override void DrawRect (RectangleF dirtyRect) 
    { 
     NSString s = new NSString ("test"); 
     s.DrawString (new PointF(25,100), new NSDictionary()); 
    } 

,如果你想自定义,这里有一个good reference