Skip to content

Instantly share code, notes, and snippets.

@dhrrgn
Last active September 8, 2015 21:23
Show Gist options
  • Save dhrrgn/8b9637f25ab0bcfc7306 to your computer and use it in GitHub Desktop.
Save dhrrgn/8b9637f25ab0bcfc7306 to your computer and use it in GitHub Desktop.
RCT_EXPORT_METHOD(measureTextRect:(NSString *)string withWidth:(double)width withFontName:(NSString *)fontName andFontSize:(double)fontSize andCallback:(RCTResponseSenderBlock)callback)
{
UIFont *font = [UIFont fontWithName:fontName size:fontSize];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
CGRect rect = [string boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
callback(@[
@{
@"height": [NSNumber numberWithFloat:rect.size.height],
@"width": [NSNumber numberWithFloat:rect.size.width],
}
]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment