为什么是textView:shouldInteractWithTextAttachment:inRange:永远不会在UITextView委托上调用?

时间:2013-10-23 09:28:19

标签: ios cocoa uitextview

是否有人使用此委托方法?

我得到了回调
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView

但不是这个。对于

的目的,文档似乎有些含糊不清
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange

根据网络上的文档,这是它的目的:

讨论 如果用户点击或长按文本附件并且其image属性不为nil,则文本视图会调用此方法。此方法的实现是可选的。除了显示与文本内联的文本附件外,您还可以使用此方法触发操作。

这是Xcode 5文档:

询问代理是否指定的文本视图应在给定的文本范围内显示提供的文本附件。 当数据检测器在其文本容器中识别文本附件字符时,文本视图调用此方法。此方法的实现是可选的。除了显示与给定范围内的文本内联的文本附件外,您还可以使用此方法触发替代操作。

编辑:

嗯,好吧我弄清楚了问题。如果我从iOS粘贴图像然后它可以工作,但是如果从OS X粘贴图像则不然。尽管图像看起来在文本视图中显示正确,但似乎在两个平台上使用的实际附件格式并不完全相同。仔细观察,NSTextAttachment类在iOS上与在OS X上看起来并不相同。

如果有人能够对这里的跨平台兼容性有所了解,请做。

此外,如果我在iOS上粘贴图像后保存属性字符串然后检索它并在UITextView中显示它与附件的交互不再可能。当存储图像时,如果内容为零,则图像实际上被放置在内容中。所以也许我将不得不遍历所有附件来检查存储的数据,特别是要弄清楚OS X和iOS平台上的行为差异。

进一步编辑: 如果附件图像不是零,则只调用该方法,尽管显示图像,但实际图像属性实际上可能是零,傻我!无论如何,修复似乎是检查属性字符串中的所有附件并将其image属性设置为某些内容,通常是fileWrapper的内容。默认的NSTextAttachment行为似乎是在归档时将图像存储在fileWrapper中,但是在取消归档时它不会反转。无论如何,我想保留附件中的原始图像,但取决于设备显示原始的适当缩放版本!

2 个答案:

答案 0 :(得分:4)

主要的是文本视图的editable属性必须为NO且selectable属性必须为YES。这是调用此委托方法的唯一情况。如果您收到shouldBeginEditing,那么您的文字字段就是可编辑的,这绝对不是。

答案 1 :(得分:1)

这是我在确保从归档数据恢复UITextView的属性字符串时设置NSTextAttachments图像属性的方法(在这种情况下,只要用户从Core Data存储中选择一条记录)。

我将UITextView设置为textStorage的代理,并在didProcessEditing中查找可能已添加的任何附件,然后检查其图片属性是否已设置。我还在图像上设置缩放系数,以确保图像适合设备缩放。

这样我就不会丢失图像的原始分辨率,如果用户想要更详细地查看它,我提供了从弹出菜单中在图像浏览器窗口中打开它的选项。

希望这有助于其他人。

编辑: 有关NSTextView和UITextView http://ossh.com.au/design-and-technology/software-development/

的详细信息,请在此处查看
- (void)textStorage:(NSTextStorage *)textStorage didProcessEditing:(NSTextStorageEditActions)editedMask range:(NSRange)editedRange changeInLength:(NSInteger)delta {
    //FLOG(@"textStorage:didProcessEditing:range:changeInLength: called");

    [textStorage enumerateAttributesInRange:editedRange options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:
     ^(NSDictionary *attributes, NSRange range, BOOL *stop) {

         // Iterate over each attribute and look for a Font Size
         [attributes enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
             if ([[key description] isEqualToString:NSAttachmentAttributeName]) {
                 NSTextAttachment *attachment = obj;

                 //Reset the image attribute and scale for the device size if necessary
                 [self resetAttachmentImage:attachment];
             }

         }];
     }];
}
- (void)resetAttachmentImage:(NSTextAttachment*)attachment {
    UIImage *image = [attachment image];
    float factor = 2;

    if (image == nil) {
        if (attachment.fileWrapper == nil || !attachment.fileWrapper.isRegularFile) {
            attachment.image = [UIImage imageNamed:@"unknown_attachment.png"];
            return;
        }
        //Usually retrieved from store
        image = [UIImage imageWithData:attachment.fileWrapper.regularFileContents];
    } else {
        // Convert any pasted image
        image = [UIImage imageWithData:UIImagePNGRepresentation(image)];
    }

    float imgWidth = image.size.width;

    // If its wider than the view scale it to fit
    if (imgWidth > _viewWidth) {
        factor = imgWidth / _viewWidth + 0.5;

        attachment.image = [UIImage imageWithData:UIImagePNGRepresentation(image) scale:factor];
    } else {
        attachment.image = [UIImage imageWithData:UIImagePNGRepresentation(image) scale:_scale];
    }

}