2017-07-18 81 views
1

我有按钮。为了获得按钮的标题,我向服务器发送请求并设置Button的标题。现在我需要设置自动调整大小这个按钮。这个怎么做 ?我在按钮的身份检查器中将LineWrap设置为LineWrap。现在文本显示为多行,但我也需要调整按钮的大小,因为一些文本太大而且太小。这是它在故事板中的样子,约束和约束都可以,我只需要调整按钮大小。 enter image description here如何根据标题的按钮将autoSize设置为UIbutton?

import UIKit 

@IBDesignable 
class ButtonTableViewCell: UITableViewCell { 

@IBInspectable var selectedColor : UIColor = UIColor.init(red: 34/255, green: 89/255, blue: 128/255, alpha: 1.0) 
@IBInspectable var normalColor : UIColor = UIColor.init(red: 59/255, green: 169/255, blue: 246/255, alpha: 1.0) 


@IBOutlet weak var variant4button: UIButton! 
@IBOutlet weak var variant3button: UIButton! 
@IBOutlet weak var variant2Button: UIButton! 
@IBOutlet weak var variant1button: UIButton! 

这里是4个按钮。 这里我设置标题:

if (numberOfVariants.count != 0) { 
      let questionTextView = cell.contentView.viewWithTag(5) as! UITextView 
      questionTextView.text = "\(Questions[indexPath.row].content!)" 
      variant1.addTarget(self, action: #selector(self.variant1ButtonPressed), for: .touchUpInside) 
      variant2.addTarget(self, action: #selector(self.variant2ButtonPressed), for: .touchUpInside) 
      variant3.addTarget(self, action: #selector(self.variant3ButtonPressed), for: .touchUpInside) 
      variant4.addTarget(self, action: #selector(self.variant4ButtonPressed), for: .touchUpInside) 
      let numberOfVars = numberOfVariants[indexPath.row] 
      if (numberOfVars == 2) { 
       variant1.setTitle(Variants[0+amaunty[indexPath.row]].title, for: .normal) 
       variant2.setTitle(Variants[1+amaunty[indexPath.row]].title, for: .normal) 
       variant3.isHidden = true 
       variant4.isHidden = true 
      } 
      else if (numberOfVars == 3){ 
       variant1.setTitle(Variants[0+amaunty[indexPath.row]].title, for: .normal) 
       variant2.setTitle(Variants[1+amaunty[indexPath.row]].title, for: .normal) 
       variant3.setTitle(Variants[2+amaunty[indexPath.row]].title, for: .normal) 
       variant4.isHidden = true 
      } 
      else if (numberOfVars == 4) { 
       variant1.setTitle(Variants[0+amaunty[indexPath.row]].title, for: .normal) 
       variant2.setTitle(Variants[1+amaunty[indexPath.row]].title, for: .normal) 
       variant3.setTitle(Variants[2+amaunty[indexPath.row]].title, for: .normal) 
       variant4.setTitle(Variants[3+amaunty[indexPath.row]].title, for: .normal) 
      } 
     } 

为Jaydeep维亚斯 enter image description here

回答

1

只要你有创建类uibutton并分配此cla SS所需按钮

class AutoSizableButton: UIButton 
{ 
    override var intrinsicContentSize: CGSize 
     { 
     get { 
      let labelSize = titleLabel?.sizeThatFits(CGSize(width: self.frame.size.width, height: CGFloat.greatestFiniteMagnitude)) ?? CGSize.zero 
      let reqiredButtonSize = CGSize(width: labelSize.width + titleEdgeInsets.left + titleEdgeInsets.right, height: labelSize.height + titleEdgeInsets.top + titleEdgeInsets.bottom) 

      return reqiredButtonSize 
     } 
    } 
} 

也不fogot在viewDidLoad中

添加此
self.btnDemo.titleLabel?.numberOfLines = 0; 
     self.btnDemo.titleLabel?.lineBreakMode = .byWordWrapping; 

输出 enter image description here

+0

它不工作,我添加了这个代码,但一切都是一样的。请结帐我已将照片添加到问题 –

+0

@АзаматБулегенов对我来说,完美的工作已分配任何新的约束或忘记将类分配给按钮 –

+0

我认为我的单元格TableViewCell中的问题不允许将大小更改为此按钮。你能给我你的电子邮件吗?我会寄给你,也许你会更好理解? –

0

试试这个程序:在你的代码

- (float) getRequiredWidth : (UIButton *)button 
{ 
    float requiredWidth = 
    [button.titleLabel.text 
    boundingRectWithSize:button.frame.size 
    options:NSStringDrawingUsesLineFragmentOrigin 
    attributes:@{ NSFontAttributeName:button.font } 
    context:nil] 
    .size.width; 

    return requiredWidth; 
} 

float totalWidth = [self getRequiredWidth : yourButton]; 
yourButton.frame = CGRectMake(yourButtonX, yourButtonY, totalWidth, yourButtonHeight) 
+0

我没有到按钮的宽度任何约束,这是不加工。大小不变 –