编辑:与 NSAttributedString、OHAttributedLabel 或 TTTAttributedLabel 无关!
简短的问题:我在使用UILabel、 OHAttributedLabel 或 TTTAttributedLabel 时遇到同样的问题 - 滚动表格视图会破坏标签的显示。更多关于下面...
长格式:我有一个 NSAttributedString 对象,表示来自推文的文本。我已经根据推文中的各种内容修饰了字符串(例如,主题标签为蓝色,url 为绿色并带下划线,用户提及为红色)。
例如,这条推文:
@MeetGreen Keep up the good work. Say Hi to Nancy for me - she convinced me that I can be green and save green at the same time!
导致这个 NSAttributedString: (使用我编写的实用程序显示以帮助我查看发生了什么):
@MeetGreen{
NSColor = "UIDeviceRGBColorSpace 1 0 0 1";
NSFont = "<UICFFont: 0x93c57d0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 17px";
} Keep up the good work. Say Hi to Nancy for me - she convinced me that I can be green and save green at the same time!{
CTForegroundColor = "<CGColor 0x93c8f20> [<CGColorSpace 0x938b570> (kCGColorSpaceDeviceGray)] ( 0 1 )";
NSFont = "<UICFFont: 0x93c57d0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 17px";
}
生成 NSAttributedString 对象的代码似乎正在做我希望它做的事情,但是当我尝试在 UILabel(或 TTTAttributedLabel 或 OHAttributedLabel)中显示此对象时,我无法说服它正常工作。主要问题似乎是滚动行为 - 单元格无法正确绘制(文本处于某个奇怪的位置)。但是,如果我只是选择单元格(例如,通过触摸它),则单元格将完全按照应有的方式绘制。这让我认为问题在于我的委托方法中的表格滚动处理,而不是属性标签本身(因为 OHAttributedLabel 和 TTTAttributedLabel 的行为相同),但我不确定为什么会这样。(我还没有尝试过的一件事是纯文本和用于推文内容的纯 UILabel - 这是今晚的列表。好的,UILabel 和纯文本内容的行为相同。我究竟做错了什么???)
这是我的代码 tableView:cellForRowAtIndexPath:, tableView:heightForRowAtIndexPath: 和 tableView:didSelectRowAtIndexPath:; 请注意,此代码位于 UIViewController 子类中,而不是 UITableViewController 的子类中。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TweetingTweetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Tweet"];
TSTweet *tweet = [self.model tweetAtIndexPath:indexPath];
cell.userHandleLabel.text = tweet.user.screenName;
cell.userNameLabel.text = tweet.user.name;
cell.timestampLabel.text = timeStamp;
cell.tweetTextLabel.attributedText = [self.model decoratedStringForTweetAtIndexPath:indexPath];
UIImage *image = [UIImage imageNamed:@"avatar"];
cell.userImage.image = image;
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat tweetHeight = [self.model heightForTweetAtIndexPath:indexPath
usingWidth:label_width];
CGFloat height = tweetHeight + label_headroom + label_footroom;
CGFloat result = MAX(height,defaultHeight);
NSLog(@"[%@ %@] %@ (%@)\ntweetHeight=%f, height=%f, defaultHeight=%f, result=%f", NSStringFromClass([self class]), NSStringFromSelector(_cmd),indexPath,[self.model decoratedStringForTweetAtIndexPath:indexPath].string,tweetHeight,height,defaultHeight,result);
return result;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"[%@ %@] indexPath=%@", NSStringFromClass([self class]), NSStringFromSelector(_cmd),indexPath);
TweetingTweetCell *cell = (TweetingTweetCell *)[tableView cellForRowAtIndexPath:indexPath];
NSLog(@"decoratedString=%@",[self.model decoratedStringForTweetAtIndexPath:indexPath]);
[cell dumpDebugInfo];
[tableView deselectRowAtIndexPath:indexPath
animated:NO];
}
TweetingTweetCell 是 UITableViewCell 的子类,它包含 4 个对象:用于头像的 UIImageView、用于发送者屏幕名称的 UILabel、用于发送时间的 UILabel 以及用于推文内容的“标签”对象。正如我上面所说,我已经尝试过 OHAttributedLabel 和 TTTAttributedLabel 并且行为是相同的。
坏滚动:
- 文本和屏幕名称之间的所有空白都很奇怪。
选择时:
- 看看定位是如何固定的