UILabel+Size.m 827 B

1234567891011121314151617181920212223242526272829303132
  1. //
  2. // UILabel+Size.m
  3. // shiku_im
  4. //
  5. // Created by JayLuo on 2020/4/29.
  6. // Copyright © 2020 Reese. All rights reserved.
  7. //
  8. #import "UILabel+Size.h"
  9. @implementation UILabel (Size)
  10. + (CGFloat)getHeightByWidth:(CGFloat)width title:(NSString *)title font:(UIFont *)font
  11. {
  12. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
  13. label.text = title;
  14. label.font = font;
  15. label.numberOfLines = 0;
  16. [label sizeToFit];
  17. CGFloat height = label.frame.size.height;
  18. return ceil(height);
  19. }
  20. + (CGFloat)getWidthWithTitle:(NSString *)title font:(UIFont *)font {
  21. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1000, 0)];
  22. label.text = title;
  23. label.font = font;
  24. [label sizeToFit];
  25. CGFloat width = label.frame.size.width;
  26. return ceil(width);
  27. }
  28. @end