簡體   English   中英

UIButton自動調整按鈕字體大小Swift

[英]UIButton auto-adjust Button font size Swift

我嘗試進行一些測驗,並且回答按鈕有問題。 簡單事實:文本太長,我嘗試使用不同的方法針對單元格自動調整文本。 我目前的狀態:

        for btn in btnArr{
        btn.titleLabel!.minimumScaleFactor = 0.3
        btn.titleLabel!.numberOfLines = 0
        btn.titleLabel!.adjustsFontSizeToFitWidth = true
        btn.titleLabel?.baselineAdjustment = UIBaselineAdjustment.AlignCenters
        //btn.contentEdgeInsets = UIEdgeInsets(top: 3.0,left: 3.0,bottom: 3.0,right: 3.0)
    }

我希望有人對我有另一種選擇:)

問候,帕特里克

編輯:

 self.view.addConstraint(NSLayoutConstraint(item: btn.titleLabel!, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: btn, attribute:NSLayoutAttribute.Height, multiplier: 0.99, constant: 0))  

使用自動布局,您可以設置按鈕之間的間距以及最大和最小尺寸。 在所有標簽的代碼中使用:

self.button.titleLabel.numberOfLines = 0;
self.button.titleLabel.adjustsFontSizeToFitWidth = YES;

這樣,所有標簽都會調整文本大小。

對於對titleLabel的調整按鈕,請對titleLabel使用自動布局約束。 舉些例子:

  [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.button.titleLabel
                                                          attribute:NSLayoutAttributeHeight
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeHeight
                                                         multiplier:0.5
                                                           constant:0]];

這個約束定義titleLabel的高度的50% self.view高度。 現在,您可以根據需要調整約束。

這項工作在您的代碼中嗎?

假設單元格的高度和字體相同,則可以通過將“ numberOfLines”設置為非0的值(例如3或4)來限制文本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM