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];

}



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄权浩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值