Bug 32891 - Bad binding for NSAttributedString attributes
Summary: Bad binding for NSAttributedString attributes
Status: RESOLVED FIXED
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Library (Xamarin.Mac.dll) ()
Version: 2.0.0
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Chris Hamons
URL:
Depends on:
Blocks:
 
Reported: 2015-08-08 13:01 UTC by Frank A. Krueger
Modified: 2015-09-29 17:32 UTC (History)
3 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 FIXED

Description Frank A. Krueger 2015-08-08 13:01:08 UTC
NSAttributedString takes attributes as a dictionary.

XamMac offers two overloads: from NSDictionary and from CTStringAttributes.

I used to use NSDictionary like so:

return NSDictionary.FromObjectsAndKeys (
	new NSObject[] { NSFont.FromFontName (name, size), color },
	new NSObject[] { NSAttributedString.FontAttributeName, NSAttributedString.ForegroundColorAttributeName });

However, the latest Xam Mac breaks this code (why?)

I cannot find FontAttributeName anywhere.

The only overload is the CTStringAttributes - but that doesn't work at all with AppKit - it's meant for CoreText. For example, colors should be NSColors, but CTStringA forces CGColor. Result: no colors.

Please please either do what iOS did and have a binding to the NSStringAttributes, or bring back the attribute names.

Example code: https://github.com/praeclarum/NGraphics/blob/master/NGraphics.Editor/Style.cs






=== Xamarin Studio ===

Version 5.9.4 (build 5)
Installation UUID: fce13fdd-e8e3-48ef-99f1-4acbb06f0240
Runtime:
	Mono 4.0.2 ((detached/c99aa0c)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 400020005

=== Xamarin.Android ===

Version: 5.1.4.16 (Enterprise Edition)
Android SDK: /Users/fak/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		2.3   (API level 10)
		4.0.3 (API level 15)
		4.3   (API level 18)
		4.4   (API level 19)
		5.0   (API level 21)
Java SDK: /usr
java version "1.8.0_20-ea"
Java(TM) SE Runtime Environment (build 1.8.0_20-ea-b23)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b22, mixed mode)

=== Xamarin Android Player ===

Version: Unknown version
Location: /Applications/Xamarin Android Player.app

=== Apple Developer Tools ===

Xcode 6.4 (7720)
Build 6E35b

=== Xamarin.Mac ===

Version: 2.0.2.35 (Enterprise Edition)

=== Xamarin.iOS ===

Version: 8.10.3.2 (Enterprise Edition)
Hash: 8b265d6
Branch: master
Build date: 2015-06-30 15:19:13-0400

=== Build Information ===

Release ID: 509040005
Git revision: 8010a90f6e246b32364e3fb46ef2c9d1be9c9a2b
Build date: 2015-06-08 16:52:06-04
Xamarin addins: 7e93e9c3503f28770f23ce1b7eafd829919f18e8

=== Operating System ===

Mac OS X 10.10.4
Darwin muon.local 14.4.0 Darwin Kernel Version 14.4.0
    Thu May 28 11:35:04 PDT 2015
    root:xnu-2782.30.5~1/RELEASE_X86_64 x86_64
Comment 1 Frank A. Krueger 2015-08-08 13:08:42 UTC
It would seem that there is an NSStringAtrbiutes class! Yay! :-)

BUT the AddAttributes method of NSAttributedString does not accept an argument of that type. :-(
Comment 2 Timothy Risi 2015-08-09 00:24:39 UTC
Hi Frank,

I'm glad you found the NSStringAttributes class.  The actual change to use NSStringAttributes happened with the Unified release  of XM 2.0 (Classic still uses the old version).   Looking back at the release notes it looks like that change didn't get documented.  I can also confirm that it's missing the overload for AddAttributes, so we'll definitely get that added.
Comment 3 Frank A. Krueger 2015-08-10 13:30:00 UTC
There was a breaking change in the last few months that took away consts. That was not documented either. (Please see NGraphics for proof, it had builds using NSDictionary with Unified, which is not possible anymore.)
Comment 4 Chris Hamons 2015-08-17 15:02:19 UTC
@Frank / @Tim - We _should not_ break the stable API unless the existing API was so wrong that it is unusable AND we can't fix it without breaking the API (and then we document those).

We normally run an API diff to catch these, but somehow we missed this. I'm going to track it down shortly. Apologies for the pain.
Comment 5 Chris Hamons 2015-08-17 15:27:56 UTC
Frank - Do you happen to know what version of Xamarin.Mac you had before this that worked? 

- If you had 2.0 and had just upgraded, I'd expect the previous version was 1.10 or 1.12
- The 1.12 release notes had [1]:
      - This will be the last Xamarin.Mac release in which the Unified API will be considered unstable. We do not anticipate any major changes (the parts shared with iOS are already frozen) but we will be removing some deprecated APIs and doing some fine tuning to the Mac Unified API in the coming months.
- Since [2] (before your fix) has "using AppKit;" it appears to be Unified XM, which would be before we froze Unified.

If that is the case, then it technically isn't an API break, since it was breaking an API we didn't consider stable at the time. 

I took the version of that file before your changes to fix the API break:

https://github.com/praeclarum/NGraphics/commit/5e601a01aed86776e84274e0ff61d2ddc14438b0#diff-b5836dc0f9bde085352782bf1496d652

and fixed it to use NSStringAttributeKey in a few places, and it builds fine with the latest XM (2.0.2.111) and it appears to work for me.

https://gist.github.com/chamons/5d148e08b3ec589122c2

compared to your fix here:

https://github.com/praeclarum/NGraphics/commit/37d0323c1a269b9311d48aca59f963b0da3ed6c6

Am I missing something, or is this just:

NSAttributedString.ForegroundColorAttributeName -> NSStringAttributeKey.ForegroundColor mostly?



[1] http://developer.xamarin.com/releases/mac/xamarin.mac_1/xamarin.mac_1.12/
[2] https://github.com/praeclarum/NGraphics/blob/5e601a01aed86776e84274e0ff61d2ddc14438b0/NGraphics.Editor/Style.cs
Comment 6 Frank A. Krueger 2015-08-18 12:32:14 UTC
You can see the code i pointed to you to answer all the questions.

Yes the breaking change was NSAttributedString.ForegroundColorAttributeName ->
NSStringAttributeKey.ForegroundColor

Anyway, but I'm more concerned about the overload - right now we're stuck with CoreText attributes.
Comment 7 Chris Hamons 2015-09-29 17:32:07 UTC
So, from looking at it more closely, at least with latest Alpha builds, the only real breaking change were the key renames (when Unified was unstable).

I was able to roll back most of your API fix and just apply those to show that. PR is out. Feel free to merge or kill.