NSTextAttachment 光标对于 UITextView 来说太大了

时间:2021-04-14 20:34:49

标签: ios objective-c iphone nstextattachment

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];```


0 个答案:

没有答案
相关问题