gpt4 book ai didi

ios - TableView 单元格中的 NSAttributedString - 有时显示为纯文本

转载 作者:搜寻专家 更新时间:2023-10-30 20:18:03 25 4
gpt4 key购买 nike

我的表格 View 单元格中有一个 UITextView。这就是我将 NSAttributedString 分配给它的内容的方式:

- (void)setAttributedMessage:(NSAttributedString *)msg
{
self.bubbleView.textView.text = nil;
self.bubbleView.textView.font = nil;
self.bubbleView.textView.textColor = nil;
self.bubbleView.textView.textAlignment = NSTextAlignmentLeft;
self.bubbleView.textView.attributedText = msg;
self.bubbleView.textView.userInteractionEnabled = NO;
}

我的属性字符串包含一条消息文本和一个较小字体的日期,所以这就是我创建它的方式:

- (NSAttributedString *) attributedTextForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *txt = @"Some text";
NSMutableAttributedString *a = [[NSMutableAttributedString alloc] initWithString:txt attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:16]}];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.timeStyle = NSDateFormatterShortStyle;
df.dateStyle = NSDateFormatterNoStyle;
NSString *timestamp = [df stringFromDate:[NSDate date]];
[a appendAttributedString:[[NSAttributedString alloc]
initWithString:[NSString stringWithFormat:@" %@", timestamp]
attributes:@{NSFontAttributeName: [UIFont italicSystemFontOfSize:10]}]];

return [[NSAttributedString alloc] initWithAttributedString:a];
}

当显示 TableView 时,我得到的是纯文本 - 字体大小没有变化,也没有带日期的斜体。当重新显示单元格时,它会正确显示。因此,当我将单元格滚动到屏幕外并将其放回原处时,没问题。我也可以重新加载 TableView 以强制重新显示,但这个解决方案听起来真的很蹩脚。

知道那里发生了什么吗?

最佳答案

你可能不喜欢它,但最好的解决方案确实是“蹩脚”的:

[yourTableView reloadData];

这样,将为所有(可见)条目生成新 View ,尤其是屏幕上的条目。

关于ios - TableView 单元格中的 NSAttributedString - 有时显示为纯文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22166375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com