NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:labelText];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
[textStorage addLayoutManager:layoutManager];
CGRect bounds = label.bounds;
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:bounds.size];
[layoutManager addTextContainer:textContainer];
Now you just need to find the index of the character that was clicked, which is simple!
NSUInteger characterIndex = [layoutManager characterIndexForPoint:location
inTextContainer:textContainer
fractionOfDistanceBetweenInsertionPoints:NULL];
Which makes it trivial to find the word itself:
if (characterIndex < textStorage.length) {
[labelText.string enumerateSubstringsInRange:NSMakeRange(0, textStorage.length)
options:NSStringEnumerationByWords
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
if (NSLocationInRange(characterIndex, enclosingRange)) {
// Do your thing with the word, at range 'enclosingRange'
*stop = YES;
}
}];
}
다른 방법
1) Subclass UILabel, and add a touchesEnded: to recognize touches on your label.
2) Store in an array all the sizes of the letters using system method sizeWithFont:
3) Get the touch point and compare with the letters size.
Done!
Create an UILabel subclass
//
// APLabel.h
//
#import <uikit /UIKit.h>
@protocol APLabelDelegate <nsobject>
@required
- (void) didLetterFound:(char)letter;
@end
@interface APLabel : UILabel
@property (nonatomic, assign) id <aplabeldelegate> delegate;
@end
// APLabel.h
//
#import <uikit /UIKit.h>
@protocol APLabelDelegate <nsobject>
@required
- (void) didLetterFound:(char)letter;
@end
@interface APLabel : UILabel
@property (nonatomic, assign) id <aplabeldelegate> delegate;
@end
//
// APLabel.m
//
#import "APLabel.h"
@implementation APLabel
- (id)initWithCoder:(NSCoder *)aDecoder
{
if ( self = [super initWithCoder:aDecoder])
{
self.backgroundColor = [UIColor greenColor];
self.userInteractionEnabled = YES;
}
return self;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[touches allObjects] objectAtIndex:0];
CGPoint pos = [touch locationInView:self];
int sizes[self.text.length];
for ( int i=0; i<self .text.length; i++ )
{
char letter = [self.text characterAtIndex:i];
NSString *letterStr = [NSString stringWithFormat:@"%c", letter];
CGSize letterSize = [letterStr sizeWithFont:self.font];
sizes[i] = letterSize.width;
}
int sum = 0;
for ( int i=0; i<self.text.length; i++)
{
sum += sizes[i];
if ( sum >= pos.x )
{
[ _delegate didLetterFound:[ self.text characterAtIndex:i] ];
return;
}
}
}
@end
// APLabel.m
//
#import "APLabel.h"
@implementation APLabel
- (id)initWithCoder:(NSCoder *)aDecoder
{
if ( self = [super initWithCoder:aDecoder])
{
self.backgroundColor = [UIColor greenColor];
self.userInteractionEnabled = YES;
}
return self;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[touches allObjects] objectAtIndex:0];
CGPoint pos = [touch locationInView:self];
int sizes[self.text.length];
for ( int i=0; i<self .text.length; i++ )
{
char letter = [self.text characterAtIndex:i];
NSString *letterStr = [NSString stringWithFormat:@"%c", letter];
CGSize letterSize = [letterStr sizeWithFont:self.font];
sizes[i] = letterSize.width;
}
int sum = 0;
for ( int i=0; i<self.text.length; i++)
{
sum += sizes[i];
if ( sum >= pos.x )
{
[ _delegate didLetterFound:[ self.text characterAtIndex:i] ];
return;
}
}
}
@end
'ios' 카테고리의 다른 글
json dictionary로 가져오기 (0) | 2017.01.20 |
---|---|
일정 시간 지연후 실행 (0) | 2016.12.19 |
ios info.plist 권한수정 (0) | 2016.12.01 |
WRITTEN BY
,