123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- //
- // HBCoreLabel
- // CoreTextMagazine
- //
- // Created by weqia on 13-10-27.
- // Copyright (c) 2013年 Marin Todorov. All rights reserved.
- //
- #import "HBCoreLabel.h"
- #import "NSStrUtil.h"
- #import "JXActionSheetVC.h"
- @interface HBCoreLabel () <JXActionSheetVCDelegate>
- @property (nonatomic, strong) JXActionSheetVC *actionVC;
- @end
- @implementation HBCoreLabel
- @synthesize match=_match,linesLimit;
- #pragma -mark 接口方法
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- // Initialization code
- }
- return self;
- }
-
- -(id)initWithCoder:(NSCoder *)aDecoder
- {
- self=[super initWithCoder:aDecoder];
- if(self){
- _copyEnableAlready=NO;
- }
- return self;
- }
- -(void)setAttributedText:(NSString *)attributedText
- {
- _attributed=YES;
- if([NSStrUtil isEmptyOrNull:attributedText ])
- {
- self.match=nil;
- return;
- }
- MatchParser * parser=[[MatchParser alloc]init];
- parser.width=self.bounds.size.width;
- [parser match:attributedText];
- self.match=parser;
- }
- -(void)setText:(NSString *)text
- {
- _attributed=NO;
- [super setText:text];
- }
- -(void)setMatch:(MatchParser *)match
- {
- if(match==_match)
- return;
- _attributed=YES;
- _match=match;
- [self setNeedsDisplay];
- }
- -(void)registerCopyAction
- {
- if(_copyEnableAlready)
- return;
- _copyEnableAlready=YES;
- self.userInteractionEnabled=YES;
- NSArray * gestures=self.gestureRecognizers;
- for(UIGestureRecognizer * gesture in gestures){
- if([gestures isKindOfClass:[UILongPressGestureRecognizer class]]){
- UILongPressGestureRecognizer * longPress=(UILongPressGestureRecognizer*)gestures;
- [longPress addTarget:self action:@selector(longPressAction:)];
- return;
- }
- }
- UILongPressGestureRecognizer * longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
- [longPress setMinimumPressDuration:0.8];
- [self addGestureRecognizer:longPress];
- }
- - (void)drawRect:(CGRect)rect
- {
- // Drawing code
- if(!_attributed){
- [super drawRect:rect];
- return;
- }
- if(self.match!=nil&&[self.match isKindOfClass:[MatchParser class]]){
- CGContextRef context = UIGraphicsGetCurrentContext();
- // Flip the coordinate system
- CGContextSetTextMatrix(context, CGAffineTransformIdentity);
- CGContextSaveGState(context);
- CGContextTranslateCTM(context, 0, 10000);
- CGContextScaleCTM(context, 1.0, -1.0);
- if(self.match.numberOfLimitLines==0||(self.match.numberOfLimitLines>=self.match.numberOfTotalLines)||!self.linesLimit){
- CTFrameDraw((__bridge CTFrameRef)(self.match.ctFrame), context);
- for (NSDictionary* imageData in self.match.images) {
- NSString* img = [imageData objectForKey:MatchParserImage];
- UIImage * image=[UIImage imageNamed:img];
- NSValue * value=[imageData objectForKey:MatchParserRects];
- CGRect imgBounds;
- // if(![value isKindOfClass:[NSNull class]]){
- imgBounds=[[imageData objectForKey:MatchParserRects] CGRectValue];
- // }
- CGContextDrawImage(context, imgBounds, image.CGImage);
-
- }
- }
- else{
- NSArray *lines = (__bridge NSArray *)CTFrameGetLines((__bridge CTFrameRef)(self.match.ctFrame));
- CGPoint origins[[lines count]];
- CTFrameGetLineOrigins((__bridge CTFrameRef)(self.match.ctFrame), CFRangeMake(0, 0), origins); //2
- for(int lineIndex=0;lineIndex<self.match.numberOfLimitLines;lineIndex++){
- CTLineRef line=(__bridge CTLineRef)(lines[lineIndex]);
- CGContextSetTextPosition(context,origins[lineIndex].x,origins[lineIndex].y);
- // NSLog(@"%d: %f,%f",lineIndex,origins[lineIndex].x,origins[lineIndex].y);
- CTLineDraw(line, context);
- }
- for (NSDictionary* imageData in self.match.images) {
- NSString* img = [imageData objectForKey:MatchParserImage];
- UIImage * image=[UIImage imageNamed:img];
- NSValue * value=[imageData objectForKey:MatchParserRects];
- CGRect imgBounds;
- if(![value isKindOfClass:[NSNull class]])
- {
- imgBounds=[[imageData objectForKey:MatchParserRects] CGRectValue];
- NSNumber * number=[imageData objectForKey:MatchParserLine];
- int line=[number intValue];
- if(line<self.match.numberOfLimitLines){
- CGContextDrawImage(context, imgBounds, image.CGImage);
- }
- }
- }
- }
- }
- }
- #pragma -mark 事件响应方法
- -(void)longPressAction:(UIGestureRecognizer*)gesture
- {
- if (gesture.view!=self) {
- return;
- }
- if(gesture.state==UIGestureRecognizerStateBegan){
- self.backgroundColor=[UIColor lightGrayColor];
- self.actionVC = [[JXActionSheetVC alloc] initWithImages:@[] names:@[Localized(@"JX_Copy")]];
- self.actionVC.delegate = self;
- [g_App.window addSubview:self.actionVC.view];
- }else if(gesture.state==UIGestureRecognizerStateEnded){
- self.backgroundColor=[UIColor clearColor];
- }
- }
- -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- {
- UITouch * touch1=[touches anyObject];
- CGPoint point=[touch1 locationInView:self];
- for(NSDictionary * dic in self.match.links){
- NSArray * rects=[dic objectForKey:MatchParserRects];
- for(NSValue * value in rects){
- CGRect rect= [value CGRectValue];
- if(point.x>rect.origin.x&&point.y>rect.origin.y&&point.x<(rect.origin.x+rect.size.width)&&point.y<(rect.origin.y+rect.size.height)){
- NSValue * rangeValue=[dic objectForKey:MatchParserRange];
- NSRange range1=[rangeValue rangeValue];
- id<MatchParserDelegate> data=self.match.data;
- _data=data;
- [data updateMatch:^(NSMutableAttributedString *string, NSRange range) {
- CTFontRef fontRef=CTFontCreateWithName((__bridge CFStringRef)(self.font.fontName),self.font.pointSize,NULL);
- if(range.location==range1.location){
- NSDictionary *attribute=[NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)fontRef,kCTFontAttributeName,(id)self.match.keyWorkColor.CGColor,kCTForegroundColorAttributeName,[NSNumber numberWithFloat:1],kCTStrokeWidthAttributeName,nil];
- [string addAttributes:attribute range:range];
- }else{
- NSDictionary *attribute=[NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)fontRef,kCTFontAttributeName,(id)self.match.keyWorkColor.CGColor,kCTForegroundColorAttributeName,nil];
- [string addAttributes:attribute range:range];
- }
- CFRelease(fontRef);
- }];
- NSArray * ges = [self.superview gestureRecognizers];
- for(UIGestureRecognizer * recognizer in ges){
- recognizer.enabled = NO;
- }
- ges=[self gestureRecognizers];
- for(UIGestureRecognizer * recognizer in ges){
- recognizer.enabled = NO;
- }
- _linkStr=[self.match.attrString.string substringWithRange:range1];
- _linkType=[dic objectForKey:MatchParserLinkType];
- [self setNeedsDisplay];
- touch=YES;
- return;
- }
- }
- }
- [super touchesBegan:touches withEvent:event];
- }
- -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
- {
- if (touch) {
- touch=NO;
- if(_data){
- [_data updateMatch:^(NSMutableAttributedString *string, NSRange range) {
- CTFontRef fontRef=CTFontCreateWithName((__bridge CFStringRef)(self.font.fontName),self.font.pointSize,NULL);
- NSDictionary *attribute=[NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)fontRef,kCTFontAttributeName,(id)self.match.keyWorkColor.CGColor,kCTForegroundColorAttributeName,nil];
- [string addAttributes:attribute range:range];
- }];
- NSArray * ges=[self.superview gestureRecognizers];
- for(UIGestureRecognizer * recognizer in ges){
- recognizer.enabled=YES;
- }
- ges=[self gestureRecognizers];
- for(UIGestureRecognizer * recognizer in ges){
- recognizer.enabled=YES;
- }
- [self setNeedsDisplay];
- if([_linkType isEqualToString:MatchParserLinkTypeUrl]){
- if(self.delegate&&[self.delegate respondsToSelector:@selector(coreLabel:linkClick:)]){
- [self.delegate coreLabel:self linkClick:_linkStr];
- }
- }else if ([_linkType isEqualToString:MatchParserLinkTypePhone]){
- if(self.delegate&&[self.delegate respondsToSelector:@selector(coreLabel:phoneClick:)]){
- [self.delegate coreLabel:self phoneClick:_linkStr];
- }
- }else if ([_linkType isEqualToString:MatchParserLinkTypeMobie]){
- if(self.delegate&&[self.delegate respondsToSelector:@selector(coreLabel:mobieClick:)]){
- [self.delegate coreLabel:self mobieClick:_linkStr];
- }
- }
- return;
- }
- }
- [super touchesCancelled:touches withEvent:event];
- }
- -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- {
- if(touch){
- touch=NO;
- if(_data){
- [_data updateMatch:^(NSMutableAttributedString *string, NSRange range) {
- CTFontRef fontRef=CTFontCreateWithName((__bridge CFStringRef)(self.font.fontName),self.font.pointSize,NULL);
- NSDictionary *attribute=[NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)fontRef,kCTFontAttributeName,(id)self.match.keyWorkColor.CGColor,kCTForegroundColorAttributeName,nil];
- [string addAttributes:attribute range:range];
- CFRelease(fontRef);
- }];
- NSArray * ges=[self.superview gestureRecognizers];
- for(UIGestureRecognizer * recognizer in ges){
- recognizer.enabled=YES;
- }
- ges=[self gestureRecognizers];
- for(UIGestureRecognizer * recognizer in ges){
- recognizer.enabled=YES;
- }
- [self setNeedsDisplay];
- if([_linkType isEqualToString:MatchParserLinkTypeUrl]){
- if(self.delegate&&[self.delegate respondsToSelector:@selector(coreLabel:linkClick:)]){
- [self.delegate coreLabel:self linkClick:_linkStr];
- }
- }else if ([_linkType isEqualToString:MatchParserLinkTypePhone]){
- if(self.delegate&&[self.delegate respondsToSelector:@selector(coreLabel:phoneClick:)]){
- [self.delegate coreLabel:self phoneClick:_linkStr];
- }
- }else if ([_linkType isEqualToString:MatchParserLinkTypeMobie]){
- if(self.delegate&&[self.delegate respondsToSelector:@selector(coreLabel:phoneClick:)]){
- [self.delegate coreLabel:self phoneClick:_linkStr];
- }
- }
- return;
- }
- }
- [super touchesEnded:touches withEvent:event];
- }
- #pragma -mark 回调方法
- - (void)actionSheet:(JXActionSheetVC *)actionSheet didButtonWithIndex:(NSInteger)index {
- self.backgroundColor=[UIColor clearColor];
- if(index==0){
- if([self.match.source isKindOfClass:[NSString class]]){
- [UIPasteboard generalPasteboard].string=self.match.source;
- }
- }
- }
- @end
|