Bug 17691 - MonoTouch.UIKit.UIStringAttributes.set_Font bug
Summary: MonoTouch.UIKit.UIStringAttributes.set_Font bug
Status: RESOLVED INVALID
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 5.0
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-02-10 15:09 UTC by guivho
Modified: 2014-02-10 15:55 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED INVALID

Description guivho 2014-02-10 15:09:17 UTC
Following method crashes on (iPad running) iOS 5.1.1.

    private void DrawChar (CGContext g, RectangleF frame, string c, StateType state)
    {
      var h = Convert.ToInt32 (frame.Height * .8);
      var s = new NSString (c);
      var size = MCiOS.TextSize (UIFont.BoldSystemFontOfSize (h), c.ToString ());
      var p = new PointF (frame.X + (frame.Width - size.Width) / 2, frame.Y + (frame.Height - size.Height) / 2);
      var font = UIFont.BoldSystemFontOfSize(h);
      Debug.Assert(font != null, "Failed to get font of size " + h);
      s.DrawString (p, new UIStringAttributes {
        Font = font,
        ForegroundColor = _textColors [(int)state],
      });
    }

The 'Font = font' line is painted green.

Initially I did not use the intermediate variable but changed the code
so that I could check on the font being null. Turns out it isn't: I get
{Helvetica-Bold 56}.

The details of the exception seem to indicate that it really crashes
inside MonoTouch:

System.ArgumentNullException: Argument cannot be null.
Parameter name: key
  at MonoTouch.Foundation.DictionaryContainer.SetNativeValue (MonoTouch.Foundation.NSString key, INativeObject value, Boolean removeNullValue) [0x0002f] in /Developer/MonoTouch/Source/maccore/src/Foundation/.pmcs-compat.DictionaryContainer.cs:343
  at MonoTouch.UIKit.UIStringAttributes.set_Font (MonoTouch.UIKit.UIFont value) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/UIKit/.pmcs-compat.UIStringAttributes.cs:78
  at XOOX.Classes.PuzzleView.DrawChar (MonoTouch.CoreGraphics.CGContext g, RectangleF frame, System.String c, StateType state) [0x000ad] in /Users/guivho/Mono/XOOX/XOOX/Classes/PuzzleView.cs:332
  at XOOX.Classes.PuzzleView.DrawCell (RectangleF frame, System.String c, TrailType trail, StateType state, System.Collections.Generic.List`1 dots) [0x00066] in /Users/guivho/Mono/XOOX/XOOX/Classes/PuzzleView.cs:294
  at XOOX.Classes.PuzzleView.DrawPuzzle () [0x00163] in /Users/guivho/Mono/XOOX/XOOX/Classes/PuzzleView.cs:163
  at XOOX.Classes.PuzzleView.Draw (RectangleF rect) [0x00002] in /Users/guivho/Mono/XOOX/XOOX/Classes/PuzzleView.cs:112
  at at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/.pmcs-compat.UIApplication.cs:38
  at XOOX.Application.Main (System.String[] args) [0x00008] in /Users/guivho/Mono/XOOX/XOOX/Main.cs:15

This code runs fine on the 7.0.4 but my app is targeting 4.3 as minimum version.
Comment 1 Sebastien Pouliot 2014-02-10 15:55:34 UTC
The class `UIStringAttributes` is decorated with a [Since(6,0)] attribute*. This means the API used are not available before iOS 6.0.

If you need to support older versions of iOS you'll need to ensure you're only using API that are available for those versions. Targetting iOS 4.3 can be very restrictive.

* Note that some members of UIStringAttributes are only available in iOS 7.0+ (and are decorated with [Since(7,0)].