UIView绘图时的背景颜色

时间:2013-06-25 19:54:53

标签: iphone ios uiview xamarin.ios xamarin

Objective-C或C#.Net答案很好

我有一个 UIView ,我在其中绘制文字。背景为黑色。如何使其白色或清除

    public override void Draw(RectangleF rect)
    {   
        using (var context = UIGraphics.GetCurrentContext())
        {
            context.ScaleCTM(1f,-1f);
            context.SetFillColor(Settings.ColorButtonText.CGColor);
            context.SetTextDrawingMode(CGTextDrawingMode.Fill);
            context.SelectFont("HelveticaNeue-Bold",20f, CGTextEncoding.MacRoman);
            context.ShowTextAtPoint(5,-25,_title);
        }
    }

enter image description here

编辑:我的Monotouch.Dialog自定义部分解决方案

public class BigBlueSectionHeaderView : UIView
{
    private readonly string _title;

    public BigBlueSectionHeaderView(string title): base(new RectangleF(0,0,320,35))
    {
        _title = title;
    }

    public override void Draw(RectangleF rect)
    {   
        using (var context = UIGraphics.GetCurrentContext())
        {
            context.SetFillColorWithColor(UIColor.White.CGColor);
            context.FillRect(new RectangleF(10, 0, 320, 35));
            context.ScaleCTM(1f,-1f);
            context.SetFillColor(Settings.ColorButtonText.CGColor);
            context.SetTextDrawingMode(CGTextDrawingMode.Fill);
            context.SelectFont("HelveticaNeue-Bold",20f, CGTextEncoding.MacRoman);
            context.ShowTextAtPoint(5,-25,_title);

        }
    }
}

2 个答案:

答案 0 :(得分:6)

您可以使用以下方式清除背景:

CGContextClearRect(context, rect);

或设置为白色:

CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextFillRect(context, rect);

答案 1 :(得分:0)

使用此代码。

CGContextClearRect(context, rect);
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextFillRect(context, rect);

这是实现的代码。谢谢