2016-12-27 94 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 
    }