HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 自动更改字体大小以适应swift中的按钮大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_772_0@
我试过这个,但它不起作用,文本超出按钮边界.
button.titleLabel!.adjustsFontSizeToFitWidth = true
button.titleLabel!.numberOfLines = 0
button.titleLabel!.minimumScaleFactor = 0.1

当我尝试以下操作时,所有文本都适合,但文本仍然是小字体:

button.titleLabel!.font = UIFont(name: "Heiti TC",size: 9)

如何让字体自动适合按钮的大小?

func nextQuestion() {

    let currentQuestion = mcArray![questionIdx]

    answers = currentQuestion["Answers"] as! [String]
    correctAnswer = currentQuestion["CorrectAnswer"] as? String
    question = currentQuestion["Question"] as? String

    titlesForButtons()
}

func titlesForButtons() {
    for (idx,button) in ANSWERButtons.enumerate() {
        button.titleLabel!.lineBreakmode = .byWordWrapping

        button.titleLabel!.font = UIFont(name: "Heiti TC",size: 5)

        button.titleLabel!.numberOfLines = 0

        button.titleLabel!.minimumScaleFactor = 0.1

        button.titleLabel!.baselineAdjustment = .AlignCenters

        button.titleLabel!.textAlignment  = NSTextAlignment.Center

        button.settitle(answers[idx],forState: .Normal)
        button.enabled = true
        button.BACkgroundColor = UIColor(red: 83.0/255.0,green: 184.0/255.0,blue: 224.0/255.0,alpha: 1.0)
    }

    questionLabel.text = question
    startTimer()
}

这是我到目前为止的代码,它从plist文件获取答案

解决方法

你可以试试这个:

1.根据按钮的当前字体大小定义标题大小

let nstitle = NSString(String:"yourButtontitle")
let font = button.titleLabel?.font
let titleSize = nstitle.sizeWithAttributes([NSFontAttributename:font])

2.检查您的标题是否适合按钮标题标签

if titleSize.width > button.titleLabel?.bounds.width{

    //set the appropriate font size

}else{

    //set the appropriate font size or do nothing
}

大佬总结

以上是大佬教程为你收集整理的ios – 自动更改字体大小以适应swift中的按钮全部内容,希望文章能够帮你解决ios – 自动更改字体大小以适应swift中的按钮所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。