Texture icon indicating copy to clipboard operation
Texture copied to clipboard

textAttachment unavailable in ASTextNode2

Open WIZOooo opened this issue 6 years ago • 3 comments

Hello Guys, I found that the NSTextAttachment is not visible as I set the ASTextNode2.truncationMode with NSLineBreakByTruncatingTail. The ASTextNode2.attributedText property description says that For inline image attachments, add an attribute of key NSAttachmentAttributeName, with a value of an NSTextAttachment. I added NSAttachmentAttributeName to ASTextNode2.attributedText, it doesn’t work though. Any ideas to make NSTextAttachment visible in ASTextNode2?

ASTextNode2 *textNode = [ASTextNode2 new];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:content ?: @"" attributes:@{
                                                                                                                NSFontAttributeName : contentFont,
                                                                                                                NSForegroundColorAttributeName : contentTextColor
                                                                                                                }];

NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [UIImage as_imageNamed:answerIconName];
textAttachment.bounds = CGRectMake(0, -2, [UIImage as_imageNamed:answerIconName].size.width, [UIImage as_imageNamed:answerIconName].size.height);
NSAttributedString *attachmentAttributedString = [NSAttributedString attributedStringWithAttachment:textAttachment];
NSMutableAttributedString *mutableAttachmentAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:attachmentAttributedString];
[mutableAttachmentAttributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@" " attributes:nil]];
[mutableAttachmentAttributedString addAttribute:NSAttachmentAttributeName value:textAttachment range:NSMakeRange(0, mutableAttachmentAttributedString.length)];
[attributedString insertAttributedString:mutableAttachmentAttributedString atIndex:0];

textNode.attributedText = attributedString;
textNode.userInteractionEnabled = YES;
textNode.maximumNumberOfLines = _maxLineCount;
textNode.truncationMode = NSLineBreakByTruncatingTail;
textNode.additionalTruncationMessage = [[NSAttributedString alloc] initWithString:@"...unfold" attributes:@{
                                                                                                        NSFontAttributeName : contentFont,
                                                                                                        NSForegroundColorAttributeName : SRGBCOLOR_HEX(0x4769a9)
                                                                                                        }];
textNode.delegate = self;

WIZOooo avatar Aug 14 '19 07:08 WIZOooo

I found a tricky way to do this. NSMutableParagraphStyle.firstLineHeadIndent can be used to make space for the attachment image. And you can overlay the attachment image on the ASTextNode2 at the firstLineHeadIndent place.

WIZOooo avatar Aug 15 '19 03:08 WIZOooo

I meet the same problem, any other idea can fix this issue?

heqichang avatar May 18 '20 03:05 heqichang

same issue. YYLabel is fine, ASTextNode and ASTextNode2 is broken.

CleanShot 2021-08-23 at 09 49 26@2x

CleanShot 2021-08-23 at 09 52 53@2x

CleanShot 2021-08-23 at 09 50 59@2x

iwecon avatar Aug 23 '21 01:08 iwecon