Iva*_*ino 5 drag-and-drop uitextview ios swift
我正在我的应用程序中实现拖放。
将应用拖放(主要是拖放)的主屏幕之一位于UITextView.
我UITextView使用以下代码为此添加了一个放置交互:
let dropInteraction = UIDropInteraction(delegate: self)
inputTextView.addInteraction(dropInteraction)
Run Code Online (Sandbox Code Playgroud)
通过这样做,我现在接受其中的下降。
为了只接受图像和文本作为 drop,我实现了以下委托方法:
func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool {
return session.hasItemsConforming(toTypeIdentifiers: [kUTTypeImage as String, kUTTypeText as String]) && session.items.count == 1
}
Run Code Online (Sandbox Code Playgroud)
根据需要,我还实现了以下必需的委托方法来返回一个UIDropProposal:
func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate session: UIDropSession) -> UIDropProposal {
let dropProposal: UIDropProposal = UIDropProposal(operation: .copy)
dropProposal.isPrecise = true
return dropProposal
}
Run Code Online (Sandbox Code Playgroud)
然后在 performDrop 委托方法中,我处理丢弃的内容并将其附加到 UITextView
func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
// Access the dropped object types: UIImage, NSString
session.loadObjects(ofClass: UIImage.self) { (imageItems) in
let images = imageItems as! [UIImage]
guard var image = images.first else { return }
// scale the image
image = UIImage(cgImage: image.cgImage!, scale: 4, orientation: .up)
let currentText: NSMutableAttributedString = NSMutableAttributedString(attributedString: self.inputTextView.attributedText)
let textAttachment = NSTextAttachment()
textAttachment.image = image
let imageText = NSAttributedString(attachment: textAttachment).mutableCopy() as! NSMutableAttributedString
let imageStyle = NSMutableParagraphStyle()
imageStyle.alignment = .center
imageText.addAttribute(NSAttributedStringKey.paragraphStyle, value: imageStyle, range: NSMakeRange(0, imageText.length))
// Prepares the string to append with line breaks and the image centered
let attributedStringToAppend: NSMutableAttributedString = NSMutableAttributedString(attributedString: NSAttributedString(string: "\n\n"))
attributedStringToAppend.append(imageText)
attributedStringToAppend.append(NSAttributedString(string: "\n\n"))
// Applies text attributes to the NSMutableAttrString as the default text input attributes
let range: NSRange = NSRange.init(location: 0, length: attributedStringToAppend.length)
let style = NSMutableParagraphStyle()
style.lineSpacing = 4
style.alignment = .center
let font = Font(.installed(.OpenSansRegular), size: .custom(18)).instance
let attr: [NSAttributedStringKey : Any] = [NSAttributedStringKey(rawValue: NSAttributedStringKey.paragraphStyle.rawValue): style, NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue): font, NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor.rgb(red: 52, green: 52, blue: 52)]
attributedStringToAppend.addAttributes(attr, range: range)
let location = self.inputTextView.selectedRange.location
currentText.insert(attributedStringToAppend, at: location)
self.inputTextView.attributedText = currentText
}
session.loadObjects(ofClass: NSString.self) { (stringItems) in
let strings = stringItems as! [NSString]
guard let text = strings.first else { return }
self.inputTextView.insertText(text as String)
}
}
Run Code Online (Sandbox Code Playgroud)
通过执行所有这些操作,图像和文本都会按预期放入所需的字段中,但我在这里遇到了一个问题。
在New Mail Composer邮件应用程序中,当您将项目或图像拖动到它时,它会显示一个跟踪光标,以便您知道放置内容的去向,但在我的示例中我不能这样做。
滴到现有文本的末尾。
如果我想拖放到特定位置,我必须先将光标放在所需位置然后执行拖放,因此在performDrop您看到的函数中,我通过selectedRange.
我想在放置动画发生时看到光标流动,就像邮件应用程序或笔记应用程序一样,您可以在其中选择放置对象的位置。
有没有人有关于如何实现这一目标的提示?
提前致谢。
selectedRange您可以通过将设为NSRange有效位置且长度为 的 来在 textView 或 textField 中移动光标0。
我不会用完成的代码来破坏您,而是一步步解释如何自己解决问题:
计算超级视图中触摸的当前位置,例如您可以使用touchesMoved:,然后将触摸的位置转换为 textView 的坐标系
text通过应用 textView 的文本属性(字体、大小等)来计算 textView 的边界矩形,如下所示:
[textView.text boundingRectWithSize:textView.bounds.size options:NSStringDrawingUsesLineFragmentOrigin attributes:textAttributesDict context:nil]
将触摸位置与文本的边界矩形进行匹配,并在文本中找到所需的位置
设置textView.selectedRangeNSMakeRange(calculatedLoc, 0)
| 归档时间: |
|
| 查看次数: |
1123 次 |
| 最近记录: |