Invalidate NSTextStorage on AttributedString change (#38070)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/38070

changelog:
[iOS][fix]: Correctly invalidate NSTextStorage when non layout related props change

Fixes: https://github.com/facebook/react-native/issues/37944

Problem:
NSTextStorage was not invalidated if non-layout props were changed. So for example 'color' dynamically changed, it wouldn't get invalidated and font of incorrect color would be rendered on screen.

Reviewed By: javache

Differential Revision: D47019250

fbshipit-source-id: bd5d8e6ee4493791dedbafc64a8b8df48a5681e4
This commit is contained in:
Samuel Susla
2023-06-26 13:49:47 -07:00
committed by Facebook GitHub Bot
parent e1fd4a8fcd
commit 247da6ef7f
2 changed files with 8 additions and 7 deletions
@@ -42,20 +42,22 @@ bool ParagraphLayoutManager::shoudMeasureString(
AttributedString const &attributedString,
ParagraphAttributes const &paragraphAttributes,
LayoutConstraints layoutConstraints) const {
size_t newHash = folly::hash::hash_combine(
0,
textAttributedStringHashLayoutWise(attributedString),
paragraphAttributes);
size_t newParagraphInputHash =
folly::hash::hash_combine(0, attributedString, paragraphAttributes);
if (newHash != paragraphInputHash_) {
if (newParagraphInputHash != paragraphInputHash_) {
// AttributedString or ParagraphAttributes have changed.
// Must create new host text storage and trigger measure.
hostTextStorage_ = textLayoutManager_->getHostTextStorage(
attributedString, paragraphAttributes, layoutConstraints);
paragraphInputHash_ = newHash;
paragraphInputHash_ = newParagraphInputHash;
return true; // Must measure again.
}
// Detect the case when available width for Paragraph meaningfully changes.
// This is to prevent unnecessary re-creation of NSTextStorage on iOS.
// On Android, this is no-op.
bool hasMaximumSizeChanged =
layoutConstraints.maximumSize.width != lastAvailableWidth_;
Float threshold = 0.01f;
@@ -9,7 +9,6 @@
#include <react/renderer/components/text/BaseTextShadowNode.h>
#include <react/renderer/components/text/ParagraphEventEmitter.h>
#include <react/renderer/components/text/ParagraphLayoutManager.h>
#include <react/renderer/components/text/ParagraphProps.h>
#include <react/renderer/components/text/ParagraphState.h>
#include <react/renderer/components/view/ConcreteViewShadowNode.h>