diff --git a/EGOTextView/EGOTextView.h b/EGOTextView/EGOTextView.h index 67974aa..b933402 100644 --- a/EGOTextView/EGOTextView.h +++ b/EGOTextView/EGOTextView.h @@ -47,6 +47,8 @@ extern NSString * const EGOTextAttachmentPlaceholderString; - (void)egoTextView:(EGOTextView*)textView didSelectURL:(NSURL*)URL; +- (void)egoTextViewTouched:(EGOTextView*)textView; + @end @protocol EGOTextAttachmentCell @@ -79,6 +81,7 @@ extern NSString * const EGOTextAttachmentPlaceholderString; BOOL _delegateRespondsToDidChange; BOOL _delegateRespondsToDidChangeSelection; BOOL _delegateRespondsToDidSelectURL; + BOOL _delegateRespondsToTouched; NSAttributedString *_attributedString; UIFont *_font; @@ -121,5 +124,6 @@ extern NSString * const EGOTextAttachmentPlaceholderString; @property(nonatomic) NSRange markedRange; - (BOOL)hasText; +- (void)tap:(UITapGestureRecognizer*)gesture; @end \ No newline at end of file diff --git a/EGOTextView/EGOTextView.m b/EGOTextView/EGOTextView.m index cb5b4ef..861c473 100644 --- a/EGOTextView/EGOTextView.m +++ b/EGOTextView/EGOTextView.m @@ -413,6 +413,7 @@ - (void)setDelegate:(id)aDelegate { _delegateRespondsToDidChange = [delegate respondsToSelector:@selector(egoTextViewDidChange:)]; _delegateRespondsToDidChangeSelection = [delegate respondsToSelector:@selector(egoTextViewDidChangeSelection:)]; _delegateRespondsToDidSelectURL = [delegate respondsToSelector:@selector(egoTextView:didSelectURL:)]; + _delegateRespondsToTouched = [delegate respondsToSelector:@selector(egoTextViewTouched:)]; } @@ -969,6 +970,9 @@ - (void)selectionChanged { [_textContentView setNeedsDisplay]; } + if(_delegateRespondsToDidChangeSelection) { + [self.delegate egoTextViewDidChangeSelection:self]; + } } - (NSRange)markedRange { @@ -1419,10 +1423,11 @@ - (void)insertText:(NSString *)text { [newString release]; - self.attributedString = _mutableAttributedString; self.markedRange = markedTextRange; - self.selectedRange = selectedNSRange; - + _selectedRange = selectedNSRange; + self.attributedString = _mutableAttributedString; + [self selectionChanged]; + if (text.length > 1 || ([text isEqualToString:@" "] || [text isEqualToString:@"\n"])) { [self checkSpellingForRange:[self characterRangeAtIndex:self.selectedRange.location-1]]; [self checkLinksForRange:NSMakeRange(0, self.attributedString.length)]; @@ -1484,9 +1489,14 @@ - (void)deleteBackward { } - self.attributedString = _mutableAttributedString; self.markedRange = markedTextRange; - self.selectedRange = selectedNSRange; + if(selectedNSRange.location > _mutableAttributedString.length) { + selectedNSRange.location = _mutableAttributedString.length; + selectedNSRange.length = 0; + } + _selectedRange = selectedNSRange; + self.attributedString = _mutableAttributedString; + [self selectionChanged]; } @@ -1692,6 +1702,10 @@ - (EGOTextWindow*)egoTextWindow { } - (void)longPress:(UILongPressGestureRecognizer*)gesture { + if(_delegateRespondsToTouched) { + [self.delegate egoTextViewTouched:self]; + } + if (gesture.state==UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) { @@ -1764,7 +1778,7 @@ - (void)longPress:(UILongPressGestureRecognizer*)gesture { } else { CGPoint location = [gesture locationInView:_textWindow]; - CGRect rect = CGRectMake(location.x, location.y, _caretView.bounds.size.width, _caretView.bounds.size.height); + CGRect rect = CGRectMake(location.x + self.frame.origin.x, location.y - self.frame.origin.y, _caretView.bounds.size.width, _caretView.bounds.size.height); self.selectedRange = NSMakeRange(index, 0); @@ -1801,6 +1815,10 @@ - (void)longPress:(UILongPressGestureRecognizer*)gesture { } - (void)doubleTap:(UITapGestureRecognizer*)gesture { + if(_delegateRespondsToTouched) { + [self.delegate egoTextViewTouched:self]; + } + [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(showMenu) object:nil]; [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(showCorrectionMenu) object:nil]; @@ -1821,7 +1839,10 @@ - (void)doubleTap:(UITapGestureRecognizer*)gesture { } - (void)tap:(UITapGestureRecognizer*)gesture { - + if(_delegateRespondsToTouched) { + [self.delegate egoTextViewTouched:self]; + } + if (_editable && ![self isFirstResponder]) { [self becomeFirstResponder]; return; @@ -1835,7 +1856,12 @@ - (void)tap:(UITapGestureRecognizer*)gesture { self.selectedRange = NSMakeRange(_selectedRange.location, 0); } - NSInteger index = [self closestWhiteSpaceIndexToPoint:[gesture locationInView:self]]; + NSInteger index; + if(gesture) { + index = [self closestWhiteSpaceIndexToPoint:[gesture locationInView:self]]; + } else { + index = 0; + } if (_delegateRespondsToDidSelectURL && !_editing) { if ([self selectedLinkAtIndex:index]) { @@ -1850,13 +1876,13 @@ - (void)tap:(UITapGestureRecognizer*)gesture { } else { - if (index==self.selectedRange.location) { - [self performSelector:@selector(showMenu) withObject:nil afterDelay:0.35f]; - } else { - if (_editing) { - [self performSelector:@selector(showCorrectionMenu) withObject:nil afterDelay:0.35f]; - } - } +// if (index==self.selectedRange.location) { +// [self performSelector:@selector(showMenu) withObject:nil afterDelay:0.35f]; +// } else { +// if (_editing) { +// [self performSelector:@selector(showCorrectionMenu) withObject:nil afterDelay:0.35f]; +// } +// } } @@ -1869,6 +1895,13 @@ - (void)tap:(UITapGestureRecognizer*)gesture { } +-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { + if(_delegateRespondsToTouched) { + [self.delegate egoTextViewTouched:self]; + } + [super touchesBegan:touches withEvent:event]; +} + ///////////////////////////////////////////////////////////////////////////// // MARK: - @@ -2507,8 +2540,8 @@ - (void)showFromView:(UIView*)view rect:(CGRect)rect { } CGRect frame = _view.frame; - frame.origin.x = floorf(pos.x - (_view.bounds.size.width/2)); - frame.origin.y = floorf(pos.y - _view.bounds.size.height); + frame.origin.x = floorf(pos.x - (_view.bounds.size.width/2) - view.superview.frame.origin.x); + frame.origin.y = floorf(pos.y - _view.bounds.size.height + view.superview.frame.origin.y); if (_type==EGOWindowMagnify) { @@ -2643,8 +2676,8 @@ - (void)renderWithContentView:(UIView*)view fromRect:(CGRect)rect { if (_showing && _view!=nil) { CGRect frame = _view.frame; - frame.origin.x = floorf((pos.x - (_view.bounds.size.width/2)) + (rect.size.width/2)); - frame.origin.y = floorf(pos.y - _view.bounds.size.height); + frame.origin.x = floorf((pos.x - (_view.bounds.size.width/2)) + (rect.size.width/2) - view.superview.frame.origin.x); + frame.origin.y = floorf(pos.y - _view.bounds.size.height + view.superview.frame.origin.y); if (_type==EGOWindowMagnify) { frame.origin.y = MAX(0.0f, frame.origin.y); diff --git a/EGOTextView_Demo/EGOTextView_DemoViewController.m b/EGOTextView_Demo/EGOTextView_DemoViewController.m index 584ad91..7b37880 100644 --- a/EGOTextView_Demo/EGOTextView_DemoViewController.m +++ b/EGOTextView_Demo/EGOTextView_DemoViewController.m @@ -103,6 +103,14 @@ - (void)egoTextViewDidChange:(EGOTextView *)textView { } +- (void)egoTextViewDidChangeSelection:(EGOTextView*)textView { + NSLog(@"%s", __PRETTY_FUNCTION__); +} + +- (void)egoTextViewTouched:(EGOTextView*)textView { + NSLog(@"%s", __PRETTY_FUNCTION__); +} + - (void)egoTextView:(EGOTextView*)textView didSelectURL:(NSURL *)URL { }