ibireme / YYKit

A collection of iOS components.
MIT License
13.99k stars 3.69k forks source link

向YYTextView中添加图片,UIMenuController选项不显示 #166

Closed cmallocfree closed 8 years ago

cmallocfree commented 8 years ago

代码如下(self.contentTextView是YYTextView类型~):

NSRange selectedRange = self.contentTextView.selectedRange;
NSMutableAttributedString *text = [self.contentTextView.attributedText mutableCopy];  
UIImage *image = [UIImage imageNamed:@"img.png"];    
image = [UIImage imageWithCGImage:image.CGImage scale:1.0 orientation:UIImageOrientationUp]; 
YYTextAttachment *attachment = [[YYTextAttachment alloc] init];
NSMutableAttributedString *attachText = [NSMutableAttributedString attachmentStringWithContent:image contentMode:UIViewContentModeCenter attachmentSize:image.size alignToFont:[UIFont systemFontOfSize: 17.0f] alignment:YYTextVerticalAlignmentBottom];
[text replaceCharactersInRange:selectedRange withAttributedString:attachText];
self.contentTextView.attributedText = text;

之后在self.contentTextView上长按,就无法出现UIMenuController的粘贴选项了。。。我跟了一下代码,_showMenu方法中的[menu setMenuVisible:YES animated:YES];还是执行的,请教一下是怎么回事~~

ibireme commented 8 years ago

抱歉我复现不了。。如果你还能复现,可以试试跟进到 [menu setTargetRect: 这一行看看 rect 是否在屏幕范围内,如果 rect 有问题我再查一下这个地方。如果这个也没问题,那我就怀疑是系统的原因了(有时在用系统的 textView 或 webView 时,偶尔会遇到弹不出菜单)。

ibireme commented 8 years ago

Fixed: https://github.com/ibireme/YYKit/commit/3be148151c1dc741a6aa53a31d38a93d19fd4aec