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

来源:互联网 发布:mac屏幕最大化快捷键 编辑:程序博客网 时间:2024/06/12 16:18

/*

     最近的一个需求在一大段自动换行的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 = [selfcharacterRectAtIndex:tag];

    tag++;

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

        tag = 0;

    }

}


- (void)viewDidLoad {

    [superviewDidLoad];

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

    

    self.textStorage = [NSTextStoragenew];

    self.layoutManager = [NSLayoutManagernew];

    self.textContainer = [NSTextContainernew];

    [self.textStorageaddLayoutManager:self.layoutManager];

    [self.layoutManageraddTextContainer:self.textContainer];

    

    tag = 0;

    [_labelsizeToFit];

    flagView = [UIViewnew];

    flagView.frame =CGRectZero;

    flagView.layer.borderColor = [UIColorredColor].CGColor;

    flagView.layer.borderWidth =1.f;

    flagView.clipsToBounds =YES;

    [self.labeladdSubview:flagView];

    

}


- (void)viewDidLayoutSubviews

{

    [superviewDidLayoutSubviews];

    [selfconfigWithLabel: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 = [[NSMutableAttributedStringalloc] initWithString:label.text];

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

    [attributedText addAttribute:NSFontAttributeNamevalue:label.font range:textRange];

    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStylenew];

    paragraphStyle.alignment = label.textAlignment;

    [attributedText addAttribute:NSParagraphStyleAttributeNamevalue:paragraphStyle range:textRange];

    [self.textStoragesetAttributedString:attributedText];

}


- (CGRect)characterRectAtIndex:(NSUInteger)charIndex

{

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

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

        returnCGRectZero;

    }

    NSRange characterRange = NSMakeRange(charIndex, 1);

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

    return [self.layoutManagerboundingRectForGlyphRange:glyphRange inTextContainer:self.textContainer];

}



阅读全文
0 0