2016-11-26 34 views
1

我想通過繼承UIButton做一個多行按鈕。爲了避免得出兩個自定義UILabel(我還是很新的雨燕/ Xcode的),我使用的歸因字符串現有UILabel和分割線與新線的性格,就像這樣:多行UIButton與每行獨立截尾

func prepareAttributedTitle(_ primaryTitle: String = "", _ secondaryTitle: String = "") { 
    let title = NSMutableAttributedString() 
    let first = NSAttributedString(string: primaryTitle, attributes: [ 
     NSForegroundColorAttributeName: tintColor, 
     NSFontAttributeName: UIFont.systemFont(ofSize: UIFont.systemFontSize, weight: UIFontWeightSemibold) 
    ]) 
    let newLine = NSAttributedString(string: "\n") 
    let second = NSAttributedString(string: secondaryTitle, attributes: [ 
     NSForegroundColorAttributeName: tintColor.withAlphaComponent(0.75), 
     NSFontAttributeName: UIFont.systemFont(ofSize: UIFont.smallSystemFontSize) 
    ]) 

    title.append(first) 
    title.append(newLine) 
    title.append(second) 

    setAttributedTitle(title, for: .normal) 
} 

而結果是(很抱歉,我沒有足夠的代表,發佈圖片):

| This is the long first | 
| line     | 
| Secondary line   | 

不過,我想獨立截斷線,像這樣:

| This is the long fi... | 
| Secondary line   | 

有沒有辦法做到這一點,而不使用兩個自定義UILabels?

感謝

回答

0

UILabel不支持你所需要的。您將不得不使用兩個單線標籤,每個標籤都設置尾部截斷。

0

我在回答我自己的問題,對我有用。這裏是我的UIButton的子類,但請記住我不是一個有經驗的開發人員。還有一些造型和色調的支持:

import UIKit 

@IBDesignable 

class FilterButton: UIButton { 

    let primaryLabel = UILabel() 
    let secondaryLabel = UILabel() 

    @IBInspectable var primaryTitle = "" { 
     didSet { 
      primaryLabel.text = primaryTitle 
     } 
    } 
    @IBInspectable var secondaryTitle = "" { 
     didSet { 
      secondaryLabel.text = secondaryTitle 
     } 
    } 

    // MARK: Initialization 
    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     commonInit() 
    } 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
     commonInit() 
    } 

    override func prepareForInterfaceBuilder() { 
     primaryTitle = "Primary title" 
     secondaryTitle = "Secondary title" 
     commonInit() 
    } 

    func commonInit() { 
     // Force left alignment (FIXME: Use user language direction) 
     contentHorizontalAlignment = .left 

     // Set some padding and styling 
     contentEdgeInsets = UIEdgeInsets(top: 5, left: 10, bottom: 5, right: 10) 
     layer.cornerRadius = 5 
     layer.borderWidth = 1 

     // Hide button original label 
     titleLabel?.isHidden = true 

     // Prepare the primary label 
     primaryLabel.frame = CGRect(x: contentEdgeInsets.left, 
            y: contentEdgeInsets.top, 
           width: frame.width - contentEdgeInsets.left - contentEdgeInsets.right, 
           height: (frame.height - contentEdgeInsets.top - contentEdgeInsets.bottom)/2) 
     primaryLabel.font = UIFont.boldSystemFont(ofSize: UIFont.systemFontSize) 
     primaryLabel.textColor = tintColor 
     // primaryLabel.backgroundColor = UIColor.green // For debugging 
     primaryLabel.lineBreakMode = .byTruncatingMiddle // Truncate first line 
     primaryLabel.autoresizingMask = .flexibleWidth 
     addSubview(primaryLabel) 

     // Prepare the secondary label 
     secondaryLabel.frame = CGRect(x: contentEdgeInsets.left, 
             y: contentEdgeInsets.top + primaryLabel.frame.height, 
            width: frame.width - contentEdgeInsets.left - contentEdgeInsets.right, 
           height: (frame.height - contentEdgeInsets.top - contentEdgeInsets.bottom)/2) 
     secondaryLabel.font = UIFont.systemFont(ofSize: UIFont.smallSystemFontSize) 
     secondaryLabel.textColor = tintColor.withAlphaComponent(0.75) 
     // secondaryLabel.backgroundColor = UIColor.yellow // For debugging 
     secondaryLabel.lineBreakMode = .byTruncatingMiddle // Truncate second line 
     secondaryLabel.autoresizingMask = .flexibleWidth 
     addSubview(secondaryLabel) 

     primaryLabel.text = primaryTitle 
     secondaryLabel.text = secondaryTitle 
    } 

    // Support tint color 
    override func tintColorDidChange() { 
     super.tintColorDidChange() 
     layer.borderColor = tintColor.cgColor 
     layer.backgroundColor = tintColor.withAlphaComponent(0.05).cgColor 

     primaryLabel.textColor = tintColor 
     secondaryLabel.textColor = tintColor.withAlphaComponent(0.75) 
    } 
}