委託方法做工作,它不被調用,但只有當附着在圖像屬性,如果編輯圖像= NO!所以如果你從別的地方將圖像粘貼到了attributesString中,那麼數據似乎最終被存儲在fileWrapper中,並且下一次將attributesString放回到textView中時,image屬性爲零,而佈局管理器或任何獲取圖像來自fileWrapper。
在文檔中的某處它確實提到NSTextAttachment中沒有用於持久化圖像屬性的方法。
爲了測試這個嘗試從照片應用程序複製照片並將其粘貼到您的textView,現在如果你按住你的手指,你應該看到默認菜單彈出。現在,如果你保存這個豐富的文本,說一個核心數據實體,然後檢索它的圖像屬性將爲零,但圖像數據將在attachment.fileWrapper.regularFileContents
它的痛苦,我很想知道工程師的意圖。所以你看起來有兩個選擇。
- 把你的字符串回的TextView你發現之前創建自己的自定義NSTextAttachment,包括用於歸檔圖像和其他設置方法(請告訴我怎麼過當你明白這出一個)
每次所有附件並重新創建圖像屬性,如下所示:
attachment.image = [UIImage imageWithData:attachment.fileWrapper.regularFileContents];
記住這樣做的副作用是使fileWrapper無效。我想調整圖像大小,但也要保留原始圖像,以免丟失全部分辨率。我認爲這樣做的唯一方法可能是將NSTextAttachment轉換爲子類。
編輯:
我想出瞭如何創建自定義NSTextAttachments - 這裏是一個鏈接爲那些有興趣http://ossh.com.au/design-and-technology/software-development/implementing-rich-text-with-images-on-os-x-and-ios/
編輯2:自定義菜單時,在編輯模式下看到下面的Apple文檔中,問題是'touchEnded'似乎永遠不會被調用,所以你可能不得不嘗試使用touchesBegan。小心你不要干預默認的編輯行爲。
https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/AddingCustomEditMenuItems/AddingCustomEditMenuItems.html
注意,在下面的代碼,你需要後// selection management
評論添加代碼,以確定哪些字符被感動了,檢查它是否是特殊文本附件的性格和 然後修改編輯菜單或採取一些其他行動。
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *theTouch = [touches anyObject];
if ([theTouch tapCount] == 2 && [self becomeFirstResponder]) {
// selection management code goes here...
// bring up edit menu.
UIMenuController *theMenu = [UIMenuController sharedMenuController];
CGRect selectionRect = CGRectMake (currentSelection.x, currentSelection.y, SIDE, SIDE);
[theMenu setTargetRect:selectionRect inView:self];
[theMenu setMenuVisible:YES animated:YES];
}
}
或者,您可以通過添加菜單項然後修改canPerformAction方法來添加自定義菜單。
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
LOG(@"canPerformAction: called");
if (action == @selector(viewImage)) {
// Check the selected character is the special text attachment character
return YES;
}
return NO;
}
這是一些附加代碼,但它有點挑剔。第二種方法只是在檢測到附件時禁用默認編輯菜單。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
FLOG(@"touchesBegan:withEvent: called");
if (self.selectedRange.location != NSNotFound) {
FLOG(@" selected location is %d", self.selectedRange.location);
int ch;
if (self.selectedRange.location >= self.textStorage.length) {
// Get the character at the location
ch = [[[self textStorage] string] characterAtIndex:self.selectedRange.location-1];
} else {
// Get the character at the location
ch = [[[self textStorage] string] characterAtIndex:self.selectedRange.location];
}
if (ch == NSAttachmentCharacter) {
FLOG(@" selected character is %d, a TextAttachment", ch);
} else {
FLOG(@" selected character is %d", ch);
}
}
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
FLOG(@"canPerformAction: called");
FLOG(@" selected location is %d", self.selectedRange.location);
FLOG(@" TextAttachment character is %d", NSAttachmentCharacter);
if (self.selectedRange.location != NSNotFound) {
int ch;
if (self.selectedRange.location >= self.textStorage.length) {
// Get the character at the location
ch = [[[self textStorage] string] characterAtIndex:self.selectedRange.location-1];
} else {
// Get the character at the location
ch = [[[self textStorage] string] characterAtIndex:self.selectedRange.location];
}
if (ch == NSAttachmentCharacter) {
FLOG(@" selected character is %d, a TextAttachment", ch);
return NO;
} else {
FLOG(@" selected character is %d", ch);
}
// Check for an attachment
NSTextAttachment *attachment = [[self textStorage] attribute:NSAttachmentAttributeName atIndex:self.selectedRange.location effectiveRange:NULL];
if (attachment) {
FLOG(@" attachment attribute retrieved at location %d", self.selectedRange.location);
return NO;
}
else
FLOG(@" no attachment at location %d", self.selectedRange.location);
}
return [super canPerformAction:action withSender:sender];
}
嗨米歇爾,我現在有完全相同的問題,並希望能夠檢測到'NSTextAttachment'即使'textView.editable = YES'上的點擊和觸摸 - 您是否找到了解決方案? – Jon