MDHTMLLabel icon indicating copy to clipboard operation
MDHTMLLabel copied to clipboard

Set frame width/height depends on htmlText

Open haxinc opened this issue 10 years ago • 1 comments

Hi

Its great one, but i'm asking how to take the CGSize for htmlText to set the frame depends on it

I used this one but it doesn't make what I need

NSString *titlec = [NSString stringWithFormat:@"<b>%@</b> %@",thecomment.user.username,thecomment.text];
CGSize size = [self frameForText:titlec sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:12.0] constrainedToSize:CGSizeMake(300.f, CGFLOAT_MAX)];

-(CGSize)frameForText:(NSString*)text sizeWithFont:(UIFont*)font constrainedToSize:(CGSize)size{

    NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                          font, NSFontAttributeName,
                                          nil];
    CGRect frame = [text boundingRectWithSize:size
                                      options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                   attributes:attributesDictionary
                                      context:nil];

    // This contains both height and width, but we really care about height.
    return frame.size;
}

haxinc avatar Mar 27 '14 14:03 haxinc

what were your results? too big of a size? I haven't tried to do this myself but you most likely you need to use the attributed string from the label after setting the html since it's modifies the incoming html string to produce the attributed string.

aprato avatar Mar 27 '14 17:03 aprato