2011-04-19 38 views
4

在UIView子類中,我重寫Draw子來繪製一些自定義文本,但文本allways被繪製爲翻轉文本。MonoTouch:自定義繪圖文本allways繪製翻轉文本

這是代碼我使用:

class TextViewProblem : UIView 
{ 
public override void Draw (RectangleF rect) 
    { 
     base.Draw (rect); 

     CGContext g = UIGraphics.GetCurrentContext(); 

     UIColor.White.SetFill(); 

     g.SelectFont("Arial",16f,CGTextEncoding.MacRoman); 

     UIColor.White.SetFill(); 
     g.SetTextDrawingMode(CGTextDrawingMode.Fill); 
     g.ShowTextAtPoint(1,25,"Yiannis 123"); 



    } 

} 

這是這段代碼的輸出: enter image description here

爲什麼文本取用翻轉?

我運行: 的MonoDevelop 2.4.2 iPhone模擬器4.2 的MonoTouch 3.2.6

你可以下載一個項目,從這個鏈接重現此問題:www.grbytes.com/downloads/TextProblem.zip

+0

我發現在Draw重寫中我可以使用UIView的DrawString方法。它以這種方式繪製文本。 – 2011-10-25 02:20:27

回答

10

圖像翻轉,因爲CoreGraphics的座標系與UIKit的座標系不同,您需要應用一個包含翻轉渲染(x = 1,y = -1)的轉換,然後轉換高度(x = 0,y = height )。

您可以通過將轉換應用於圖形上下文來完成此操作。

+0

我試過這個沒成功: gctx.ScaleCTM(1,-1); gctx.TranslateCTM(0,this.Frame.Width); – Scarlaxx 2011-04-19 13:07:45

+0

我也試過g.ScaleCTM(1,-1); g.TranslateCTM(0,this.Frame.Height);但它不起作用。我得到一個黑色的UIView – 2011-04-19 13:33:23

+2

首先翻譯,然後縮放 – alecnash 2012-05-23 15:43:37

0

這裏有一個工作代碼:

 CGContext g = UIGraphics.GetCurrentContext(); 
     UIColor.White.SetFill(); 
     g.ScaleCTM(1f,-1f); 
     g.SelectFont("Arial",16f,CGTextEncoding.MacRoman); 
     UIColor.White.SetFill(); 
     g.SetTextDrawingMode(CGTextDrawingMode.Fill); 
     g.ShowTextAtPoint(1,-50,"Yiannis 123"); 

一旦您設置了規模爲-1 Y,現在你的座標系是上下顛倒。所以(0,0)是左上角,但是左下角現在是(0,-480),而不是(0,480)。所以請注意ShowTextAtPoint中的-50。

-1

此代碼工作正常:

public override void Draw (System.Drawing.RectangleF rect) 
    { 
     base.Draw (rect); 

     CGContext g = UIGraphics.GetCurrentContext(); 

     g.TranslateCTM(0,Bounds.Height); 
     g.ScaleCTM(1f,-1f); 
     g.DrawImage .......... 
    } 

尖端是:第一TranslateCTM然後ScaleCTM。

+0

我不明白downvote。我發佈的代碼作品。 – 2014-05-23 02:21:34

0
using (CGContext g = UIGraphics.GetCurrentContext()) { 
      g.ScaleCTM (1f, -1f); 
      g.TranslateCTM (0, -Bounds.Height); 
       ....