JXLabel.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // JXLabel.m
  3. // sjvodios
  4. //
  5. // Created by on 12-2-1.
  6. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "JXLabel.h"
  9. @interface JXLabel ()
  10. @property (nonatomic, assign) BOOL isAction; // 防止重复点击
  11. @end
  12. @implementation JXLabel
  13. @synthesize delegate;
  14. @synthesize didTouch;
  15. @synthesize changeAlpha,line;
  16. - (id)initWithFrame:(CGRect)frame
  17. {
  18. self = [super initWithFrame:frame];
  19. if (self) {
  20. // Initialization code
  21. changeAlpha = NO;
  22. line = 0;
  23. _isAction = NO;
  24. self.userInteractionEnabled=YES;
  25. }
  26. return self;
  27. }
  28. -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
  29. //[super touchesBegan: touches withEvent: event];
  30. if(changeAlpha)
  31. self.alpha = 0.5;
  32. }
  33. -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
  34. // NSLog(@"touchesMoved");
  35. [super touchesMoved: touches withEvent: event];
  36. }
  37. -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
  38. //[super touchesEnded: touches withEvent: event];
  39. if(changeAlpha)
  40. self.alpha = 1;
  41. if (self.isAction) {
  42. return;
  43. }
  44. self.isAction = YES;
  45. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  46. self.isAction = NO;
  47. });
  48. BOOL inside = YES;
  49. for(int i=0;i<[touches count];i++){
  50. CGPoint p = [[[touches allObjects] objectAtIndex:i] locationInView:self];
  51. // NSLog(@"%d=%f,%f",i,p.x,p.y);
  52. if(p.x<0 || p.y <0){
  53. inside = NO;
  54. break;
  55. }
  56. if(p.x>self.frame.size.width || p.y>self.frame.size.height){
  57. inside = NO;
  58. break;
  59. }
  60. }
  61. if(!inside)
  62. return;
  63. if(self.delegate != nil && [self.delegate respondsToSelector:self.didTouch])
  64. [self.delegate performSelectorOnMainThread:self.didTouch withObject:self waitUntilDone:NO];
  65. }
  66. -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
  67. //[super touchesCancelled: touches withEvent: event];
  68. if(changeAlpha)
  69. self.alpha = 1;
  70. }
  71. - (void)dealloc
  72. {
  73. delegate = nil;
  74. didTouch = nil;
  75. //[_delegate release];
  76. // [super dealloc];
  77. }
  78. -(void)drawRect:(CGRect)rect
  79. {
  80. [super drawRect:rect];
  81. if(line>0){
  82. CGContextRef ctx = UIGraphicsGetCurrentContext();
  83. // CGSize fontSize =[self.text sizeWithFont:self.font
  84. // forWidth:self.bounds.size.width
  85. // lineBreakMode:UILineBreakModeTailTruncation];
  86. CGSize fontSize = [self.text boundingRectWithSize:CGSizeMake(self.bounds.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.font} context:nil].size;
  87. // Get the fonts color.
  88. const CGFloat * colors = CGColorGetComponents(self.textColor.CGColor);
  89. // Sets the color to draw the line
  90. CGContextSetRGBStrokeColor(ctx, colors[0], colors[1], colors[2], line); // Format : RGBA
  91. [self.textColor set];
  92. // Line Width : make thinner or bigger if you want
  93. CGContextSetLineWidth(ctx, line);
  94. // Calculate the starting point (left) and target (right)
  95. CGPoint l,r;
  96. if (self.textAlignment == NSTextAlignmentLeft) {
  97. l = CGPointMake(0, self.frame.size.height/2.0 +fontSize.height/2.0);
  98. r = CGPointMake(fontSize.width/2.0, self.frame.size.height/2.0 + fontSize.height/2.0);
  99. }else if (self.textAlignment == NSTextAlignmentRight) {
  100. l = CGPointMake(self.frame.size.width - fontSize.width,
  101. self.frame.size.height/2.0 +fontSize.height/2.0);
  102. r = CGPointMake(self.frame.size.width,
  103. self.frame.size.height/2.0 + fontSize.height/2.0);
  104. }else if (self.textAlignment == NSTextAlignmentCenter) {
  105. l = CGPointMake(self.frame.size.width/2.0 - fontSize.width/2.0,
  106. self.frame.size.height/2.0 + fontSize.height/2.0);
  107. r = CGPointMake(self.frame.size.width/2.0 + fontSize.width/2.0,
  108. self.frame.size.height/2.0 + fontSize.height/2.0);
  109. }
  110. // Add Move Command to point the draw cursor to the starting point
  111. CGContextMoveToPoint(ctx, l.x, l.y);
  112. // Add Command to draw a Line
  113. CGContextAddLineToPoint(ctx, r.x, r.y);
  114. // Actually draw the line.
  115. CGContextStrokePath(ctx);
  116. // should be nothing, but who knows...
  117. // [super drawRect:rect];
  118. }
  119. }
  120. -(void)setDidTouch:(SEL)value{
  121. if(value){
  122. didTouch = value;
  123. changeAlpha = YES;
  124. self.userInteractionEnabled = YES;
  125. }
  126. }
  127. @end