如何在键入文本时自动生成文本字体?

时间:2023-02-13 03:35:30

I am using a UITextView and I wonder if it's possible to auto-resize the text depending to the text typing.I mean when the user taps a long text the font size of that text reduces dynamically.

我使用的是UITextView,我想知道是否可以根据文本类型自动调整文本大小。我的意思是当用户点击一个长文本时,文本的字体大小会动态减少。

1 个解决方案

#1


1  

There's a delegate method in UITextViewDelegate that tells you when the text has changed. You can use the textView.text.length property and perform some calculations on it, say, 14 - textView.text.length*3/100 if you want the text to be 14pt at start, and 11pt when it has 100 characters in it. Then you set your textfield's font right away.

在UITextViewDelegate中有一个委托方法,它告诉您文本何时发生了更改。你可以使用textView.text。长度属性并在它上面执行一些计算,比如,14 - textView.text。长度*3/100如果你想让文本在开始时是14pt,而在它有100个字符的时候是11pt。然后立即设置textfield的字体。

#1


1  

There's a delegate method in UITextViewDelegate that tells you when the text has changed. You can use the textView.text.length property and perform some calculations on it, say, 14 - textView.text.length*3/100 if you want the text to be 14pt at start, and 11pt when it has 100 characters in it. Then you set your textfield's font right away.

在UITextViewDelegate中有一个委托方法,它告诉您文本何时发生了更改。你可以使用textView.text。长度属性并在它上面执行一些计算,比如,14 - textView.text。长度*3/100如果你想让文本在开始时是14pt,而在它有100个字符的时候是11pt。然后立即设置textfield的字体。