AliSoftware / OHAttributedLabel

UILabel that supports NSAttributedString
https://github.com/AliSoftware/OHAttributedLabel/wiki
1.51k stars 344 forks source link

NSMutableAttributedString setFont functions crash with iOS 6 drawing functions #89

Open newacct opened 12 years ago

newacct commented 12 years ago

Just a heads-up.

NSAttributedStrings with the font attribute set using the old Core Text functions (which the methods in the NSAttributedString attributes category included with this project use), when drawn using the new iOS 6 NSAttributedString drawing functions, will crash.

NSMutableAttributedString* foo = [NSMutableAttributedString attributedStringWithString:@"foo"];
[foo setFontName:@"Helvetica" size:13];
[foo boundingRectWithSize:CGSizeMake(320, 50) options:0 context:nil]; // CRASH

It appears that Apple assumed that the attribute value would be a UIFont object, when the documentation for the Core Text font attribute (still) says that it needs to be a CTFont object.

Although this is an iOS bug and not you guys' fault, you should probably still keep this in mind.

AliSoftware commented 12 years ago

Thanks for the heads-up, I recentrly had this kind of issue reported and didn't have time to check yet but was already wondering why a crash would occur for that since iOS6… you solved the mystery before I even tried to :)

What do you mean by "the old Core Text functions", is there any new function since iOS6 to do that, that would replace the current way we used to do it in iOS5? Didn't have time to check this out but if you have any info on any new way to do, I am interested!

AliSoftware commented 12 years ago

OK I got it, you talk about the new API to draw attributed strings (namely the NSAttributedString UIKit Additions, not any new API to build and NSMutableString and change its attributes like its font and so on.

It seems that when using CoreText to draw the NSAttributedString it expects a CTFont, whereas using UIKit to draw the NSAttributedString (using the new API, or using the built NSAttributedString in an UITextView in iOS6 or similar too probably) expect a UIFont… not very consistent!

Not sure how to address that, as when you build your NSAttributedString using my category, the class to use (UIFont vs. CTFont) will depend on if you intend to draw it using CoreText or using UIKit… and those are not toll-free-bridged :-(

newacct commented 12 years ago

Yeah actually I found this when I constructed the NSAttributedString with your category, and then try to use it with UILabel on iOS 6, because UILabel on iOS 6 supports attributed strings (coincidentally, also using the attributedText property). I was thinking, well, if they have it natively, why not use it when available?

No problem occurs if I continue to use OHAttributedLabel. So this is not a direct concern.

But it is unfortunate, that it seems that attributed strings with attributes set from Core Text must be drawn with Core Text; and attributed strings with the new attributes defined in iOS 6 (https://developer.apple.com/library/ios/Documentation/UIKit/Reference/NSAttributedString_UIKit_Additions/Reference/Reference.html#//apple_ref/doc/uid/TP40011688-CH1-DontLinkElementID_2) must be drawn using the new NSAttributedString UIKit drawing functions, or put in a UILabel. But you can't mix them or it will crash.

AliSoftware commented 12 years ago

Yes I see the problem. CoreText expects kCTFontNameAttribute to contain a CTFontRef but UIKit and its drawing methods to draw NSAttributedStrings behave like this attribute should be an UIFont instead of a CTFontRef.

Thanks again for your feedback on this!

As this is an iOS bug, I encourage you to report the bug to Apple (as I just did).

jpredham commented 11 years ago

Just a note: this issue is still ongoing as of September 2013. We only discovered this through provisioning and distributing for ad-hoc deployment - strangely it did not manifest itself when run in development.

AliSoftware commented 11 years ago

Sadly enough I can't do anything about that :(

Apple uses the same key for the font attribute both for CoreText that expects a CTFontRef and for UIKit that expects an UIFont. They should have used a different constant value for the key or test it's type before in their internal code… as they didn't I don't see a solution to make this compatible with everything :-(