diff --git a/src/NIAttributedLabel.h b/src/NIAttributedLabel.h index 962d5cc..64c6e0d 100644 --- a/src/NIAttributedLabel.h +++ b/src/NIAttributedLabel.h @@ -70,6 +70,7 @@ extern NSString* const NIAttributedLabelLinkAttributeName; // Value is an NSText @property (nonatomic) BOOL autoDetectLinks; // Default: NO @property (nonatomic) NSTextCheckingType dataDetectorTypes; // Default: NSTextCheckingTypeLink @property (nonatomic) BOOL deferLinkDetection; // Default: NO +@property (nonatomic) BOOL linkShortTouch; // Default: NO - (void)addLink:(NSURL *)urlLink range:(NSRange)range; - (void)removeAllExplicitLinks; // Removes all links that were added by addLink:range:. Does not remove autodetected links. @@ -105,6 +106,7 @@ extern NSString* const NIAttributedLabelLinkAttributeName; // Value is an NSText - (void)insertImage:(UIImage *)image atIndex:(NSInteger)index margins:(UIEdgeInsets)margins verticalTextAlignment:(NIVerticalTextAlignment)verticalTextAlignment; - (void)invalidateAccessibleElements; +- (CGRect)textdataSize:(NSString *)string withContentWidth:(CGFloat)size fontsize:(CGFloat)font weight:(UIFontWeight)weight; @property (nonatomic, weak) IBOutlet id delegate; @end diff --git a/src/NIAttributedLabel.m b/src/NIAttributedLabel.m index cc915a5..4d8d479 100644 --- a/src/NIAttributedLabel.m +++ b/src/NIAttributedLabel.m @@ -873,15 +873,17 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { // If the user moves their finger within the link beyond a certain gutter amount, reset the // hold timer. The user must hold their finger still for the long press interval in order for // the long press action to fire. - if (fabs(self.touchPoint.x - point.x) >= kLongPressGutter - || fabs(self.touchPoint.y - point.y) >= kLongPressGutter) { - [self.longPressTimer invalidate]; - self.longPressTimer = nil; - if (nil != self.touchedLink) { - self.longPressTimer = [NSTimer scheduledTimerWithTimeInterval:kLongPressTimeInterval target:self selector:@selector(_longPressTimerDidFire:) userInfo:nil repeats:NO]; - self.touchPoint = point; + if ((self.linkShortTouch && self.touchPoint.x - point.x >= 0 ) || + (self.linkShortTouch && self.touchPoint.y - point.y >= 0 )){ + if (nil != self.touchedLink && nil != self.originalLink) { + [[UIApplication sharedApplication] openURL:self.originalLink.URL options:@{} completionHandler:nil]; + } + }else if (fabs(self.touchPoint.x - point.x) >= kLongPressGutter + || fabs(self.touchPoint.y - point.y) >= kLongPressGutter) { + if (nil != self.touchedLink) { + self.touchPoint = point; + } } - } } else { [super touchesMoved:touches withEvent:event]; } @@ -1524,6 +1526,37 @@ - (void)insertImage:(UIImage *)image atIndex:(NSInteger)index margins:(UIEdgeIns [self.images addObject:labelImage]; } +- (CGRect)textdataSize:(NSString *)string withContentWidth:(CGFloat)size fontsize:(CGFloat)font weight:(UIFontWeight)weight { + CGSize maximumTextViewSize = CGSizeMake(size, CGFLOAT_MAX); + NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading; + UIFont *uifont = [UIFont fontWithName:@"AppleSDGothicNeo-Regular" size:font]; + + // Get text + CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0); + CFAttributedStringReplaceString (attrString, CFRangeMake(0, 0), (CFStringRef) string ); + CFIndex stringLength = CFStringGetLength((CFStringRef) attrString); + + // Change font + CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef) uifont.fontName, uifont.pointSize, NULL); + CFAttributedStringSetAttribute(attrString, CFRangeMake(0, stringLength), kCTFontAttributeName, ctFont); + + // Calc the size + CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString); + CFRange fitRange; + CGSize frameSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, 0), NULL, maximumTextViewSize, &fitRange); + + CFRelease(ctFont); + CFRelease(framesetter); + CFRelease(attrString); + + CGRect textViewBounds = [string boundingRectWithSize:maximumTextViewSize + options:options + attributes:@{NSFontAttributeName:uifont} + context:nil]; + textViewBounds.size = frameSize; + return textViewBounds; +} + @end @implementation NIAttributedLabel (ConversionUtilities)