From f9c7b7734ed5b2fea84af0185c2e866f0877dba0 Mon Sep 17 00:00:00 2001 From: Jiwoo Date: Thu, 15 Nov 2018 21:24:58 +0900 Subject: [PATCH 1/2] Add URL connection with short touch --- src/NIAttributedLabel.h | 1 + src/NIAttributedLabel.m | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/NIAttributedLabel.h b/src/NIAttributedLabel.h index 962d5cc..f9c35db 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. diff --git a/src/NIAttributedLabel.m b/src/NIAttributedLabel.m index cc915a5..a80da3a 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]; } From 3d5d76a230cbcd9fd2a1ec7c6b4cfce2949b4e46 Mon Sep 17 00:00:00 2001 From: Jiwoo Date: Sat, 17 Nov 2018 17:17:52 +0900 Subject: [PATCH 2/2] Add Non-Alphabet linebreak with AutoLayout --- src/NIAttributedLabel.h | 1 + src/NIAttributedLabel.m | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/NIAttributedLabel.h b/src/NIAttributedLabel.h index f9c35db..64c6e0d 100644 --- a/src/NIAttributedLabel.h +++ b/src/NIAttributedLabel.h @@ -106,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 a80da3a..4d8d479 100644 --- a/src/NIAttributedLabel.m +++ b/src/NIAttributedLabel.m @@ -1526,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)