对于给定的NSRange,我想找到一个CGRect在UILabel对应的的字形NSRange.例如,我想CGRect在"快速的棕色狐狸跳过懒狗"这句话中找到包含"狗"字样的字样.

诀窍是,UILabel有多行,而文本确实attributedText如此,所以找到字符串的确切位置有点困难.
我想在我的UILabel子类上写的方法看起来像这样:
- (CGRect)rectForSubstringWithRange:(NSRange)range;
Run Code Online (Sandbox Code Playgroud)
细节,对于有兴趣的人:
我的目标是能够创建一个具有UILabel的确切外观和位置的新UILabel,然后我可以制作动画.我已经把剩下的事情搞清楚了,但特别是这一步让我暂时退缩了.
到目前为止我尝试解决问题的方法是:
UITextView和UITextField而不是UILabel.我敢打赌,对此的正确答案涉及以下其中一项:
NSLayoutManager和textContainerForGlyphAtIndex:effectiveRange更新:这是一个github要点,我已经尝试了迄今为止解决这个问题的三件事:https://gist.github.com/bryanjclark/7036101
现在,NSAttributedString在iOS 6中可用.出于布局目的,我想知道如何在固定宽度下计算NSAttributedString所需的高度.我正在寻找与NSString相当的东西,- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size但是对于NSAttributedString.
要计算NSAttributedStrings的绘图大小,有两种方法可用:
- (CGSize)size 不能使用,因为它没有考虑任何宽度.- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options context:(NSStringDrawingContext *)context,但不知怎的,它没有给我正确的高度.我觉得这个方法很麻烦.如果我运行以下代码,它bounding size: 572.324951, 19.000000会让我忽略200的给定宽度.它应该给我一些像100的高度.
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
NSDictionary *attributes = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue" size:15], NSForegroundColorAttributeName : [UIColor blueColor]};
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Attributed String\n" attributes:attributes]];
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Attributed String\n" attributes:attributes]];
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Attributed String\n" attributes:attributes]];
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Attributed String\n" attributes:attributes]];
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Attributed String\n" attributes:attributes]];
CGRect frame = [attributedString boundingRectWithSize:CGSizeMake(200, 1000) options:0 …