Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Support inline images in Text with lineHeight #48994

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion packages/react-native/Libraries/Text/Text/RCTTextView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,47 @@ - (void)didUpdateReactSubviews
// Do nothing, as subviews are managed by `setTextStorage:` method
}

- (NSAttributedString *)attributedStringWithImage:(UIImage *)image {
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = image;

// Position the image vertically within the line using baseline offset
CGFloat baselineOffset = (self.font.ascender - self.font.descender) / 2;
attachment.bounds = CGRectMake(0, -baselineOffset, image.size.width, image.size.height);

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"\uFFFC"];
[attributedString addAttribute:NSAttachmentAttributeName value:attachment range:NSMakeRange(0, 1)];

return attributedString;
}

- (void)setTextStorage:(NSTextStorage *)textStorage
contentFrame:(CGRect)contentFrame
descendantViews:(NSArray<UIView *> *)descendantViews
{
_textStorage = textStorage;
_contentFrame = contentFrame;

// FIXME: Optimize this.
// Embed images into attributed text
for (UIView *view in descendantViews) {
if ([view isKindOfClass:[RCTImageView class]]) {
RCTImageView *imageView = (RCTImageView *)view;
UIImage *image = imageView.image;
if (image) {
NSAttributedString *imageAttributedString = [self attributedStringWithImage:image];
[_textStorage appendAttributedString:imageAttributedString];
}
}
}

// Remove old subviews
for (UIView *view in _descendantViews) {
[view removeFromSuperview];
}

_descendantViews = descendantViews;

// Add new subviews
for (UIView *view in descendantViews) {
[self addSubview:view];
}
Expand Down
Loading