Search code examples
iosobjective-cdtcoretext

Transparent background & Auto resize DTAttributedTextView


I used the following codes to apply the contents the DTAttributedTextView:

NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *builderOptions = @{DTDefaultFontFamily: @"Helvetica", DTUseiOS6Attributes: [NSNumber numberWithBool:YES]};
NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTMLData:data options:builderOptions  documentAttributes:nil];
DTCoreTextLayouter *layouter = [[DTCoreTextLayouter alloc] initWithAttributedString:attrString];
CGRect maxRect = CGRectMake(10, 20, CGFLOAT_WIDTH_UNKNOWN, CGFLOAT_HEIGHT_UNKNOWN);
NSRange entireString = NSMakeRange(0, [attrString length]);
DTCoreTextLayoutFrame *layoutFrame = [layouter layoutFrameWithRect:maxRect range:entireString];
CGSize sizeNeeded = [layoutFrame frame].size;
[cell.lbl setContentSize:sizeNeeded];
[cell.lbl setAttributedString:attrString];

where the above codes are in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath and cell.lbl is a UIScrollView with custom class DTAttributedTextView.

However, the output result is that the DTAttributedTextView is in white background and does not resize according to setContentSize.

What do I miss?

Note: Using iOS 7 SDK, DTCoreText 1.6.12 (via Cocoapods)


Solution

  • I finally figured out an easy way to make the background transparent.

    Add this line at the end of the code:

    [cell.lbl setBackgroundColor:[UIColor clearColor]];