2017-08-16 30 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编程为好。