Question

I'm using the great TTTAttributedLabel (https://github.com/mattt/TTTAttributedLabel) which works fine under iOS 5. Under iOS 6 however, I get the error:

-[__NSCFType set]: unrecognized selector sent to instance 0x200020e0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-  [__NSCFType set]: unrecognized selector sent to instance 0x200020e0'

Having researched the issue a little, it would appear that the set message is being sent to an object which has been released. Using the debugger, I have po'd 0x200020e0 which appears to be a CTFontRef.

po 0x200020e0
(int) $0 = 536879328 CTFont <name: .HelveticaNeueUI-Bold, size: 20.000000, matrix: 0x0>
CTFontDescriptor <attributes: <CFBasicHash 0x20001900 [0x3c2a4100]>{type = mutable dict, count = 1,
entries =>
1 : <CFString 0x3be2a768 [0x3c2a4100]>{contents = "NSFontNameAttribute"} = <CFString 0x3c292c14 [0x3c2a4100]>{contents = ".HelveticaNeueUI-Bold"}

}

This led me straightaway to the code which sets up the TTTAttributedLabel:

 [label setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
    NSRange boldRange = [[mutableAttributedString string] rangeOfString:title options:NSCaseInsensitiveSearch];
    NSRange strikeRange = [[mutableAttributedString string] rangeOfString:@"sit amet" options:NSCaseInsensitiveSearch];


    UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:20];

    CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);

    if (font) {
        [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:boldRange];
        [mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(__bridge id)font range:boldRange];
        CFRelease(font);
    }

    return mutableAttributedString;
}];

as in the example usage here:

https://github.com/mattt/TTTAttributedLabel

That code isn't ARCified so I've added in bridged casts (see above). I've tried retains all over the place but that doesn't seem to solve the issue (which appears to be) that the CTFontRef is getting released too early (I think - other suggestions welcome).

Any ideas on how to solve this and why this only crops up under the iOS 6 simulator? Thanks in advance.

Was it helpful?

Solution 2

In the end it was just a stupid mistake -

[mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(__bridge id)font range:boldRange];

should have read:

[mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[UIColor blueColor].CGColor range:boldRange]; 

OTHER TIPS

Replaced:

CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);

with:

UIFont *font = [UIFont fontWithName:boldSystemFont.fontName size:boldSystemFont.pointSize];

Fixed the issue in iOS6 for me. Not tested on iOS 5.

I encountered the same error when I have a UILabel myLabel, and doing myLabel.attributedText = NSMutableAttributedString.

Try https://github.com/AliSoftware/OHAttributedLabel

Declaring:

@property (nonatomic, strong) IBOutlet OHAttributedLabel *myLabel;

instead of UILabel solves the error.

I managed to get this error when I was using a bad range adding attributes to an NSMutableAttributedString. Fixing the range fixed the crash!

EDIT: Also managed to get it by using UITextAttributeFont in the attributes dictionary which is apparently now deprecated in iOS 7 which just makes things crash mysteriously. The replacement is NSForegroundColorAttributeName.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top