Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add URL connection with short touch #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/NIAttributedLabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<NIAttributedLabelDelegate> delegate;
@end
Expand Down
49 changes: 41 additions & 8 deletions src/NIAttributedLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down Expand Up @@ -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)
Expand Down