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

Already on GitHub? Sign in to your account

iOS 6.0 crash on boundingRectWithSize: #217

kubbing opened this Issue Sep 10, 2012 · 22 comments


None yet
8 participants

kubbing commented Sep 10, 2012

Hi in XCode 4.5, iOS 6.0 beta4,

I thing there is a problem with NSAttributedString generation The following code crashes on the last line.

NSString *htmlString = @"Ooooh, Mr. Protagonist(s)";

NSData *htmlData = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
NSAttributedString *string = [[NSAttributedString alloc] initWithHTMLData:htmlData
CGRect rect = [string boundingRectWithSize:CGSizeMake(300, 0) options:0 context:NULL];

With some weird bug.

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

I'd like to help, but I have no idea what could cause the problem.

kubbing commented Sep 10, 2012

I bypassed the crash by commenting out default values in - (BOOL)_buildString in DTHTMLAttributedStringBuilder.m.

But still I do not get the correct size from the method. There is one line only :/. I must be something missing…


odrobnik commented Sep 17, 2012

The boundingRectWithSize is part of iOS 6 which is not currently compatible with DTCoreText.

For example this might have trouble with the pre-iOS-6-style attributes.

It might expect the font to be specified as UIFont.

cmicali commented Nov 26, 2012

Kubbing could you post the code you used to bypass this crash? I am seeing it consistently on iOS 6.0


odrobnik commented Dec 11, 2012

please try again with my latest additions. There is a new option to make the output compatible with iOS 6 UITextViews: 46d2910

@odrobnik odrobnik closed this Dec 23, 2012

cmicali commented Jan 21, 2013

This appears to have solved the problem... thanks!

Just remember to add the following in your options dictionary:

[NSNumber numberWithBool:YES], DTUseiOS6Attributes

It would be great if this tip was included in the setup guide, that is, have the smoke test use initWithHTML:options:documentAttributes instead of initWithHTML:documentAttributes.


odrobnik commented Sep 14, 2013

@magmatic if you feel some addition is helpful then please provide it via pull request.

Fosile commented Nov 12, 2013

I ´m getting the same Problem. On iOS 6 it crashes with an Error:

-[__NSCFType lineBreakMode]: unrecognized selector sent to instance

My Code looks like this:

NOTICE: I´m using DTAttributeTextView

NSString *html = item.text;

NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];

CGFloat fontSizeMultiplier = 1.33;
CGFloat const DTCoreTextDefaultFontSize = 12.0;
NSString * fontName = @"Helvetica";

NSMutableDictionary *options = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:fontSizeMultiplier], NSTextSizeMultiplierDocumentOption,
                                fontName, DTDefaultFontFamily,
                                @"purple", DTDefaultLinkColor,
                                @"red", DTDefaultLinkHighlightColor,
                                [NSNumber numberWithBool:YES],DTUseiOS6Attributes,

NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTMLData:data options:options documentAttributes:nil];

NSLog(@"%@", attrString);

     self.textView.attributedString = attrString;
     self.textView.userInteractionEnabled = NO;

 CGSize size = CGSizeMake(320,CGFLOAT_MAX);

     CGRect sizeCalculation = [attrString boundingRectWithSize:size
                                                 options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)

self.textView.contentSize = CGSizeMake(320, sizeCalculation.size.height);
[self addSubview:self.textView];

     CGRect messageFrame = self.textView.frame;
     messageFrame.size.height = self.textView.contentSize.height;
     self.textView.frame = messageFrame;


odrobnik commented Nov 12, 2013

-boundingRectWithSize:options:context cannot be used with DTCoreText since this is a method by Apple.

Fosile commented Nov 12, 2013

Any Idea how i can calculate the correct size (specially the height) of my text which comes from an api?

Are you sure that it's the -boundingRectWithSize:options:context line that causes this error? I use the same method with the same options and it works fine on both iOS 6 and 7.

Fosile commented Nov 12, 2013

The error occurs on the line : context:nil]; of boundingRectWithSize..... The Error occurs only on iOS 6, not on iOS 7...

I need another Method for the size Calculation...its really a bad problem in my app...


odrobnik commented Nov 13, 2013

You need to enable iOS6 tags via parsing option to have an NSAttributedString that is compatible with iOS 6. You get the needed size by creating a DTCoreTextLayoutFrame and inspecting the frame property

Fosile commented Nov 14, 2013

Sry for my silly question, but is there any Demo about how to use DTCoreTextLayoutFrame? I´m not sure how tu use it?


odrobnik commented Nov 14, 2013

Fosile commented Nov 15, 2013

You are write: "If you specify CGFLOAT_HEIGHT_UNKNOWN the height will be calculated."

Do you mean it will be calculated automtically if i implement "CGFLOAT_HEIGHT_UNKNOWN" ??

I tried to implement the Code, but Xcode can´t find:
to calculate the correct size....

Whats the problem? I also tried to import "DTCoreTextLayoutFrame.h"

mapedd commented Nov 18, 2013

Those constants are defined in #import <DTCoreText/DTCoreTextConstants.h>

Fosile commented Nov 19, 2013

Thanks for the tip! but i still cant find the two needed constants. I searched the whole DTCoreTExt Project for: CGFLOAT_WIDTH_UNKNOWN

but i cant find them anywhere??

@Fosile , the same for me. I'm using cocoapods 1.6.8 version. But there is an update and 1.6.9 version is now available, and these constants are present there.

Fosile commented Nov 22, 2013

Ok now i found the constants, thank you very much for that! It works now as expected :)

k06a commented Aug 25, 2016

Used option DTUseiOS6Attributes to avoid crash, but have new crash on iOS 9.3.4:

-[DTImageTextAttachment attachmentCell]: unrecognized selector sent to instance 0x12abe44e0

odrobnik commented Aug 25, 2016

This is a result from DTCoreText not yet supporting NSTextAttachment. Needs a sponsor to report the old attachment support to be compatible with iOS.

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