2016-12-27 93 views
2

我正在寻找一种方法来更改气泡中的消息文本颜色,我发现在ObjC示例中很容易,试图在swift中做同样的事情,但失败了,任何解决方案?更改聊天气泡中的textview颜色

这里是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

+0

后,你已经尝试和没有工作的SWIFT代码。 –

+0

同样的代码翻译成swift,它不会工作 – Abdoelrhman

+0

@AbdoelrhmanMohamed你需要告诉我们你有什么尝试和什么不工作,告诉我们它的截图。 –

回答

3

崩溃从客观上面发布的c代码需要创建JSQMessagesCollectionViewCell使用super.collectionView(_:cellForRowAt:)的实例,请尝试这样。

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 
    }