(NSCFType set) - Unrecognized selector in iOS 6
Asked Answered
V

4

13

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.

Velazquez answered 21/9, 2012 at 15:32 Comment(0)
V
5

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]; 
Velazquez answered 11/4, 2013 at 15:13 Comment(0)
C
6

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.

Corabelle answered 28/3, 2013 at 11:3 Comment(0)
V
5

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]; 
Velazquez answered 11/4, 2013 at 15:13 Comment(0)
D
2

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.

Drinkwater answered 31/10, 2012 at 8:29 Comment(0)
K
1

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.

Kittrell answered 25/4, 2014 at 12:7 Comment(1)
Sorry, just noticed that the edit there doesn't make much sense.. Can't remember which keys I meant but you might be able to figure out in your situation. Basically don't use deprecated keys!Kittrell

© 2022 - 2024 — McMap. All rights reserved.