(IOS)计算UILabel上某段文字的frame

- (CGRect)boundingRectForCharacterRange:(NSRange)range

{

    NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:[self attributedText]];

    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];

    [textStorageaddLayoutManager:layoutManager];

    NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:[self bounds].size];

    textContainer.lineFragmentPadding=0;

    [layoutManageraddTextContainer:textContainer];

    NSRangeglyphRange;

    [layoutManagercharacterRangeForGlyphRange:range actualGlyphRange:&glyphRange];

    return[layoutManagerboundingRectForGlyphRange:glyphRangeinTextContainer:textContainer];

}

你可能感兴趣的:((IOS)计算UILabel上某段文字的frame)