2017-07-18 39 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

我沒有到按鈕的寬度任何約束,這是不加工。大小不變 –

相關問題