2012-12-12 31 views
1

我正在使用OHAttributeLabel将自定义链接添加到我的标签文本。我正在使用的代码粘贴在下面。它曾经使用旧版OHA分布式标签(2010),但是随着新版本(最近更新),我的标签中的文本不再可以作为链接点击。无法将自定义链接添加到OHAttributeLabel

任何人都可以建议我在这里失踪?

// Set Question Label 
Question *question = self._answerForCell.question; 
NSString *questionText = [NSString stringWithFormat:@"Q: %@", question.text]; 
CustomOHAttributLabel *thisQuestionLabel = (CustomOHAttributLabel *)[self.contentView viewWithTag:QUESTIONLABEL_TAG]; 

//Set up dictionary for question 
NSString *questionStr = [question.text stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 

NSString *urlForQn = [NSString stringWithFormat:@"dailythingsfm://redirect_to/questions/%@/answers?text=%@&nickname=%@&question_id=%@&question_curious=%i&showEveryOneTab=%i", question.slug, questionStr, [[UserInfo sharedUserInfo] getNickname], question.qid, question.curious, 1]; 
NSString *qnStartIndex = @"0"; 
NSString *qnLength = [NSString stringWithFormat:@"%i", [questionText length]]; 

NSDictionary *qnDict = [NSDictionary dictionaryWithObjectsAndKeys:qnStartIndex, @"start", qnLength, @"length", urlForQn, @"url", nil]; 
NSArray *array = [NSArray arrayWithObject:qnDict]; 
[thisQuestionLabel setLabelwithText:questionText fontSize:QUESTION_FONT_SIZE andSubStringToURLArrayViaRange:array withHexColor:@"#555555"]; 


//Method to set the text in UILabel to a custom link 
- (void)setLabelwithText:(NSString *)text fontSize:(CGFloat)fontSize andSubStringToURLArrayViaRange:(NSArray *)array withHexColor:(NSString *)textColor 
{ 
    NSMutableAttributedString *attrStr = [NSMutableAttributedString attributedStringWithString:text]; 
    [attrStr setFont:[UIFont systemFontOfSize:fontSize]]; 
    [attrStr setTextColor:[UIColor grayColor]]; 
    [self removeAllCustomLinks]; 

    for (NSDictionary *dict in array) { 
     NSString *start = [dict objectForKey:@"start"]; 
     NSString *length = [dict objectForKey:@"length"]; 
     NSString *url = [dict objectForKey:@"url"]; 

     NSUInteger startIndex = [start intValue]; 
     NSUInteger len = [length intValue]; 

     NSRange range = NSMakeRange(startIndex, len); 

     [attrStr setFont:[UIFont boldSystemFontOfSize:fontSize] range:range]; 
     [attrStr setTextColor:[UIColor colorWithHexString:textColor] range:range]; 
     [self addCustomLink:[NSURL URLWithString:url] inRange:range]; 
    } 

    self.attributedText = attrStr; 
} 
+2

你是否实施处理链接操作的委托方法? – gdavis

+0

是的,我是。其实我只是意识到了这个问题。似乎addCustomLink方法不起作用,我需要使用最新库版本“setLink”附带的另一种方法。 – Zhen

回答

0

我必须使用“setLink”的方法,而不是addCustomLink最新OHAttribute库(3.2.1)

相关问题