StrikeThroughLabel.m 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //
  2. // StrikeThroughLabel.m
  3. // StrikeThroughLabelExample
  4. //
  5. // Created by Scott Hodgin on 12/14/10.
  6. // Copyright (c) 2010 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "StrikeThroughLabel.h"
  9. @implementation StrikeThroughLabel
  10. @synthesize strikeThroughEnabled = _strikeThroughEnabled;
  11. - (void)drawTextInRect:(CGRect)rect{
  12. [super drawTextInRect:rect];
  13. CGSize textSize = NewStringSize(self.text, self.font);
  14. CGFloat strikeWidth = textSize.width;
  15. CGRect lineRect;
  16. if ([self textAlignment] == NSTextAlignmentRight) {
  17. lineRect = CGRectMake(rect.size.width - strikeWidth, rect.size.height/2, strikeWidth, 1);
  18. } else if ([self textAlignment] == NSTextAlignmentCenter) {
  19. lineRect = CGRectMake(rect.size.width/2 - strikeWidth/2, rect.size.height/2, strikeWidth, 1);
  20. } else {
  21. lineRect = CGRectMake(0, rect.size.height/2, strikeWidth, 1);
  22. }
  23. // if (_strikeThroughEnabled) {
  24. CGContextRef context = UIGraphicsGetCurrentContext();
  25. // Get the fonts color.
  26. CGContextSetFillColorWithColor(context,self.textColor.CGColor);
  27. CGContextFillRect(context, lineRect);
  28. // }
  29. [super drawTextInRect:rect];
  30. }
  31. - (void)setStrikeThroughEnabled:(BOOL)strikeThroughEnabled {
  32. _strikeThroughEnabled = strikeThroughEnabled;
  33. NSString *tempText = [self.text copy];
  34. self.text = @"";
  35. self.text = tempText;
  36. [tempText release];
  37. }
  38. @end