多行UIButton每行自动调整大小

我想使用标准UIButton ,但我希望把对文本UIButton有...

  • ...多行(每行是不同的字符串)和...
  • ...每一行应该有不同的字体和大小......
  • ...自动调整字体大小以适合按钮宽度(不是高度使其更容易一些)
  • 在这里输入图像描述

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

    我不想要的东西:

    我不想开始将UILabels添加到UIButton (例如子视图)或使用IB在场景中放置UILabels,并在其周围绘制UIButton(为什么:我想要标准的UIButton突出显示行为)

    我想要的是:

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

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

        func addToAttributedString(attString : NSMutableAttributedString, plainString : String, font : UIFont, preferredSize : CGFloat, maxWidth : CGFloat)
    

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

    有任何想法吗?


    sizeToFit()将帮助你调整文本的高度。

        var str : NSMutableAttributedString = NSMutableAttributedString(string: "BlanBlanBlanBlanBlanBlanBlanBlanBlanBlanBlanBlanBlanBlanBlanBlanBlanBla")
        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()
    

    在这里输入图像描述


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

    1:将按钮类型从系统更改为自定义

    在这里输入图像描述

    2:将按钮标题从简单更改为归因

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

    在这里输入图像描述

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

    在这里输入图像描述

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

    5:现在选择单行和设置字体。 U还可以更改textColor。

    在这里输入图像描述

    6:为每一行重复步骤5

    这里是我的模拟器输出:

    在这里输入图像描述


    想出了一个解决方案(有关更多信息,请参阅我的博客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
    }
    

    在这里输入图像描述

    链接地址: http://www.djcxy.com/p/28221.html

    上一篇: Multiline UIButton autosize per line

    下一篇: iOS AutoLayout multi