Skip to content

Commit

Permalink
UILabel增加点击测试
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenglibao committed Jun 2, 2020
1 parent 5bf0d40 commit 0ad1296
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions FlexLib/Classes/ViewExt/UILabel+Flex.h
Expand Up @@ -32,5 +32,10 @@

-(void)updateAttributeText;

/**
* 返回点对应的字符索引,pt为UILabel自身坐标系的点
*/
-(NSUInteger)charIndexAtPoint:(CGPoint)pt;

@end

6 changes: 6 additions & 0 deletions FlexLib/Classes/ViewExt/UILabel+Flex.m
Expand Up @@ -73,6 +73,12 @@ - (NSUInteger)charIndexOfTap:(UITapGestureRecognizer *)recognizer
{
CGPoint location = [recognizer locationInView:self];

return [self charIndexAtPoint:location];
}

-(NSUInteger)charIndexAtPoint:(CGPoint)location
{

NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:self.attributedText];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
[textStorage addLayoutManager:layoutManager];
Expand Down

0 comments on commit 0ad1296

Please sign in to comment.