【问题标题】:UIView Background color when DrawingUIView绘制时的背景色
【发布时间】:2013-06-25 19:54:52
【问题描述】:

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);
        }
    }

编辑:我的 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);

        }
    }
}

【问题讨论】:

    标签: iphone ios uiview xamarin.ios xamarin


    【解决方案1】:

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

    CGContextClearRect(context, rect);
    

    或设置为白色:

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

    【讨论】:

    • 还要确保将自定义视图的背景颜色设置为clear。它默认为黑色,因此是一个额外的必需步骤。
    【解决方案2】:

    使用此代码。

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

    这是实现的代码。谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-02
      • 2020-04-21
      相关资源
      最近更新 更多