我的应用程序从API中提取HTML,将其转换为NSAttributedString(为了允许可点击的链接)并将其写入AutoLayout表中的一行.麻烦的是,每当我调用这种类型的单元格时,高度都会被错误计算并且内容会被切断.我尝试过不同的行高计算实现,但都没有正常工作.
如何准确,动态地计算其中一行的高度,同时仍保持点击HTML链接的能力?
我的代码如下.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
switch(indexPath.section) {
...
case kContent:
{
FlexibleTextViewTableViewCell* cell = (FlexibleTextViewTableViewCell*)[TableFactory getCellForIdentifier:@"content" cellClass:FlexibleTextViewTableViewCell.class forTable:tableView withStyle:UITableViewCellStyleDefault];
[self configureContentCellForIndexPath:cell atIndexPath:indexPath];
[cell.contentView setNeedsLayout];
[cell.contentView layoutIfNeeded];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.desc.font = [UIFont fontWithName:[StringFactory defaultFontType] size:14.0f];
return cell;
}
...
default:
return nil;
}
}
Run Code Online (Sandbox Code Playgroud)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UIFont *contentFont = [UIFont fontWithName:[StringFactory defaultFontType] size:14.0f];
switch(indexPath.section) {
...
case kContent:
return [self textViewHeightForAttributedText:[self convertHTMLtoAttributedString:myHTMLString] andFont:contentFont andWidth:self.tappableCell.width];
break;
...
default:
return …Run Code Online (Sandbox Code Playgroud) 我有以下代码,并希望使我的文本的一部分可以点击并调用另一个UIViewController(而不是一个网站).
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"testing it out @clickhere"];
NSInteger length = str.length;
[str addAttribute:NSForegroundColorAttributeName value:[UIColor bestTextColor] range:NSMakeRange(0,length)];
Run Code Online (Sandbox Code Playgroud)
NSMutableAttributedString设置为UILabel,如下所示:
label.attributedText = str;
Run Code Online (Sandbox Code Playgroud)
什么是最好的方法呢?我似乎无法找到一个好的答案.
我想要的一个例子是假设我有一个UILabel,如下所示:
This is my label. Click here to go to UIViewController1 and then go to UIViewController1 by this #tag.
Run Code Online (Sandbox Code Playgroud)
我希望第一次单击事件传递"here"文本,并将"#tag"一词传递给同一个click事件.
iphone uibutton nsattributedstring ios uitapgesturerecognizer