gpt4 book ai didi

ios - 如果文本是单个单词,UITextField adjustmentFontSizeToFitWidth 不起作用

转载 作者:行者123 更新时间:2023-11-29 05:19:55 28 4
gpt4 key购买 nike

example example2

我有一个 UITextField,它位于具有约束的特定 View 中。如果文本不是一个单词,它就可以完美地工作。但正如您在上图中看到的,如果文本只有一个单词,它不会调整字体大小以适应。我该如何解决?这是我的代码:

class ViewController: UIViewController {

@IBOutlet weak var myView: UIView!

lazy var textInput : UITextField = {
let textInput = UITextField()
textInput.textAlignment = .center
textInput.translatesAutoresizingMaskIntoConstraints = false
textInput.minimumFontSize = 10;
textInput.adjustsFontSizeToFitWidth = true;
textInput.backgroundColor = .yellow
myView.addSubview(textInput)
var lConst = NSLayoutConstraint(item: textInput, attribute: .leading, relatedBy: .equal, toItem: myView, attribute: .leading, multiplier: 1, constant: 0)
var toConst = NSLayoutConstraint(item: textInput, attribute: .top, relatedBy: .equal, toItem: myView, attribute: .top, multiplier: 1, constant: 0)
var trConst = NSLayoutConstraint(item: textInput, attribute: .trailing, relatedBy: .equal, toItem: myView, attribute: .trailing, multiplier: 1, constant: 0)
var bConst = NSLayoutConstraint(item: textInput, attribute: .bottom, relatedBy: .equal, toItem: myView, attribute: .bottom, multiplier: 1, constant: 0)
NSLayoutConstraint.activate([lConst,toConst,trConst,bConst])

return textInput
} ()


override func viewDidLoad() {
super.viewDidLoad()
_ = textInput
textInput.font = UIFont.boldSystemFont(ofSize: 200)
// Do any additional setup after loading the view.
}
}

最佳答案

如果您的意思是“字符”而不是“单词”(查看您的图像),我认为您的问题是,默认情况下第一个字符是大写的。如果您想以小写字符开始文本,则必须设置:

textInput.autocapitalizationType = .none

编辑:抱歉,我只是误解了您的图像,并且没有看到您的信件在顶部和底部被剪切。

我用稍微不同的布局语法编写了您的代码,即使 View 小于您的字体大小,它也能正常工作。

class ViewController: UIViewController {

lazy var myView:UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()

lazy var textInput : UITextField = {
let textInput = UITextField()
textInput.textAlignment = .center
textInput.translatesAutoresizingMaskIntoConstraints = false
textInput.minimumFontSize = 10;
textInput.adjustsFontSizeToFitWidth = true;
textInput.backgroundColor = .yellow
return textInput
} ()


override func viewDidLoad() {
super.viewDidLoad()
setupLayout()
// Do any additional setup after loading the view.
}

func setupLayout(){
view.addSubview(myView)
NSLayoutConstraint.activate([
myView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
myView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
myView.widthAnchor.constraint(equalToConstant: 180),
myView.heightAnchor.constraint(equalToConstant: 180)
])
textInput.font = UIFont.boldSystemFont(ofSize: 200)
myView.addSubview(textInput)
NSLayoutConstraint.activate([
textInput.topAnchor.constraint(equalTo:myView.topAnchor),
textInput.trailingAnchor.constraint(equalTo: myView.trailingAnchor),
textInput.bottomAnchor.constraint(equalTo: myView.bottomAnchor),
textInput.leadingAnchor.constraint(equalTo: myView.leadingAnchor)
])
}
}

关于ios - 如果文本是单个单词,UITextField adjustmentFontSizeToFitWidth 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58772476/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com