0

我想得到一个框,其中一个 NSString 的某个子字符串已经在 UILabel(或 UITextView,如果更容易的话)中呈现,考虑到整个 NSString 被绘制的矩形,pus 换行模式,字体,等等。在 OSX 中,在添加中,有一个方法可以返回该矩形

- (NSRect)boundingRectWithSize:(NSSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes

iOS有类似的东西吗?我检查了文档,但没有找到任何东西。有没有类似的东西?

4

1 回答 1

2

我想,这可能对你有帮助

// Create some text view for example
_tv = [[UITextView alloc] initWithFrame:CGRectMake(10.0, 10.0, 300.0f, 250.0f)];
_tv.text = @"dkjshfk shf kjhs fkj ewkjhf kwfwkj fhwk fwh fjkw hfjkhwe fkjwh";
_tv.font = [UIFont systemFontOfSize:24];
[self.view addSubview:_tv];

这就是你要找的

- (void)someAction:(id)sender
{
    // Here is a frame of selected text in text view
    CGRect frame = [_tv firstRectForRange:_tv.selectedTextRange];

    // Mark selected text with yellow
    UIView *v = [[[UIView alloc] initWithFrame:frame] autorelease];
    v.backgroundColor = [UIColor yellowColor];
    v.alpha = .8f;
    [_tv addSubview:v];
}
于 2013-01-23T12:24:31.740 回答