Skip to content

Commit 96708d5

Browse files
ZHUANGPPfacebook-github-bot
authored andcommitted
Add getRectWithAttributedString() to RCTTextLayoutManager
Summary: Changelog: [iOS][Added] - `getRectWithAttributedString()` aims to get the rect of the fragment with embedded link, which is necessary when building the `accessibilityElement`. In this function, we first enumerate attributedString to find the range of fragments whose `accessibilityRole` is @"link". Then we calculate the rect of the fragment and send to the block and we would define what to do in the block in `RCTParagraphComponentAccessibilityProvider`. Reviewed By: shergin Differential Revision: D22286733 fbshipit-source-id: 4d11cb54375a4e9e72869e646fcb484de089b14b
1 parent 10a800d commit 96708d5

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

ReactCommon/fabric/textlayoutmanager/platform/ios/RCTTextLayoutManager.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515

1616
NS_ASSUME_NONNULL_BEGIN
1717

18+
/**
19+
@abstract Enumeration block for text fragments.
20+
*/
21+
22+
using RCTTextLayoutFragmentEnumerationBlock =
23+
void (^)(CGRect fragmentRect, NSString *_Nonnull fragmentText, NSString *value);
24+
1825
/**
1926
* iOS-specific TextLayoutManager
2027
*/
@@ -38,6 +45,12 @@ NS_ASSUME_NONNULL_BEGIN
3845
frame:(CGRect)frame
3946
atPoint:(CGPoint)point;
4047

48+
- (void)getRectWithAttributedString:(facebook::react::AttributedString)attributedString
49+
paragraphAttributes:(facebook::react::ParagraphAttributes)paragraphAttributes
50+
enumerateAttribute:(NSString *)enumerateAttribute
51+
frame:(CGRect)frame
52+
usingBlock:(RCTTextLayoutFragmentEnumerationBlock)block;
53+
4154
@end
4255

4356
NS_ASSUME_NONNULL_END

ReactCommon/fabric/textlayoutmanager/platform/ios/RCTTextLayoutManager.mm

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,44 @@ - (NSAttributedString *)_nsAttributedStringFromAttributedString:(AttributedStrin
182182
return unwrapManagedObject(sharedNSAttributedString);
183183
}
184184

185+
- (void)getRectWithAttributedString:(AttributedString)attributedString
186+
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
187+
enumerateAttribute:(NSString *)enumerateAttribute
188+
frame:(CGRect)frame
189+
usingBlock:(RCTTextLayoutFragmentEnumerationBlock)block
190+
{
191+
NSTextStorage *textStorage = [self
192+
_textStorageAndLayoutManagerWithAttributesString:[self _nsAttributedStringFromAttributedString:attributedString]
193+
paragraphAttributes:paragraphAttributes
194+
size:frame.size];
195+
196+
NSLayoutManager *layoutManager = textStorage.layoutManagers.firstObject;
197+
NSTextContainer *textContainer = layoutManager.textContainers.firstObject;
198+
[layoutManager ensureLayoutForTextContainer:textContainer];
199+
200+
NSRange glyphRange = [layoutManager glyphRangeForTextContainer:textContainer];
201+
NSRange characterRange = [layoutManager characterRangeForGlyphRange:glyphRange actualGlyphRange:NULL];
202+
203+
[textStorage enumerateAttribute:enumerateAttribute
204+
inRange:characterRange
205+
options:0
206+
usingBlock:^(NSString *value, NSRange range, BOOL *pause) {
207+
if (!value) {
208+
return;
209+
}
210+
211+
[layoutManager
212+
enumerateEnclosingRectsForGlyphRange:range
213+
withinSelectedGlyphRange:range
214+
inTextContainer:textContainer
215+
usingBlock:^(CGRect enclosingRect, BOOL *_Nonnull stop) {
216+
block(
217+
enclosingRect,
218+
[textStorage attributedSubstringFromRange:range].string,
219+
value);
220+
*stop = YES;
221+
}];
222+
}];
223+
}
224+
185225
@end

0 commit comments

Comments
 (0)