HTML TO NSSstring
Sometimes we need to display HTML data to UIlabel for this we have to change into NSString, otherwise, we have to use web view for displaying data without loss of any original data. when we display HTML content to UILabel then we have to calculate its length also otherwise we can’t set another view on the bottom of UILabel.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
NSAttributedString *result = [[NSAttributedString alloc] initWithData:[ htmlData dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil]; UILabel *shortDescription = [[UILabel alloc] initWithFrame:CGRectMake(5, 10, _mainView.frame.size.width-10, 20)]; [shortDescription setText:result.string]; [shortDescription setFont:[UIFont fontWithName:@"Trebuchet MS" size:19.0f]]; [shortDescription setLineBreakMode:NSLineBreakByWordWrapping]; shortDescription.numberOfLines = 0; shortDescription.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; shortDescription.textAlignment = NSTextAlignmentLeft; shortDescription.preferredMaxLayoutWidth = _mainView.frame.size.width-10; [shortDescription sizeToFit]; [_mainView addSubview:shortDescription]; |
1: here htmlData contains your HTML content coming from the server.
2: calculate height of UILabel : shortDescription.frame.size.height;
it will return actual height of UIlabel.