Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble adding text to a UIImageView using DrawString (MonoTouch)
    text
    copied!<p><br> I want to write text on to an image on an iPhone, using C# and MonoTouch. The DrawImage method seems pretty straight forward, but I get errors when I run it. What am I missing? Any help is greatly appreciated!<br> Thanks in advance. </p> <p>The errors:</p> <pre><code>Thu Feb 3 12:40:34 MacMini.local ImageTextMT[3877] &lt;Error&gt;: CGContextSetFont: invalid context 0x0 Thu Feb 3 12:40:34 MacMini.local ImageTextMT[3877] &lt;Error&gt;: CGContextSetTextMatrix: invalid context 0x0 Thu Feb 3 12:40:34 MacMini.local ImageTextMT[3877] &lt;Error&gt;: CGContextSetFontSize: invalid context 0x0 Thu Feb 3 12:40:34 MacMini.local ImageTextMT[3877] &lt;Error&gt;: CGContextSetTextPosition: invalid context 0x0 Thu Feb 3 12:40:34 MacMini.local ImageTextMT[3877] &lt;Error&gt;: CGContextShowGlyphsWithAdvances: invalid context 0x0 </code></pre> <p>MonoDevelop 2.4.2, MonoTouch </p> <ol> <li><p>I created a New Solution: C#, iPhone and iPad, iPhone Window-based Project</p></li> <li><p>Editted the MainWindow.xib:<br> 2a. Added UIImageView and UIButton to the Window;<br> 2b. To the AppDelegate I added two outlets, btnAddText (referenced to the UIButton) and imgImage (referenced to the UIImageView); </p></li> <li><p>Code (sorry-- code-formatting challenged...):</p> <pre><code>using System; using System.Collections.Generic; using MonoTouch.Foundation; using MonoTouch.UIKit; using MonoTouch.CoreGraphics; using System.Drawing; namespace ImageTextMT; { public class Application { static void Main (string[] args) { UIApplication.Main (args); } } // The name AppDelegate is referenced in the MainWindow.xib file. public partial class AppDelegate : UIApplicationDelegate { // This method is invoked when the application has loaded its UI and its ready to run public override bool FinishedLaunching (UIApplication app, NSDictionary options) { // If you have defined a view, add it here: // window.AddSubview (navigationController.View); window.MakeKeyAndVisible (); // Add the events for the controls btnAddText.TouchDown += btnAddText_Click; imgImage.Image = UIImage.FromFile("/Users/ParisRat/Projects/ImageTextMT/ImageTextMT/images/testblock.png"); return true; } protected void btnAddText_Click (object sender, EventArgs e) { // Define a rectangle System.Drawing.RectangleF rect = new System.Drawing.RectangleF(10.0f, 5.0f, 20.0f, 25.0f); // *** Write text on to the image *** this.imgImage.DrawString("HELLO",rect,UIFont.SystemFontOfSize(15.0f), UILineBreakMode.CharacterWrap, UITextAlignment.Left); } // This method is required in iPhoneOS 3.0 public override void OnActivated (UIApplication application) { } } } </code></pre></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload