2011-08-10 58 views
1

我正在使用-drawRect:在UIView中绘制一个字符串。该字符串可能包含链接,我想使这些链接具有不同的颜色并使其可点击。可点击文本-drawRect:

有没有人知道最好(也是最简单)的方法来做到这一点?我担心我将不得不从字符串中过滤链接,为链接创建按钮,然后手动定位字符串的不同部分(包括按钮)。

这是我在-drawRect代码:

NSString *message = [cellData objectForKey:@"message"]; 
CGSize messageLabelSize = [sizeCalc sizeOfMessageLabel:message]; 
[message drawInRect:CGRectMake(kBoxPadding + kProfilePicWidth + kBoxPadding, kBoxPadding + nameLabelSize.height + kSpacingNameToMessage - kContentOffset, messageLabelSize.width, messageLabelSize.height) withFont:[UIFont fontWithName:@"Helvetica" size:13]]; 

我的消息字符串看起来是这样的:

“这是一个测试文本和它在 中间环节http://google.com/。 “

我希望结果看上去有点像http://google.com/是超链接。

回答

0

你不能从一个drawRect中,因为这只是绘制图形,你需要一个子视图:

NSString *message = @"This is a test text and it has a link http://google.com/ in the middle."; 
CGSize size = [message sizeWithFont:[UIFont boldSystemFontOfSize:11.0]] 
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake (0, 0, size.width, size.height)]; 
textVioew.editable = NO; 
[textView setDataDetectorTypes:UIDataDetectorTypeLink]; 
[textView setText:message]; 
[self.view addSubview:textView]; 
[textView release];