在UITextView中拖放时显示文本光标 [英] Show text cursor on drag and drop in UITextView

查看:101
本文介绍了在UITextView中拖放时显示文本光标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的应用程序中实现拖放。

I'm implementing drag and drop in my app.

将要应用拖放(主要是拖放)的主屏幕之一是 UITextView

One of the main screens where drag and drop will be applied (mostly drop) is in a UITextView.

我已向此 UITextView 使用以下代码:

I've added a drop interaction to this UITextView with the following code:

    let dropInteraction = UIDropInteraction(delegate: self)     
    inputTextView.addInteraction(dropInteraction)

这样做,我现在接受其中的水滴。

By doing so I now accept drops in it.

为了只接受图像和文本作为拖放,我实现了以下委托方法:

In order to accept only images and text as drop I've implemented the following delegate method:

func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool {
    return session.hasItemsConforming(toTypeIdentifiers: [kUTTypeImage as String, kUTTypeText as String]) && session.items.count == 1
}

我也根据需要实施了以下必需的委托方法以返回 UIDropProposal

As required I've implemented as well the following required delegate method to return a UIDropProposal :

func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate session: UIDropSession) -> UIDropProposal {
    let dropProposal: UIDropProposal = UIDropProposal(operation: .copy)
    dropProposal.isPrecise = true
    return dropProposal
}

然后在performDrop委托方法中,我处理放置的内容并将其附加到 UITextView

Then in the performDrop delegate method I handle the dropped content and attach it to the 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)
    }                   
}

通过将所有这两个图像和文本都按预期方式拖放到所需字段中,但是我在这里遇到了一个问题。

By doing all of this both images and text are dropped as expected into the desired field but I have one problem here.

在邮件应用程序的 New Mail Composer 中,当您将项目或图像拖到其中时,它会显示一个跟踪光标这样您就知道放置内容的去向,但是在我的示例中我无法做到。

In the New Mail Composer in the mail app, when you're dragging either items or images to it, it shows a tracking cursor so you know where the drop content goes to, but I cannot do this in my example.

放置位置将到达现有文本的结尾。

The drops goes to the end of the existing text.

如果要拖放到特定位置,则必须先将光标放在所需位置,然后再执行拖放操作,因此请在 performDrop 函数,如您所见,我通过 selectedRange 检索了光标位置。

If I want to drop to a specific location, I have to before place the cursor in the desired position and then perform the drop, so in the performDrop function as you see, I retrieve the cursor location by the selectedRange.

我想看看拖放动画发生时的光标流,就像邮件应用程序或非es应用程序,您可以在其中选择放置对象的位置。

I want to see the cursor flow as the drop animation is occurring, just like the mail app or the notes app, where you can choose where the drop object goes to.

是否有人暗示如何实现此目的?

Does anyone have a hint on how to achieve this?

谢谢。

推荐答案

您可以通过设置<$ c来将光标移到textView或textField中。 $ c> selectedRange NSRange ,其有效位置和长度为 0

You can move the cursor in a textView or textField by setting the selectedRange to a NSRange with a valid location and a length of 0.

我不会用完成的代码破坏您,而是逐步说明如何自己解决问题:

I won't spoil you with finished code, but rather give a step by step explanation on how to solve the problem yourself:


  1. 计算超级视图中触摸的当前位置,例如,您可以使用 touchesMoved:,然后进行转换触摸到textView坐标系统中的位置

  1. Compute the current location of the touch in the superview, for instance you could use touchesMoved:, then convert the location of the touch into the textView's coordinate system

计算textView的文本的边界矩形像这样应用textView的文本属性(字体,大小等):

Calculate the bounding rect of your textView's text by applying the text properties of your textView (font, size etc.) like so:

[textView.text boundingRectWithSize:textView.bounds.size选项:NSString DrawingUsesLineFragmentOrigin属性:textAttributesDict上下文:无]

将触摸位置与文本的边界矩形匹配,并找到所需的位置在您的文本中

Match the location of touch against the bounding rect of the text and find the desired location in your text

textView.selectedRange 设置为 NSMakeRange(calculatedLoc,0 )

这篇关于在UITextView中拖放时显示文本光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆