NSString+CrStringDrawing.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef UI_GFX_IOS_NSSTRING_CRSTRINGDRAWING_H_
  5. #define UI_GFX_IOS_NSSTRING_CRSTRINGDRAWING_H_
  6. #import <UIKit/UIKit.h>
  7. @interface NSString (CrStringDrawing)
  8. // Calculates and returns the bounding rect for the receiver drawn using the
  9. // given size and font.
  10. // This method is implemented as a wrapper around
  11. // |boundingRectWithSize:options:attributes:context:| using the following values
  12. // for the parameters:
  13. // - size: the provided |size|
  14. // - options: NSStringDrawingUsesLineFragmentOrigin
  15. // - attributes: a NSDictionary with the provided |font|
  16. // - context: nil.
  17. //
  18. // Note that the rect returned may contain fractional values.
  19. - (CGRect)cr_boundingRectWithSize:(CGSize)size
  20. font:(UIFont*)font;
  21. // Convenience wrapper to just return the size of |boundingRectWithSize:font:|.
  22. //
  23. // Note that the size returned may contain fractional values.
  24. - (CGSize)cr_boundingSizeWithSize:(CGSize)size
  25. font:(UIFont*)font;
  26. // Returns the size of the string if it were to be rendered with the specified
  27. // font on a single line. The width and height of the CGSize returned are
  28. // pixel-aligned.
  29. //
  30. // This method is a convenience wrapper around sizeWithAttributes: to avoid
  31. // boilerplate required to put |font| in a dictionary of attributes. Do not pass
  32. // nil into this method.
  33. - (CGSize)cr_pixelAlignedSizeWithFont:(UIFont*)font;
  34. // Deprecated: Use cr_pixelAlignedSizeWithFont: or sizeWithAttributes:
  35. // Provides a drop-in replacement for sizeWithFont:, which was deprecated in iOS
  36. // 7 in favor of -sizeWithAttributes:. Specifically, this method will return
  37. // CGSizeZero if |font| is nil, and the width and height returned are rounded up
  38. // to integer values.
  39. // TODO(lliabraa): This method was added to ease the transition off of the
  40. // deprecated sizeWithFont: method. New call sites should not be added and
  41. // existing call sites should be audited to determine the correct behavior for
  42. // nil |font| and rounding, then replaced with cr_pixelAlignedSizeWithFont: or
  43. // sizeWithAttributes: (crbug.com/364419).
  44. - (CGSize)cr_sizeWithFont:(UIFont*)font;
  45. // If |index| is 0, returns an empty string.
  46. // If |index| is >= than self.length, returns self.
  47. // Otherwise, returns string cut to have |index| characters with an
  48. // ellipsis at the end.
  49. - (NSString*)cr_stringByCuttingToIndex:(NSUInteger)index;
  50. // Returns an elided version of string that fits in |bounds|.
  51. // System font of Label size is used for determining the string drawing size.
  52. - (NSString*)cr_stringByElidingToFitSize:(CGSize)bounds;
  53. @end
  54. #endif // UI_GFX_IOS_NSSTRING_CRSTRINGDRAWING_H_