iOS 计算UILabel上某段文字的frame

释放双眼,带上耳机,听听看~!
@implementation UILabel (TextRect)

- (CGRect)boundingRectForCharacterRange:(NSRange)range
{
    NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:[self attributedText]];
    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
    [textStorage addLayoutManager:layoutManager];
    NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:[self bounds].size];
    textContainer.lineFragmentPadding = 0;
    [layoutManager addTextContainer:textContainer];
    NSRange glyphRange;
    [layoutManager characterRangeForGlyphRange:range actualGlyphRange:&glyphRange];
    return [layoutManager boundingRectForGlyphRange:glyphRange inTextContainer:textContainer];
}

 

人已赞赏
iOS文章

iOS tableView实现无限滚动

2020-2-8 9:25:34

iOS文章

iOS 页面跳转实现翻转动画

2020-2-8 10:25:46

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索