我想得到一个框,其中NSString的某个子字符串已经在UILabel(或UITextView,如果更容易的话)中呈现,同时考虑到绘制整个NSString的矩形,换行模式,字体等。在OSX中,在加法中,有一个返回该矩形的方法
- (NSRect)boundingRectWithSize:(NSSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes
iOS 有类似的东西吗?我检查了文档,但没有找到任何东西。外面有类似的东西吗?
我想,这可能会对你有所帮助
// 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];
}