2017-08-16 57 views
0

調節按鈕的寬度我有一個表視圖,並具有button.According到我的需要,我有時SET按鈕的如何根據或長度限制:Textlength圖像化背景

1.Title自定義單元格

2.Background而不標題

圖像上runtime.The問題是我的按鈕的大小具體寬度根據標題長度限制:Textlength的或根據背景圖像size.I沒有改變已嘗試sizeToFi噸cell.btnChat.sizeThatFits(backgroundimage.size)但它一點兒也不反映任何變化。

「按鈕」條件是組標題和圖像來設置背景圖像。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 


      if blockChatForLogin{ 
       if (chatRespones[indexPath.row].type == "text"){ 
        //Adding rows in table 
//     let cell:ChatResponseController = self.tbChat.dequeueReusableCell(withIdentifier: "ChatResponseCell") as! ChatResponseController 
        let cell:CustLabelCell = self.tbChat.dequeueReusableCell(withIdentifier: "CustomLabelCell") as! CustLabelCell 
        cell.lblTextResponse.text = chatRespones[indexPath.row].value 
        cell.lblTextResponse.layer.masksToBounds=true 
        cell.lblTextResponse.layer.cornerRadius=5 
        return cell 
       } 
       else if (chatRespones[indexPath.row].type == "button"){ 
        //Adding rows in table 
        let cell:CustButtonCell = self.tbChat.dequeueReusableCell(withIdentifier: "CustomButtonCell") as! CustButtonCell 
        cell.btnChat.setTitle(chatRespones[indexPath.row].value,for: .normal) 
        cell.btnChat.layer.masksToBounds=true 
        cell.btnChat.layer.cornerRadius=5 
        cell.btnChat.layer.borderWidth=2 
        cell.btnChat.layer.borderColor=UIColor(red:0.18, green:0.61, blue:0.49 ,alpha:1.0).cgColor 
        cell.delegete = self 
        return cell 
       } 
       else if (chatRespones[indexPath.row].type == "image"){ 
        //Adding rows in table 
//     let cell:ChatButtonController = self.tbChat.dequeueReusableCell(withIdentifier: "ChatButtonCell") as! ChatButtonController 
        let cell:CustButtonCell = self.tbChat.dequeueReusableCell(withIdentifier: "CustomButtonCell") as! CustButtonCell 

        if let image: UIImage = UIImage(named:"\(chatRespones[indexPath.row].value.replacingOccurrences(of: ":", with: "")).jpg") 
        { 
         cell.btnChat.setImage(image, for: .normal) 
         cell.btnChat.setTitle("",for: .normal) 
         cell.btnChat.backgroundColor = UIColor.clear 


        } 
        else{ 
         cell.btnChat.backgroundColor = UIColor.clear 
         cell.btnChat.setTitleColor(UIColor.red, for: .normal) 
         cell.btnChat.setTitle("Error: Image Missing with name \(chatRespones[indexPath.row].value)",for: .normal) 
        } 
        cell.btnChat.layer.masksToBounds=true 
        cell.btnChat.layer.cornerRadius=5 
        cell.btnChat.layer.borderWidth=2 
        cell.btnChat.layer.borderColor=UIColor(red:0.18, green:0.61, blue:0.49 ,alpha:1.0).cgColor 
        cell.delegete = self 
        return cell 
       } 
      } 
     default: 
      print("No case") 
     } 

    } 
+2

顯示你的代碼,其中u根據長度限制:Textlength –

+0

設置寬度不給在故事板按鈕的一個固定的寬度,它根據文本調整自我。 –

+0

在標題的情況下,你應該先算甜心或文本在標籤和的UIImageView的情況下獲得的高度和圖片的寬度,然後設置動態buttonview高度和寬度根據你文字和ImageView的。 –

回答

0

在您設置單元格的文本,你可以這樣做:

cell.textLabel.intrinsicContentWidth 

來獲取文本所需的寬度,那麼你可以使用檢索到的尺寸創建視圖的框架作爲寬度。要小心,如果您有任何固定的寬度在你的故事板什麼的設置,所以它可能是最好的創建爲textLabel編程爲好。