Search code examples
iosobjective-cios6uitextview

iOS 6 '-[UITextView setTextContainerInset:]: unrecognized selector sent to instance 0x1d68e600'


So far, i have only gotten one report of this crash:

'-[UITextView setTextContainerInset:]: unrecognized selector sent to instance 0x1d68e600'

and i'm not sure why this is happening. I'm unable to reproduce the issue. My actual line of code is this:

UITextView *someInfo = = [[UITextView alloc] initWithFrame:CGRectMake(x, y, 100, 30)];
someInfo.textContainerInset = UIEdgeInsetsMake(0, HORIZONTAL_PADDING, 0, HORIZONTAL_PADDING);

But i don't understand why it is saying the selector is not sent properly. I did also notice that the bug crashed on someone's iphone 4, when they were runnig ios 6.1.3. Is this something specific to pre iOS 7??


Solution

  • Key points:

    1. Here it will not even compile if you are build your application in less than iOS 7.0 version. It will show an error of unrecognized selector sent to instance.

    2. But you have mention in your question that “I did also notice that the bug crashed on someone's iphone 4”. From that i can assume that you are creating build in iOS 7.0 or later.

    3. The issue is because of the backward compatibility. You need to take care of this type of things when you allow your application to run on earlier version.

    You have to implement conditions to check version compatibility.

    #if __IPHONE_6_0 || __IPHONE_6_1
    
    #else
    
    #endif
    

    EDIT:

    For Pre iOS 7.0 you need to use NSAttributedString and NSMutableParagraphStyle.

    For the left and right margins don't add your plain text right away but use an NSAttributedString instead with properly set left and right indents with an NSMutableParagraphStyle:

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.headIndent = 20.0;
    paragraphStyle.firstLineHeadIndent = 20.0;
    paragraphStyle.tailIndent = -20.0;
    
    NSDictionary *attrsDictionary = @{NSFontAttributeName: [UIFont fontWithName:@"TrebuchetMS" size:12.0], NSParagraphStyleAttributeName: paragraphStyle};
    textView.attributedText = [[NSAttributedString alloc] initWithString:myText attributes:attrsDictionary];
    

    Above code is straight from this answer:

    Set padding in UITextView