Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NSMutableAttributedString setFont functions crash with iOS 6 drawing functions #89

Open
newacct opened this issue Sep 26, 2012 · 6 comments

Comments

@newacct
Copy link

newacct commented Sep 26, 2012

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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Author

newacct commented Sep 26, 2012

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
Copy link
Owner

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
Copy link

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
Copy link
Owner

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 :-(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants