Skip to content

Instantly share code, notes, and snippets.

@choefele
Created January 25, 2014 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save choefele/8614533 to your computer and use it in GitHub Desktop.
Save choefele/8614533 to your computer and use it in GitHub Desktop.
//
// CCHLinkLabel.h
// Stolpersteine
//
// Created by Hoefele, Claus(choefele) on 21.01.14.
// Copyright (c) 2014 Option-U Software. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface CCHLinkLabel : UILabel
@end
//
// CCHLinkLabel.m
// Stolpersteine
//
// Created by Hoefele, Claus(choefele) on 21.01.14.
// Copyright (c) 2014 Option-U Software. All rights reserved.
//
#import "CCHLinkLabel.h"
@implementation CCHLinkLabel
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setUp];
}
return self;
}
- (void)awakeFromNib
{
[self setUp];
}
- (void)setUp
{
self.userInteractionEnabled = YES;
self.backgroundColor = UIColor.grayColor;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textTapped:)];
[self addGestureRecognizer:tapGestureRecognizer];
}
- (void)textTapped:(UITapGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateEnded) {
CGPoint location = [recognizer locationInView:self];
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:self.attributedText];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
[textStorage addLayoutManager:layoutManager];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:self.bounds.size];
[layoutManager addTextContainer:textContainer];
textContainer.maximumNumberOfLines = self.numberOfLines;
textContainer.lineBreakMode = self.lineBreakMode;
NSUInteger characterIndex = [layoutManager characterIndexForPoint:location
inTextContainer:textContainer
fractionOfDistanceBetweenInsertionPoints:NULL];
if (characterIndex < textStorage.length) {
NSRange range = NSMakeRange(characterIndex, 1);
NSString *value = [self.text substringWithRange:range];
NSLog(@"%@, %zd, %zd", value, range.location, range.length);
}
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment