2013-06-25 91 views
2

的Objective-C或C#.NET答案精UIView的背景顏色繪製

時,我有一個的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); 

     } 
    } 
} 

回答

6

您可以清除與背景:

CGContextClearRect(context, rect); 

或設爲白搭配:

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

還要確保您的自定義視圖的背景顏色設置爲'clear'。它默認爲黑色,因此是一個額外的必需步驟。 –

0

使用此代碼。

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

實現此code.Thanks