TextureGroup / Texture

Smooth asynchronous user interfaces for iOS apps.
https://texturegroup.org/
Other
8.02k stars 1.29k forks source link

textAttachment unavailable in ASTextNode2 #1616

Open WIZOooo opened 5 years ago

WIZOooo commented 5 years ago

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 commented 5 years ago

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.

heqichang commented 4 years ago

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

iWECon commented 3 years ago

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