0

I can get the string size using This method

[myString sizeWithAttributes:@{NSFontAttributeName :myFont}

but, my string has HTML content , I tried to do it like this

[myString sizeWithAttributes:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSFontAttributeName :myFont}

but,I got a wrong value ? also I tried to use NSAttributedString like this

NSAttributedString * tabText = [[NSAttributedString alloc] initWithData:[myString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,myFont: NSFontAttributeName} documentAttributes:nil error:nil];
CGSize mySize=[tabText size];

And also I got a wrong value .

my string is very basic HTML content like "test & test" it returns the width of 15 chars not 11

Ahd Radwan
  • 1,090
  • 4
  • 14
  • 31

1 Answers1

1

Try removing the html tags first, use this as reference - Remove HTML Tags from an NSString on the iPhone

Then get the string size, try this:

CGSize stringSize = [YOUR_STRING sizeWithAttributes: @{NSFontAttributeName: [UIFont systemFontOfSize:17.0f]}];

// ceilf - is used to get equivalent values
CGSize currentSize = CGSizeMake(ceilf(stringSize.width), ceilf(stringSize.height));
Community
  • 1
  • 1