UILabel获取每一个字符的实际位置

/*

 最近的一个需求在一大段自动换行的UILabel最后一行隔一段位置添加一个编辑按钮,但是代码上并不允许使用Autolayout,最开始想了2种可行方式

 1.获取最后一个字符的位置 然后把按钮放在这后边。

 2.获取最后一行文字内容然后计算宽度 然后把按钮放在这后边。

 后来测试中发现当里边数据有表情符号的时候这两种方式的计算都是有误差的,所有并不能实现最开始想要的结果,后来又想了两种方式,一种是NSTextAttachment插入图片。这个以后再说。另一种就是

 @property (strong, nonatomic) NSTextStorage *textStorage;

 @property (strong, nonatomic) NSLayoutManager *layoutManager;

 @property (strong, nonatomic) NSTextContainer *textContainer;

 用这些来获取每一个字符的实际位置

 */

@interface ViewController ()

{

UIView *flagView;//遮罩框

NSInteger tag;//记录点击tag

}

@property (weak, nonatomic) IBOutlet UILabel *label;

@property (strong, nonatomic) NSTextStorage *textStorage;

@property (strong, nonatomic) NSLayoutManager *layoutManager;

@property (strong, nonatomic) NSTextContainer *textContainer;

@end

  • (IBAction)buttonAction:(id)sender {

    flagView.frame = [self characterRectAtIndex:tag];

    tag++;

    if (tag == self.label.text.length) {

      tag = 0;
    

    }

}

  • (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

self.textStorage = [NSTextStorage new];

self.layoutManager = [NSLayoutManager new];

self.textContainer = [NSTextContainer new];

[self.textStorage addLayoutManager:self.layoutManager];

[self.layoutManager addTextContainer:self.textContainer];



tag = 0;

[_label sizeToFit];

flagView = [UIView new];

flagView.frame = CGRectZero;

flagView.layer.borderColor = [UIColor redColor].CGColor;

flagView.layer.borderWidth = 1.f;

flagView.clipsToBounds = YES;

[self.label addSubview:flagView];

}

  • (void)viewDidLayoutSubviews

{

[super viewDidLayoutSubviews];

[self configWithLabel:self.label];

}

  • (void)configWithLabel:(UILabel *)label

{

self.textContainer.size = label.bounds.size;

self.textContainer.lineFragmentPadding = 0;

self.textContainer.maximumNumberOfLines = label.numberOfLines;

self.textContainer.lineBreakMode = label.lineBreakMode;



NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:label.text];

NSRange textRange = NSMakeRange(0, attributedText.length);

[attributedText addAttribute:NSFontAttributeName value:label.font range:textRange];

NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];

paragraphStyle.alignment = label.textAlignment;

[attributedText addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:textRange];

[self.textStorage setAttributedString:attributedText];

}

  • (CGRect)characterRectAtIndex:(NSUInteger)charIndex

{

//传回self.layoutManager的位置 实际就是字符的fram

if (charIndex >= self.textStorage.length) {

    return CGRectZero;

}

NSRange characterRange = NSMakeRange(charIndex, 1);

NSRange glyphRange = [self.layoutManager glyphRangeForCharacterRange:characterRange actualCharacterRange:nil];

return [self.layoutManager boundingRectForGlyphRange:glyphRange inTextContainer:self.textContainer];

}

作者:黄权浩
来源:CSDN
原文:https://blog.csdn.net/quanhaoh/article/details/78672815
版权声明:本文为博主原创文章,转载请附上博文链接!

你可能感兴趣的:(UILabel获取每一个字符的实际位置)