当 textView isScrollEnabled = false 时使用 pinchGesture 调整字体和框架
Resize font and frame with pinchGesture when textView isScrollEnabled = false
我使用下面的代码 pinchGesture
来放大和缩小 textView
。
将 pinchGesture
添加到 textView
let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(self.handlePinch))
pinchGesture.delegate = self
view.addGestureRecognizer(pinchGesture)
延迟
@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
}
}
结果
这是我已经应用但仍然无法找到完美解决方案的可能解决方案。
@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)
}
}
结果
使用上述解决方案,我成功地增加了字体大小,但 textView 框架没有更新,因此文本被截断,因为 textView 框架较小。
预期结果
字体会变大,框架也会更新,因此它看起来像简单的放大和缩小但不会模糊。
寻找最好的解决方案来增加框架的字体大小,例如 instagram 和 snapchat 正在做。
谢谢。
如果您尝试在 handlePinch
方法的开头启用滚动并在收缩结束时再次禁用它会怎样?:
@IBAction func handlePinch(recognizer:UIPinchGestureRecognizer) {
if let textView = recognizer.view as? UITextView {
textView.isScrollingEnabled = true
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)
let width = view.frame.size.width
textView.frame.size = textView.sizeThatFits(CGSize(width: width, height: CGFloat.greatestFiniteMagnitude))
textView.isScrollingEnabled = false
}
}
这里是使用 UITextView 和 isScrollEnabled = false
调整字体大小以及捏缩放 in/out 框架的代码
@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
}
}
}
更新了与 UITextView 兼容的答案
这是在 textView
isScrollEnabled = false
时使用 pinchGesture
调整字体和框架的大小。
@IBAction func handlePinch(recognizer:UIPinchGestureRecognizer) {
if let view = recognizer.view {
if view is UITextView {
let textView = view as! UITextView
if textView.font!.pointSize * recognizer.scale < 90 {
let font = UIFont(name: textView.font!.fontName, size: textView.font!.pointSize * recognizer.scale)
textView.font = font
let sizeToFit = textView.sizeThatFits(CGSize(width: UIScreen.main.bounds.size.width,
height:CGFloat.greatestFiniteMagnitude))
textView.bounds.size = CGSize(width: textView.intrinsicContentSize.width,
height: sizeToFit.height)
} else {
let sizeToFit = textView.sizeThatFits(CGSize(width: UIScreen.main.bounds.size.width,
height:CGFloat.greatestFiniteMagnitude))
textView.bounds.size = CGSize(width: textView.intrinsicContentSize.width,
height: sizeToFit.height)
}
textView.setNeedsDisplay()
} else {
view.transform = view.transform.scaledBy(x: recognizer.scale, y: recognizer.scale)
}
recognizer.scale = 1
}
}
我使用下面的代码 pinchGesture
来放大和缩小 textView
。
将 pinchGesture
添加到 textView
let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(self.handlePinch))
pinchGesture.delegate = self
view.addGestureRecognizer(pinchGesture)
延迟
@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
}
}
结果
这是我已经应用但仍然无法找到完美解决方案的可能解决方案。
@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)
}
}
结果
使用上述解决方案,我成功地增加了字体大小,但 textView 框架没有更新,因此文本被截断,因为 textView 框架较小。
预期结果
字体会变大,框架也会更新,因此它看起来像简单的放大和缩小但不会模糊。
寻找最好的解决方案来增加框架的字体大小,例如 instagram 和 snapchat 正在做。
谢谢。
如果您尝试在 handlePinch
方法的开头启用滚动并在收缩结束时再次禁用它会怎样?:
@IBAction func handlePinch(recognizer:UIPinchGestureRecognizer) {
if let textView = recognizer.view as? UITextView {
textView.isScrollingEnabled = true
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)
let width = view.frame.size.width
textView.frame.size = textView.sizeThatFits(CGSize(width: width, height: CGFloat.greatestFiniteMagnitude))
textView.isScrollingEnabled = false
}
}
这里是使用 UITextView 和 isScrollEnabled = false
调整字体大小以及捏缩放 in/out 框架的代码@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
}
}
}
更新了与 UITextView 兼容的答案
这是在 textView
isScrollEnabled = false
时使用 pinchGesture
调整字体和框架的大小。
@IBAction func handlePinch(recognizer:UIPinchGestureRecognizer) {
if let view = recognizer.view {
if view is UITextView {
let textView = view as! UITextView
if textView.font!.pointSize * recognizer.scale < 90 {
let font = UIFont(name: textView.font!.fontName, size: textView.font!.pointSize * recognizer.scale)
textView.font = font
let sizeToFit = textView.sizeThatFits(CGSize(width: UIScreen.main.bounds.size.width,
height:CGFloat.greatestFiniteMagnitude))
textView.bounds.size = CGSize(width: textView.intrinsicContentSize.width,
height: sizeToFit.height)
} else {
let sizeToFit = textView.sizeThatFits(CGSize(width: UIScreen.main.bounds.size.width,
height:CGFloat.greatestFiniteMagnitude))
textView.bounds.size = CGSize(width: textView.intrinsicContentSize.width,
height: sizeToFit.height)
}
textView.setNeedsDisplay()
} else {
view.transform = view.transform.scaledBy(x: recognizer.scale, y: recognizer.scale)
}
recognizer.scale = 1
}
}