mylabels.m 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //
  2. // mylabels.m
  3. // YBVideo
  4. //
  5. // Created by zqm on 16/8/30.
  6. // Copyright © 2016年 cat. All rights reserved.
  7. //
  8. #import "mylabels.h"
  9. @implementation mylabels
  10. - (id)initWithFrame:(CGRect)frame {
  11. if (self = [super initWithFrame:frame]) {
  12. self.verticalAlignment = VerticalAlignmentMiddle;
  13. }
  14. return self;
  15. }
  16. - (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment {
  17. verticalAlignment = verticalAlignment;
  18. [self setNeedsDisplay];
  19. }
  20. - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
  21. CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
  22. switch (self.verticalAlignment) {
  23. case VerticalAlignmentTop:
  24. textRect.origin.y = bounds.origin.y;
  25. break;
  26. case VerticalAlignmentBottom:
  27. textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;
  28. break;
  29. case VerticalAlignmentMiddle:
  30. // Fall through.
  31. default:
  32. textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0;
  33. }
  34. return textRect;
  35. }
  36. -(void)drawTextInRect:(CGRect)requestedRect {
  37. CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];
  38. [super drawTextInRect:actualRect];
  39. }
  40. @end