删除文本时如何将UITextView还原为大小和字体大小?

时间:2019-03-20 07:58:21

标签: ios swift xcode uitextview

此代码使我可以减小用户键入时UITextView中文本的大小和UITextView的大小,直到达到一定宽度为止。

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
textView.updateTextFont()

}

func updateTextFont() {
        if (self.text.isEmpty || self.bounds.size.equalTo(CGSize.zero)) {
            return;
        }

        let textViewSize = self.frame.size;
        let fixedWidth = textViewSize.width;
        let expectSize = self.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat(MAXFLOAT)))


        var expectFont = self.font
        if (expectSize.height > textViewSize.height) {

            while (self.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat(MAXFLOAT))).height > textViewSize.height) {
                expectFont = self.font!.withSize(self.font!.pointSize - 1)
                self.font = expectFont
            }
        }
        else {
            while (self.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat(MAXFLOAT))).height < textViewSize.height) {
                expectFont = self.font
                self.font = self.font!.withSize(self.font!.pointSize + 1)
            }
            self.font = expectFont
        }
    }
}

但是当我开始删除时,它不会增加Font的大小和UITextView的大小。我该怎么办?

0 个答案:

没有答案