Advertisement
Guest User

NSString+StringSizing

a guest
Oct 24th, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. //
  2. // NSString+StringSizing.m
  3. // Complete
  4. //
  5. // Created by Erik Villegas on 8/13/14.
  6. // Copyright (c) 2014 Complete. All rights reserved.
  7. //
  8.  
  9. #import "NSString+StringSizing.h"
  10.  
  11. @implementation NSString (StringSizing)
  12.  
  13. - (CGFloat) heightUsingFont:(UIFont *) font {
  14. NSParameterAssert(font);
  15. return [self sizeConstrainedToWidth:CGFLOAT_MAX height:CGFLOAT_MAX font:font].height;
  16. }
  17.  
  18. - (CGFloat) widthUsingFont:(UIFont *) font {
  19. NSParameterAssert(font);
  20. return [self sizeConstrainedToWidth:CGFLOAT_MAX height:CGFLOAT_MAX font:font].width;
  21. }
  22.  
  23. - (CGFloat) heightConstrainedToWidth:(CGFloat) width usingFont:(UIFont *) font {
  24. NSParameterAssert(font);
  25. return [self sizeConstrainedToWidth:width height:CGFLOAT_MAX font:font].height;
  26. }
  27.  
  28. - (CGFloat) widthConstrainedToHeight:(CGFloat) height usingFont:(UIFont *) font {
  29. NSParameterAssert(font);
  30. return [self sizeConstrainedToWidth:CGFLOAT_MAX height:height font:font].width;
  31. }
  32.  
  33. - (CGSize) sizeUsingFont:(UIFont *) font {
  34. NSParameterAssert(font);
  35. return [self sizeConstrainedToWidth:CGFLOAT_MAX height:CGFLOAT_MAX font:font];
  36. }
  37.  
  38. - (CGSize) sizeConstrainedToWidth:(CGFloat) width height:(CGFloat) height font:(UIFont *) font {
  39. NSParameterAssert(font);
  40. CGSize size = [self boundingRectWithSize:CGSizeMake(width, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : font} context:NULL].size;
  41. return CGSizeMake(ceilf(size.width), ceilf(size.height));
  42. }
  43.  
  44.  
  45. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement