我如何显示其余字符已稍微超出UITextView?

问题描述 投票:0回答:1

我有一个UITextView,它根据用户输入的文本(紫色框)而改变大小,该文本位于另一个UIView(红色框)内。

但是当使用这样的手写样式字体时,结束字符有时会在边缘被截断:enter image description here

我尝试过使用text1.clipsToBounds = false,但是没有显示字符的边缘。有没有办法显示完整字符而不更改文本视图的宽度?

这也是我用来设置文本视图的代码:

    let text1 = UITextView()
    text1.text = ""
    text1.font = UIFont(name: "Gotcha", size: 27)
    text1.frame = CGRect(x: 10, y: 5, width: 70, height: 50)
    text1.isScrollEnabled = false
    text1.delegate = self
    text1.textAlignment = .center
    text1.isEditable = false
    text1.isSelectable = false
    holdingView.addSubview(text1)

然后该框架将通过此功能进行更新,并且每当文本更改时:

 func adjustTextViewSize(_ textView: UITextView) {

    let maxWidth = 300

    let newSize = textView.sizeThatFits(CGSize(width: maxWidth, height: CGFloat.greatestFiniteMagnitude))

    textView.frame = CGRect(x: (textView.frame.minX), y: (textView.frame.minY), width: newSize.width, height: newSize.height)


}

谢谢!

更新:

我为手写字体添加了30px来解决这个问题:

newSize.width
swift uitextview
1个回答
0
投票

根据字符串长度调用此函数以获取高度

if fontFile?.isHandwritten == true {
   currentView.widthConstraint?.constant = newSize.width + 30
   currentTextHoldingView.widthConstraint?.constant = newSize.width + 30
}
© www.soinside.com 2019 - 2024. All rights reserved.