gpt4 book ai didi

swift - 更改聊天气泡中的 TextView 颜色

转载 作者:可可西里 更新时间:2023-11-01 00:48:48 25 4
gpt4 key购买 nike

我正在寻找一种更改气泡中消息文本颜色的方法,我发现在 ObjC 示例中很容易,尝试快速执行相同操作但失败了,有什么解决方案吗?

这是 ObjC 代码

- (UICollectionViewCell *)collectionView:(JSQMessagesCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
/**
* Override point for customizing cells
*/
JSQMessagesCollectionViewCell *cell = (JSQMessagesCollectionViewCell *)[super collectionView:collectionView cellForItemAtIndexPath:indexPath];

/**
* Configure almost *anything* on the cell
*
* Text colors, label text, label colors, etc.
*
*
* DO NOT set `cell.textView.font` !
* Instead, you need to set `self.collectionView.collectionViewLayout.messageBubbleFont` to the font you want in `viewDidLoad`
*
*
* DO NOT manipulate cell layout information!
* Instead, override the properties you want on `self.collectionView.collectionViewLayout` from `viewDidLoad`
*/

JSQMessage *msg = [self.demoData.messages objectAtIndex:indexPath.item];

if (!msg.isMediaMessage) {

if ([msg.senderId isEqualToString:self.senderId]) {
cell.textView.textColor = [UIColor blackColor];
}
else {
cell.textView.textColor = [UIColor whiteColor];
}

cell.textView.linkTextAttributes = @{ NSForegroundColorAttributeName : cell.textView.textColor,
NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle | NSUnderlinePatternSolid) };
}
cell.accessoryButton.hidden = ![self shouldShowAccessoryButtonForMessage:msg];

return cell;
}**

我的快速试用 Swift version

根据需要将其编辑为 collectionview 并覆盖后,它再次崩溃

enter image description here

最佳答案

根据您在上面发布的 Objective c 代码,您需要使用 super.collectionView(_:cellForRowAt:) 创建 JSQMessagesCollectionViewCell 实例,所以请这样尝试。

let cell = super.collectionView(collectionView, cellForItemAt: indexPath) as! JSQMessagesCollectionViewCell

完整解决方案:

 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = super.collectionView(collectionView, cellForItemAt: indexPath) as! JSQMessagesCollectionViewCell

// messages to show
let msg = incomingMessages[indexPath.row]

if !msg.isMediaMessage {
if msg.senderId! == senderId {
cell.textView.textColor = UIColor.white
}else{
cell.textView.textColor = UIColor.black
}
cell.textView.linkTextAttributes = [NSForegroundColorAttributeName: cell.textView.textColor ?? UIColor.white]
}
return cell
}

关于swift - 更改聊天气泡中的 TextView 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41346175/

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