gpt4 book ai didi

ios - 当 textView isScrollEnabled = false 时使用 pinchGesture 调整字体和框架

转载 作者:搜寻专家 更新时间:2023-10-30 23:07:36 27 4
gpt4 key购买 nike

我使用 pinchGesture 使用以下代码放大和缩小 textView

添加pinchGesturetextView

let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(self.handlePinch))
pinchGesture.delegate = self
view.addGestureRecognizer(pinchGesture)

委托(delegate)

@IBAction func handlePinch(recognizer:UIPinchGestureRecognizer) {
if let view = recognizer.view as? UITextView {
view.transform = view.transform.scaledBy(x: recognizer.scale, y: recognizer.scale)
recognizer.scale = 1
}
}

结果

enter image description here enter image description here

这是我已经应用但仍然无法找到完美解决方案的可能解决方案。

@IBAction func handlePinch(recognizer:UIPinchGestureRecognizer) {
if let textView = recognizer.view as? UITextView {
let font = textView.font!
var pointSize = font.pointSize
let fontName = font.fontName
pointSize = ((recognizer.velocity > 0) ? 1 : -1) * 1 + pointSize;
if (pointSize < 13) {
pointSize = 13
}
if (pointSize > 100) {
pointSize = 100
}
textView.font = UIFont(name: fontName, size: pointSize)
}
}

结果

enter image description here

使用上述解决方案,我成功地增加了字体大小,但 textView 框架没有更新,因此文本被截断,因为 textView 框架较小。

预期结果

字体会变大,框架也会更新,所以它看起来像简单的放大和缩小,但不会模糊。

寻找最好的解决方案来增加框架的字体大小,例如 instagramsnapchat 正在做。

谢谢。

最佳答案

这里是使用 UITextView 和 isScrollEnabled = false 来调整字体大小以及缩放框架的代码

@objc func pinchRecoginze(_ pinchGesture: UIPinchGestureRecognizer) {
guard recognizer.view != nil, let view = recognizer.view else {return}

if view is UITextView {
let textView = view as! UITextView
if recognizer.state == .began {
let font = textView.font
let pointSize = font!.pointSize
recognizer.scale = pointSize * 0.1
}
if 1 <= recognizer.scale && recognizer.scale <= 10 {
textView.font = UIFont(name: textView.font!.fontName, size: recognizer.scale * 10)
let textViewSiSize = textView.intrinsicContentSize
textView.bounds.size = textViewSiSize
}
}
}

Smaller font size

Pinch zoom to larger font size

更新了与 UITextView 兼容的答案

关于ios - 当 textView isScrollEnabled = false 时使用 pinchGesture 调整字体和框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54159471/

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