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

Added long press event & fixed memory leak #177

Open
wants to merge 4 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDESourceControlProjectFavoriteDictionaryKey</key>
<false/>
<key>IDESourceControlProjectIdentifier</key>
<string>8E00B687-E064-4809-8B28-8796809624B8</string>
<key>IDESourceControlProjectName</key>
<string>AttributedLabel Example</string>
<key>IDESourceControlProjectOriginsDictionary</key>
<dict>
<key>C1454787-2CDA-4B38-A9B3-B2369AB9C48B</key>
<string>https://github.com/JonasGessner/OHAttributedLabel.git</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>AttributedLabel Example/AttributedLabel Example.xcodeproj/project.xcworkspace</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
<key>C1454787-2CDA-4B38-A9B3-B2369AB9C48B</key>
<string>../../..</string>
</dict>
<key>IDESourceControlProjectURL</key>
<string>https://github.com/JonasGessner/OHAttributedLabel.git</string>
<key>IDESourceControlProjectVersion</key>
<integer>110</integer>
<key>IDESourceControlProjectWCCIdentifier</key>
<string>C1454787-2CDA-4B38-A9B3-B2369AB9C48B</string>
<key>IDESourceControlProjectWCConfigurations</key>
<array>
<dict>
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
<string>public.vcs.git</string>
<key>IDESourceControlWCCIdentifierKey</key>
<string>C1454787-2CDA-4B38-A9B3-B2369AB9C48B</string>
<key>IDESourceControlWCCName</key>
<string>OHAttributedLabel</string>
</dict>
</array>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDESourceControlProjectFavoriteDictionaryKey</key>
<false/>
<key>IDESourceControlProjectIdentifier</key>
<string>183D72E1-D028-4B77-8A12-AC9BBB43440E</string>
<key>IDESourceControlProjectName</key>
<string>AttributedLabel Example</string>
<key>IDESourceControlProjectOriginsDictionary</key>
<dict>
<key>C1454787-2CDA-4B38-A9B3-B2369AB9C48B</key>
<string>https://github.com/JonasGessner/OHAttributedLabel.git</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>AttributedLabel Example/AttributedLabel Example.xcworkspace</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
<key>C1454787-2CDA-4B38-A9B3-B2369AB9C48B</key>
<string>../..</string>
</dict>
<key>IDESourceControlProjectURL</key>
<string>https://github.com/JonasGessner/OHAttributedLabel.git</string>
<key>IDESourceControlProjectVersion</key>
<integer>110</integer>
<key>IDESourceControlProjectWCCIdentifier</key>
<string>C1454787-2CDA-4B38-A9B3-B2369AB9C48B</string>
<key>IDESourceControlProjectWCConfigurations</key>
<array>
<dict>
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
<string>public.vcs.git</string>
<key>IDESourceControlWCCIdentifierKey</key>
<string>C1454787-2CDA-4B38-A9B3-B2369AB9C48B</string>
<key>IDESourceControlWCCName</key>
<string>OHAttributedLabel</string>
</dict>
</array>
</dict>
</plist>
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ -(UIColor*)attributedLabel:(OHAttributedLabel*)attrLabel colorForLink:(NSTextChe
}
}

- (void)attributedLabel:(OHAttributedLabel *)attributedLabel longPressedLink:(NSTextCheckingResult *)linkInfo {
NSLog(@"Long Pressed Link %@", linkInfo);
}

-(BOOL)attributedLabel:(OHAttributedLabel *)attributedLabel shouldFollowLink:(NSTextCheckingResult *)linkInfo
{
[self.visitedLinks addObject:objectForLinkInfo(linkInfo)];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ -(IBAction)changeLineSpacing:(UISlider*)slider
#pragma mark - OHAttributedString Delegate Method
/////////////////////////////////////////////////////////////////////////////

- (void)attributedLabel:(OHAttributedLabel *)attributedLabel longPressedLink:(NSTextCheckingResult *)linkInfo {
NSLog(@"Long Pressed Link %@", linkInfo);
}

-(BOOL)attributedLabel:(OHAttributedLabel *)attributedLabel shouldFollowLink:(NSTextCheckingResult *)linkInfo
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *
#pragma mark - OHAttributedLabel Delegate Method
/////////////////////////////////////////////////////////////////////////////

- (void)attributedLabel:(OHAttributedLabel *)attributedLabel longPressedLink:(NSTextCheckingResult *)linkInfo {
NSLog(@"Long Pressed Link %@", linkInfo);
}

-(BOOL)attributedLabel:(OHAttributedLabel *)attributedLabel shouldFollowLink:(NSTextCheckingResult *)linkInfo
{
if ([[UIApplication sharedApplication] canOpenURL:linkInfo.extendedURL])
Expand Down
47 changes: 27 additions & 20 deletions OHAttributedLabel/Source/NSAttributedString+Attributes.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ -(CGSize)sizeConstrainedToSize:(CGSize)maxSize fitRange:(NSRange*)fitRange
sz = CTFramesetterSuggestFrameSizeWithConstraints(framesetter,CFRangeMake(0,0),NULL,maxSize,&fitCFRange);
sz = CGSizeMake( floorf(sz.width+1) , floorf(sz.height+1) ); // take 1pt of margin for security
CFRelease(framesetter);

if (fitRange)
{
*fitRange = NSMakeRange((NSUInteger)fitCFRange.location, (NSUInteger)fitCFRange.length);
Expand Down Expand Up @@ -194,7 +194,7 @@ -(void)setFontFamily:(NSString*)fontFamily size:(CGFloat)size bold:(BOOL)isBold
CTFontRef aFont = CTFontCreateWithFontDescriptor(desc, size, NULL);
CFRelease(desc);
if (!aFont) return;

[self removeAttribute:(__bridge NSString*)kCTFontAttributeName range:range]; // Work around for Apple leak
[self addAttribute:(__bridge NSString*)kCTFontAttributeName value:(__bridge id)aFont range:range];
CFRelease(aFont);
Expand Down Expand Up @@ -236,11 +236,15 @@ -(void)changeFontWithTraits:(CTFontSymbolicTraits)traits
[self beginEditing];
do {
// Get font at startPoint
BOOL hadCurrentFont = YES;

CTFontRef currentFont = (__bridge CTFontRef)[self attribute:(__bridge NSString*)kCTFontAttributeName atIndex:startPoint effectiveRange:&effectiveRange];
if (!currentFont)
{

if (!currentFont) {
hadCurrentFont = NO;
currentFont = CTFontCreateUIFontForLanguage(kCTFontLabelFontType, 0.0, NULL);
}

// The range for which this font is effective
NSRange fontRange = NSIntersectionRange(range, effectiveRange);
// Create the font variant for this font according to new traits
Expand Down Expand Up @@ -272,9 +276,12 @@ -(void)changeFontWithTraits:(CTFontSymbolicTraits)traits
if (fontNameRef) CFRelease(fontNameRef);
}

if (!hadCurrentFont) {
CFRelease(currentFont);
}

// Apply the new font with new traits
if (newFont)
{
if (newFont) {
[self removeAttribute:(__bridge NSString*)kCTFontAttributeName range:fontRange]; // Work around for Apple leak
[self addAttribute:(__bridge NSString*)kCTFontAttributeName value:(__bridge id)newFont range:fontRange];
CFRelease(newFont);
Expand All @@ -296,18 +303,18 @@ -(void)setTextBold:(BOOL)isBold range:(NSRange)range
[self changeFontWithTraits:(isBold?kCTFontTraitBold:0)
mask:kCTFontTraitBold
range:range newFontFinder:^NSString *(NSString *currentFontName)
{
if ([currentFontName isEqualToString:kHelveticaNeueUI_Italic] || [currentFontName isEqualToString:kHelveticaNeueUI_Bold_Italic])
{
// Italic private font
return isBold ? kHelveticaNeueUI_Bold_Italic : kHelveticaNeueUI_Italic;
} else if ([currentFontName isEqualToString:kHelveticaNeueUI] || [currentFontName isEqualToString:kHelveticaNeueUI_Bold]) {
// Non-Italic private font
return isBold ? kHelveticaNeueUI_Bold : kHelveticaNeueUI;
} else {
return nil;
}
}];
{
if ([currentFontName isEqualToString:kHelveticaNeueUI_Italic] || [currentFontName isEqualToString:kHelveticaNeueUI_Bold_Italic])
{
// Italic private font
return isBold ? kHelveticaNeueUI_Bold_Italic : kHelveticaNeueUI_Italic;
} else if ([currentFontName isEqualToString:kHelveticaNeueUI] || [currentFontName isEqualToString:kHelveticaNeueUI_Bold]) {
// Non-Italic private font
return isBold ? kHelveticaNeueUI_Bold : kHelveticaNeueUI;
} else {
return nil;
}
}];
}

-(void)setTextItalics:(BOOL)isItalics range:(NSRange)range
Expand Down Expand Up @@ -366,9 +373,9 @@ -(void)modifyParagraphStylesInRange:(NSRange)range withBlock:(void(^)(OHParagrap
while (NSLocationInRange(loc, range))
{
CTParagraphStyleRef currentCTStyle = (__bridge CTParagraphStyleRef)[self attribute:(__bridge NSString*)kCTParagraphStyleAttributeName
atIndex:loc longestEffectiveRange:rangePtr inRange:range];
atIndex:loc longestEffectiveRange:rangePtr inRange:range];
__block OHParagraphStyle* paraStyle = [OHParagraphStyle paragraphStyleWithCTParagraphStyle:currentCTStyle];
block(paraStyle);
block(paraStyle);
[self setParagraphStyle:paraStyle range:*rangePtr];

loc = NSMaxRange(*rangePtr);
Expand Down
3 changes: 3 additions & 0 deletions OHAttributedLabel/Source/OHAttributedLabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
@protocol OHAttributedLabelDelegate <NSObject>
@optional
-(BOOL)attributedLabel:(OHAttributedLabel*)attributedLabel shouldFollowLink:(NSTextCheckingResult*)linkInfo;

-(void)attributedLabel:(OHAttributedLabel*)attributedLabel longPressedLink:(NSTextCheckingResult*)linkInfo;

//! @parameter underlineStyle Combination of CTUnderlineStyle and CTUnderlineStyleModifiers
-(UIColor*)attributedLabel:(OHAttributedLabel*)attributedLabel colorForLink:(NSTextCheckingResult*)linkInfo underlineStyle:(int32_t*)underlineStyle;
@end
Expand Down
32 changes: 31 additions & 1 deletion OHAttributedLabel/Source/OHAttributedLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ @interface OHAttributedLabel(/* Private */) <UIGestureRecognizerDelegate>
NSMutableArray* _customLinks;
CGPoint _touchStartPoint;
UIGestureRecognizer *_gestureRecogniser;
UIGestureRecognizer *_longTapGestureReconizer;
}
@property(nonatomic, retain) NSTextCheckingResult* activeLink;
-(NSTextCheckingResult*)linkAtCharacterIndex:(CFIndex)idx;
Expand Down Expand Up @@ -146,6 +147,10 @@ - (void)commonInit
_gestureRecogniser = [[OHTouchesGestureRecognizer alloc] initWithTarget:self action:@selector(_gestureRecognised:)];
_gestureRecogniser.delegate = self;
[self addGestureRecognizer:_gestureRecogniser];

_longTapGestureReconizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
_longTapGestureReconizer.delegate = self;
[self addGestureRecognizer:_longTapGestureReconizer];
}

- (id) initWithFrame:(CGRect)aFrame
Expand Down Expand Up @@ -442,6 +447,23 @@ -(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
return hitResult;
}

- (void)longPress:(UILongPressGestureRecognizer *)gesture {
CGPoint pt = [gesture locationInView:self];

if (gesture.state == UIGestureRecognizerStateBegan) {
// Check that the link on touchEnd is the same as the link on touchBegan
NSTextCheckingResult* linkAtTouchesEnded = [self linkAtPoint:pt];
BOOL closeToStart = (fabs(_touchStartPoint.x - pt.x) < 10 && fabs(_touchStartPoint.y - pt.y) < 10);

// we must check on equality of the ranges themselves since the data detectors create new results
if (_activeLink && (NSEqualRanges(_activeLink.range,linkAtTouchesEnded.range) || closeToStart)) {
// Same link on touchEnded than the one on touchBegan, so trigger it
[self processLongPressActiveLink];
}
}

}

-(void)_gestureRecognised:(UIGestureRecognizer*)recogniser
{
CGPoint pt = [recogniser locationInView:self];
Expand Down Expand Up @@ -491,6 +513,14 @@ -(void)_gestureRecognised:(UIGestureRecognizer*)recogniser
}
}

- (void)processLongPressActiveLink {
NSTextCheckingResult* linkToOpen = _activeLink;

if ([self.delegate respondsToSelector:@selector(attributedLabel:longPressedLink:)]) {
[self.delegate attributedLabel:self longPressedLink:linkToOpen];
}
}

- (void)processActiveLink
{
NSTextCheckingResult* linkToOpen = _activeLink;
Expand Down Expand Up @@ -677,7 +707,7 @@ - (CGSize)sizeThatFits:(CGSize)size
/////////////////////////////////////////////////////////////////////////////////////

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return ([[otherGestureRecognizer.view class] isSubclassOfClass:[UIScrollView class]]);
return (otherGestureRecognizer == _longTapGestureReconizer || otherGestureRecognizer == _gestureRecogniser || [[otherGestureRecognizer.view class] isSubclassOfClass:[UIScrollView class]]);
}


Expand Down