Ios 多行UIButton每行自动调整大小

Ios 多行UIButton每行自动调整大小,ios,swift,uibutton,nsattributedstring,Ios,Swift,Uibutton,Nsattributedstring,我想使用标准的ui按钮,但我想放在ui按钮上的文本有 。。。多行(每行是不同的字符串)和 。。。每行应有不同的字体和大小,以便 。。。自动调整字体大小以适应按钮宽度(而不是使其变宽的高度) (简单一点) 因此,即使我想为每一行设置(首选)字体大小,我希望字体大小自动减小,以便每一行都能很好地适应ui按钮(=与ui标签自动收缩/最小字体比例相同的行为) 我不想要的是: 我不想开始将UILabels添加到UIButton(例如,作为子视图)或使用IB将UILabels放置在场景上,然后在其周围绘制

我想使用标准的
ui按钮
,但我想放在
ui按钮
上的文本有

  • 。。。多行(每行是不同的字符串)和
  • 。。。每行应有不同的字体和大小,以便
  • 。。。自动调整字体大小以适应按钮宽度(而不是使其变宽的高度) (简单一点)
  • 因此,即使我想为每一行设置(首选)字体大小,我希望字体大小自动减小,以便每一行都能很好地适应
    ui按钮
    (=与
    ui标签
    自动收缩/最小字体比例相同的行为)

    我不想要的是:

    我不想开始将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: "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()
    

    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()
    

    您可以使用故事板自己实现它。只需执行以下步骤

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

    2:将按钮标题从普通更改为属性

    3:在文本区域中输入文本,需要换行时按Alt+Enter。如图所示,它将把我的文本分成3行

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

    注意:如果在情节提要中看不到不同行中的文本,请将文本对齐方式从“自然对齐”更改为“向左对齐”。设置为图像中的选择部分

    5:现在选择单个行并设置字体。您还可以更改文本颜色

    6:对每行重复步骤5

    这是我的模拟器输出:


    您可以使用故事板自己实现它。只需执行以下步骤

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

    2:将按钮标题从普通更改为属性

    3:在文本区域中输入文本,需要换行时按Alt+Enter。如图所示,它将把我的文本分成3行

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

    注意:如果在情节提要中看不到不同行中的文本,请将文本对齐方式从“自然对齐”更改为“向左对齐”。设置为图像中的选择部分

    5:现在选择单个行并设置字体。您还可以更改文本颜色

    6:对每行重复步骤5

    这是我的模拟器输出:

    想出了一个解决方案(查看我的博客了解更多信息)

    类ViewController:UIViewController{
    //我们正在格式化的按钮
    @IBOutlet弱var btn:UIButton!
    //在“将显示”功能中设置我们的按钮
    覆盖功能视图将出现(动画:Bool){
    超级。视图将显示(动画)
    setupBtn()
    }
    //有魔力的函数
    func setupBtn(){
    btn.titleLabel?.lineBreakMode=NSLineBreakMode.ByWordWrapping
    var attrString=nsmutableAttributeString()
    var attr=[NSFontAttributeName:UIFont.systemFontOfSize(10)]
    attrString+=(NSMutableAttributeString(字符串:“大标题”,字体:UIFont.systemFontOfSize(50),最大宽度:100)!+“\n”)
    attrString+=(NSMutableAttributeString(字符串:“小文本”,字体:UIFont.systemFontOfSize(50),最大宽度:100)!+“\n”)
    attrString+=(NSMutableAttributeString(字符串:“较小的文本”,字体:UIFont.systemFontOfSize(80),最大宽度:100)!+“\n”)
    btn.setAttributeTitle(属性字符串,用于状态:uicontrol状态.Normal)
    }
    }
    //************************
    扩展NSMutableAttributeString{
    /**使用指定的(普通)字符串和字体生成属性字符串,但将字体大小调整为更小
    如果需要适合宽度。如果无法使其适合,可以返回nil*/
    便利初始化?(字符串:字符串,字体:UIFont,最大宽度:CGFloat){
    self.init()
    对于var size=font.pointSize;size>1;size--{
    让attrs=[NSFontAttributeName:font.fontWithSize(大小)]
    让attrString=NSAttributedString(string:string,attributes:attrs)
    如果attrString.size().width NSAttributeString{
    var结果=NSMutableAttributedString(attributedString:右)
    result.AppendAttributeString(右)
    返回结果
    }
    public func+(左:NSAttributedString,右:String)->NSAttributedString{
    var结果=NSMutableAttributedString(attributedString:左)
    结果.AppendAttributeString(NSAttributeString(字符串:右))
    返回结果
    }
    

    想出了一个解决方案(查看我的博客了解更多信息)

    类ViewController:UIViewController{
    //我们正在格式化的按钮
    @IBOutlet弱var btn:UIButton!
    //在“将显示”功能中设置我们的按钮
    覆盖功能视图将出现(动画:Bool){
    超级。视图将显示(动画)
    setupBtn()
    }
    //有魔力的函数
    func setupBtn(){
    btn.titleLabel?.lineBreakMode=NSLineBreakMode.ByWordWrapping
    var attrString=nsmutableAttributeString()