问题描述
我用 UIScrollView、UIView 和 UITextView 创建了一个小项目(层次结构也是一样的。)。 UIView 显示为红色,UITextView 显示为蓝色。然后我将大小为 14 的 UIFont 添加到 UITextView。
由于我想为此启用放大/缩小功能,因此我已将 uiscrollviewdelegate 添加到此控制器并覆盖 viewForZooming 方法。 然后放大/缩小,除了一件事之外,一切都很好。
当我放大屏幕时,我可以看到 UITextView 中的字体模糊了。我想在放大完成后根据缩放比例保持字体质量。所以我覆盖了 scrollViewDidEndZooming 并调整了它不起作用的 textView 框架大小。 在谷歌搜索后,我找到了一些答案,但我尝试了但找不到解决方案,而且大多数都是非常旧的答案。
所以我恳请你们帮助我解决这个问题,我非常感谢你们的反馈和帮助。我附上了我的源代码和屏幕截图供您参考。
代码:
class ViewController: UIViewController {
var scrollView: UIScrollView!
var mainView: UIView!
var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView = UIScrollView(frame: .zero)
scrollView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(scrollView)
NSLayoutConstraint.activate([
scrollView.widthAnchor.constraint(equalTo: self.view.widthAnchor),scrollView.heightAnchor.constraint(equalTo: self.view.heightAnchor),])
mainView = UIView(frame: .zero)
mainView.translatesAutoresizingMaskIntoConstraints = false
mainView.backgroundColor = UIColor.red
scrollView.addSubview(mainView)
NSLayoutConstraint.activate([
mainView.widthAnchor.constraint(equalToConstant: 600),mainView.heightAnchor.constraint(equalToConstant: 400),mainView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),mainView.centerYAnchor.constraint(equalTo: scrollView.centerYAnchor)
])
textView = UITextView(frame: .zero)
textView.translatesAutoresizingMaskIntoConstraints = false
textView.backgroundColor = UIColor.blue
textView.font = UIFont(name: "Thonburi",size: 14)
textView.text = "Hello World"
textView.textAlignment = .center
mainView.addSubview(textView)
NSLayoutConstraint.activate([
textView.widthAnchor.constraint(equalToConstant: 200),textView.heightAnchor.constraint(equalToConstant: 40),textView.centerXAnchor.constraint(equalTo: mainView.centerXAnchor),textView.centerYAnchor.constraint(equalTo: mainView.centerYAnchor),])
scrollView.pinchGestureRecognizer?.isEnabled = true
scrollView.maximumZoomScale = 12.0
scrollView.minimumZoomScale = 0.2
scrollView.delegate = self
}
}
extension ViewController: uiscrollviewdelegate {
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
if let viewForZoom = scrollView.subviews.filter({$0.tag == 1}).first {
return viewForZoom
} else {
return scrollView.subviews.first
}
}
func scrollViewDidEndZooming(_ scrollView: UIScrollView,with view: UIView?,atScale scale: CGFloat) {
self.textView.frame = CGRect(origin: textView.frame.origin,size: CGSize(width: textView.frame.width*scale,height: textView.frame.height*scale))
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)