Bug 23400 - Calling NSAttributedString constructor results in ArgumentNullException (key) after call to MonoTouch.UIKit.UIStringAttributes.set_Font on iOS 5.1.1 device
Summary: Calling NSAttributedString constructor results in ArgumentNullException (key)...
Status: RESOLVED FEATURE
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 8.0.0
Hardware: All All
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-09-26 05:10 UTC by Frank Buckley
Modified: 2014-09-26 07:51 UTC (History)
2 users (show)

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


Attachments
Example test (7.33 KB, application/x-zip-compressed)
2014-09-26 05:13 UTC, Frank Buckley
Details


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 FEATURE

Description Frank Buckley 2014-09-26 05:10:06 UTC
Invoking new NSAttributedString(<string>, <font>) results in an ArgumentNullException (paramater name: key) when executed on an iOS 5.1.1 iPad, but not on iOS 8.0 iPad, nor on iOS 8.0 simulator.

Example code:
--------------------------------
var font = UIFont.SystemFontOfSize (14.0f);
var text = new NSAttributedString ("Hello", font);
var line = new CTLine (text);
Debug.WriteLine ("Bounds = " + line.GetBounds(CTLineBoundsOptions.UseGlyphPathBounds));

Result on iOS 8.0:
--------------------------------
2014-09-26 10:07:12.687 NSAttributedStringTest[1340:24575] Bounds = {X=1.092,Y=-0.154,Width=31.066,Height=10.15}

Result on iOS 5.1.1:
--------------------------------
System.ArgumentNullException: Argument cannot be null.
Parameter name: key
  at MonoTouch.Foundation.DictionaryContainer.NullCheckAndRemoveKey (MonoTouch.Foundation.NSString key, Boolean removeEntry) [0x00024] in /Developer/MonoTouch/Source/maccore/src/Foundation/DictionaryContainer.cs:245
  at MonoTouch.Foundation.DictionaryContainer.SetNativeValue (MonoTouch.Foundation.NSString key, INativeObject value, Boolean removeNullValue) [0x00000] in /Developer/MonoTouch/Source/maccore/src/Foundation/DictionaryContainer.cs:349
  at MonoTouch.UIKit.UIStringAttributes.set_Font (MonoTouch.UIKit.UIFont value) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIStringAttributes.cs:77
  at MonoTouch.Foundation.NSAttributedString.ToDictionary (MonoTouch.UIKit.UIFont font, MonoTouch.UIKit.UIColor foregroundColor, MonoTouch.UIKit.UIColor backgroundColor, MonoTouch.UIKit.UIColor strokeColor, MonoTouch.UIKit.NSParagraphStyle paragraphStyle, NSLigatureType ligature, Single kerning, NSUnderlineStyle underlineStyle, MonoTouch.UIKit.NSShadow shadow, Single strokeWidth, NSUnderlineStyle strikethroughStyle) [0x00006] in /Developer/MonoTouch/Source/maccore/src/Foundation/NSAttributedString.cs:103
  at MonoTouch.Foundation.NSAttributedString..ctor (System.String str, MonoTouch.UIKit.UIFont font, MonoTouch.UIKit.UIColor foregroundColor, MonoTouch.UIKit.UIColor backgroundColor, MonoTouch.UIKit.UIColor strokeColor, MonoTouch.UIKit.NSParagraphStyle paragraphStyle, NSLigatureType ligatures, Single kerning, NSUnderlineStyle underlineStyle, MonoTouch.UIKit.NSShadow shadow, Single strokeWidth, NSUnderlineStyle strikethroughStyle) [0x00000] in /Developer/MonoTouch/Source/maccore/src/Foundation/NSAttributedString.cs:152
  at NSAttributedStringTest.AppDelegate.Test () [0x00024] in /dsedev/src/NSAttributedStringTest/NSAttributedStringTest/AppDelegate.cs:48
  at NSAttributedStringTest.AppDelegate.FinishedLaunching (MonoTouch.UIKit.UIApplication app, MonoTouch.Foundation.NSDictionary options) [0x00017] in /dsedev/src/NSAttributedStringTest/NSAttributedStringTest/AppDelegate.cs:37
  at at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:62
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0001c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:45
  at NSAttributedStringTest.Application.Main (System.String[] args) [0x00008] in /dsedev/src/NSAttributedStringTest/NSAttributedStringTest/Main.cs:17
Comment 1 Frank Buckley 2014-09-26 05:13:36 UTC
Created attachment 8204 [details]
Example test
Comment 2 Rolf Bjarne Kvinge [MSFT] 2014-09-26 06:43:44 UTC
This is because the UIKit additions to NSAttributedString (such as the font) didn't exist in iOS 5.1.1 (they were introduced in iOS 6 according to Apple's documentation): https://developer.apple.com/library/ios/documentation/UIKit/Reference/NSAttributedString_UIKit_Additions/index.html
Comment 3 Frank Buckley 2014-09-26 07:11:43 UTC
I am using Core Text to custom draw formatted text to avoid relying on UIKit additions to UILabel introduced in 6.0 in order to maintain support for 5.1.1.

Should I not be able to create an NSAttributedString with the font specified - the documentation states this is available since iOS 3.2 with kCTFontAttributeName:

https://developer.apple.com/library/ios/documentation/Carbon/Reference/CoreText_StringAttributes_Ref/#//apple_ref/doc/constant_group/String_Attribute_Name_Constants
Comment 4 Frank Buckley 2014-09-26 07:51:59 UTC
OK - apologies - I now see there is a constructor that takes a CTStringAttributes.