Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

iOS 6.0 crash on boundingRectWithSize: #217

Closed
kubbing opened this Issue · 20 comments

7 participants

@kubbing

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
                                              baseURL:nil
                                   documentAttributes:NULL];
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

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…

@Cocoanetics
Owner

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

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

@Cocoanetics
Owner

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

@cmicali

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

Just remember to add the following in your options dictionary:

[NSNumber numberWithBool:YES], DTUseiOS6Attributes
@magmatic

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.

@Cocoanetics
Owner

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

@Fosile

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,
                                nil];


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)
                                                 context:nil];


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;


 }
@Cocoanetics
Owner

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

@Fosile

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

@magmatic

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

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...

@Cocoanetics
Owner

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

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

@Fosile

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:
CGFLOAT_WIDTH_UNKNOWN
CGFLOAT_Height_UNKNOWN
to calculate the correct size....

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

@mapedd

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

@Fosile

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

but i cant find them anywhere??

@lukasseriously

@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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.