NSTextAttachment 光标对于 UITextView 来说太大了

问题描述

You can see a screenshot of the issue here

我在 UITextView 内部添加一个 NSTextAttachment,但是,在我插入图像后,随机对于某些照片,编辑时光标变得非常大。我尝试了以下修复但没有运气:

  1. 使用 NSKernAttributeName 设置字距
  2. 使用 NSMutableParagraphStyle 设置行高
  3. 为 NSTextAttachment 设置输入属性
    NSAttributedString *attachmentimage = [NSAttributedString attributedStringWithAttachment:attachment];
    [attachmentimage boundingRectWithSize:image.size options:NsstringDrawingUsesLineFragmentOrigin | NsstringDrawingUsesFontLeading context:nil];
    NSAttributedString *spacer = [[NSAttributedString alloc]initWithString:@"\n\n"];
    NSAttributedString *prefixAttachmentString = spacer;
    if([self.text isEqualToString:@""]){prefixAttachmentString = [[NSAttributedString alloc]initWithString:@""];} //no spacer if text empty
    NSMutableAttributedString *attachmentString = [[NSMutableAttributedString alloc]initWithAttributedString:prefixAttachmentString];
    [attachmentString appendAttributedString:attachmentimage];
    NSKernAttributeName
    NSMutableParagraphStyle *paragraphSytle = [[NSMutableParagraphStyle alloc]init];
    NSLog(@"image attachment: %@",attachmentimage);
    NSLog(@"image cursor size: %f",image.size.height);
    paragraphSytle.paragraphSpacingBefore = 0.0;
    paragraphSytle.linespacing = 10;
    [attachmentString addAttributes:@{@"attachmentName" : name,NSParagraphStyleAttributeName : paragraphSytle}
                              range:NSMakeRange((int)prefixAttachmentString.length,1)];
    [attachmentString addAttributes:self.typingAttr range:NSMakeRange((int)prefixAttachmentString.length,1)];

    [attachmentString appendAttributedString:spacer];
    [self.textStorage insertAttributedString:attachmentString
                                     atIndex:(int)(self.selectedRange.location)];
    
    [attachmentString addAttributes:self.typingAttr range:NSMakeRange(0,attachmentString.length)];
    
    self.selectedRange = NSMakeRange(self.selectedRange.location+(int)attachmentString.length,self.selectedRange.length);
    
    [self.textStorage endEditing];```


解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...