多行UIButton每行自动调整大小

时间:2015-06-20 06:16:14

标签: ios swift uibutton nsattributedstring

我想使用标准UIButton,但我想要放在UIButton上的文字有......

  1. ...多行(每行是不同的字符串)和......
  2. ...每一行应具有不同的字体和大小......
  3. ...自动调整字体大小以适应按钮宽度(不是高度来制作它 更容易一点)
  4. enter image description here

    所以,即使很难我想为每一行设置(首选)字体大小,我希望字体大小自动缩小,以便每个单独的行很好地适合{{ 1}}(=与UIButton AutoShrink / Minimum Font Scale相同的行为。

    我不想要的东西:

    我不想开始将UILabel添加到UILabel(例如子视图)或者使用IB将UILabel放在场景上并且只是围绕它画一个UIButton(为什么:我想要标准{{1}突出行为)

    我想要的是什么:

    一个干净的解决方案,使用属性字符串,给定宽度缩小字体(我猜想更新属性字符串),如果需要,逐行。

    我的想法,实现这样的功能:

    UIButton

    然后我可以通过用文本1,2,3调用它来制作属性字符串...并在它们之间插入换行符(\ n)。

    有什么想法吗?

3 个答案:

答案 0 :(得分:3)

sizeToFit()可帮助您根据文字调整身高。

    var str : NSMutableAttributedString = NSMutableAttributedString(string: "Bla\nBla\nBla\nBla\nBla\nBla\nBla\nBla\nBla\nBla\nBla\nBla\nBla\nBla\nBla\nBla\nBla\nBla")
    str.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(20), range: NSRange(location: 13,length: 3))
    button.setAttributedTitle(str, forState: UIControlState.Normal)
    button.titleLabel!.lineBreakMode = .ByWordWrapping
    button.titleLabel?.textAlignment = .Center
    button.titleLabel?.adjustsFontSizeToFitWidth = true
    button.sizeToFit()
    button.layoutIfNeeded()

使用您的代码:

    var attrString = NSMutableAttributedString()
    addNewLineToAttributedString(attrString, plainString: "Big title", font: UIFont.systemFontOfSize(10), preferredSize: 50, maxWidth: 100)
    addNewLineToAttributedString(attrString, plainString: "Smaller text", font: UIFont.systemFontOfSize(10), preferredSize: 50, maxWidth: 100)
    addNewLineToAttributedString(attrString, plainString: "Smaller smaller text", font: UIFont.systemFontOfSize(10), preferredSize: 50, maxWidth: 100)
    addNewLineToAttributedString(attrString, plainString: "Smaller Smaller Smaller text", font: UIFont.systemFontOfSize(10), preferredSize: 50, maxWidth: 100)
    addNewLineToAttributedString(attrString, plainString: "Smaller Smaller Smaller text", font: UIFont.systemFontOfSize(10), preferredSize: 50, maxWidth: 100)
    addNewLineToAttributedString(attrString, plainString: "Big title", font: UIFont.systemFontOfSize(10), preferredSize: 50, maxWidth: 100)
    addNewLineToAttributedString(attrString, plainString: "Big title", font: UIFont.systemFontOfSize(10), preferredSize: 50, maxWidth: 100)
    addNewLineToAttributedString(attrString, plainString: "Smaller Smaller Smaller text", font: UIFont.systemFontOfSize(10), preferredSize: 50, maxWidth: 100)
    addNewLineToAttributedString(attrString, plainString: "Smaller Smaller Smaller text", font: UIFont.systemFontOfSize(10), preferredSize: 50, maxWidth: 100)
    addNewLineToAttributedString(attrString, plainString: "Smaller Smaller Smaller text", font: UIFont.systemFontOfSize(10), preferredSize: 50, maxWidth: 100)
    addNewLineToAttributedString(attrString, plainString: "Smaller Smaller Smaller text", font: UIFont.systemFontOfSize(10), preferredSize: 50, maxWidth: 100)
    button.setAttributedTitle(attrString, forState: UIControlState.Normal)
    button.titleLabel!.lineBreakMode = .ByWordWrapping
    button.titleLabel?.textAlignment = .Center
    button.titleLabel?.adjustsFontSizeToFitWidth = true
    button.sizeToFit()
    button.layoutIfNeeded()

enter image description here

答案 1 :(得分:2)

你可以自己使用Storyboard来实现它。只需执行此步骤

1:将按钮类型从系统更改为客户

enter image description here

2:将按钮标题从普通更改为归因

3:在textArea中输入文字,并在需要newLine时按 Alt + Enter 。看到图像,它会将我的文字分成3行。

enter image description here

4:现在将换行模式设置为字符换行

enter image description here

注意:如果您没有在故事板中看到不同行中的文字,请将文字对齐自然对齐左对齐。设置为图像中的选择部分

enter image description here

5:现在选择单行并设置字体。你也可以改变textColor。

enter image description here

6:对每一行重复 Step5

这是我的模拟器输出:

enter image description here

答案 2 :(得分:0)

找出解决方案(查看我的博客http://www.hixfield.net/blog/2015/06/multiline-uibutton-with-each-line-resized-to-fit-width/了解更多信息)

class ViewController: UIViewController {

    //the button that we are formatting
    @IBOutlet weak var btn: UIButton!

    //setup our button in the will appear function
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        setupBtn()
    }

    //function that does the magic
    func setupBtn() {
        btn.titleLabel?.lineBreakMode=NSLineBreakMode.ByWordWrapping
        var attrString = NSMutableAttributedString()
        var attr = [NSFontAttributeName : UIFont.systemFontOfSize(10)]
        attrString += (NSMutableAttributedString(string : "Big title", font: UIFont.systemFontOfSize(50), maxWidth: 100)! + "\n" )
        attrString += (NSMutableAttributedString(string : "Smaller text", font: UIFont.systemFontOfSize(50), maxWidth: 100)!  + "\n" )
        attrString += (NSMutableAttributedString(string : "Smaller smaller text", font: UIFont.systemFontOfSize(80), maxWidth: 100)! + "\n" )
        btn.setAttributedTitle(attrString, forState: UIControlState.Normal)
    }
}

//************************

extension NSMutableAttributedString {
    /**Makes an attributes string with the specified (plain) string and font but resizes the font smaller
    to fit the width if required. Can return nil, if there is no way to make it fit*/
    convenience init?(string : String, font : UIFont, maxWidth : CGFloat){
        self.init()
        for var size = font.pointSize ; size>1 ; size-- {
            let attrs = [NSFontAttributeName : font.fontWithSize(size)]
            let attrString = NSAttributedString(string: string, attributes: attrs)
            if attrString.size().width <= maxWidth {
                self.setAttributedString(attrString)
                return
            }
        }
        return nil
    }
}

//************************

public func += (inout left: NSMutableAttributedString, right: NSAttributedString) {
    left.appendAttributedString(right)
}

public func + (left: NSAttributedString, right: NSAttributedString) -> NSAttributedString {
    var result  = NSMutableAttributedString(attributedString: right)
    result.appendAttributedString(right)
    return result
}

public func + (left: NSAttributedString, right: String) -> NSAttributedString {
    var result  = NSMutableAttributedString(attributedString: left)
    result.appendAttributedString(NSAttributedString(string: right))
    return result
}

enter image description here