|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +#import "RCTBaseTextShadowView.h" |
| 11 | + |
| 12 | +#import <React/RCTShadowView+Layout.h> |
| 13 | + |
| 14 | +#import "RCTRawTextShadowView.h" |
| 15 | +#import "RCTVirtualTextShadowView.h" |
| 16 | + |
| 17 | +NSString *const RCTBaseTextShadowViewEmbeddedShadowViewAttributeName = @"RCTBaseTextShadowViewEmbeddedShadowViewAttributeName"; |
| 18 | + |
| 19 | +@implementation RCTBaseTextShadowView |
| 20 | +{ |
| 21 | + NSAttributedString *_Nullable _cachedAttributedText; |
| 22 | + RCTTextAttributes *_Nullable _cachedTextAttributes; |
| 23 | +} |
| 24 | + |
| 25 | +- (instancetype)init |
| 26 | +{ |
| 27 | + if (self = [super init]) { |
| 28 | + _textAttributes = [RCTTextAttributes new]; |
| 29 | + } |
| 30 | + |
| 31 | + return self; |
| 32 | +} |
| 33 | + |
| 34 | +- (void)setReactTag:(NSNumber *)reactTag |
| 35 | +{ |
| 36 | + [super setReactTag:reactTag]; |
| 37 | + _textAttributes.tag = reactTag; |
| 38 | +} |
| 39 | + |
| 40 | +#pragma mark - attributedString |
| 41 | + |
| 42 | +- (NSAttributedString *)attributedTextWithBaseTextAttributes:(nullable RCTTextAttributes *)baseTextAttributes |
| 43 | +{ |
| 44 | + RCTTextAttributes *textAttributes; |
| 45 | + |
| 46 | + if (baseTextAttributes) { |
| 47 | + textAttributes = [baseTextAttributes copy]; |
| 48 | + [textAttributes applyTextAttributes:self.textAttributes]; |
| 49 | + } else { |
| 50 | + textAttributes = [self.textAttributes copy]; |
| 51 | + } |
| 52 | + |
| 53 | + if (_cachedAttributedText && [_cachedTextAttributes isEqual:textAttributes]) { |
| 54 | + return _cachedAttributedText; |
| 55 | + } |
| 56 | + |
| 57 | + NSMutableAttributedString *attributedText = [NSMutableAttributedString new]; |
| 58 | + |
| 59 | + [attributedText beginEditing]; |
| 60 | + |
| 61 | + for (RCTShadowView *shadowView in self.reactSubviews) { |
| 62 | + // Special Case: RCTRawTextShadowView |
| 63 | + if ([shadowView isKindOfClass:[RCTRawTextShadowView class]]) { |
| 64 | + RCTRawTextShadowView *rawTextShadowView = (RCTRawTextShadowView *)shadowView; |
| 65 | + NSString *text = rawTextShadowView.text; |
| 66 | + if (text) { |
| 67 | + NSAttributedString *rawTextAttributedString = |
| 68 | + [[NSAttributedString alloc] initWithString:rawTextShadowView.text |
| 69 | + attributes:textAttributes.effectiveTextAttributes]; |
| 70 | + [attributedText appendAttributedString:rawTextAttributedString]; |
| 71 | + } |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + // Special Case: RCTBaseTextShadowView |
| 76 | + if ([shadowView isKindOfClass:[RCTBaseTextShadowView class]]) { |
| 77 | + RCTBaseTextShadowView *baseTextShadowView = (RCTBaseTextShadowView *)shadowView; |
| 78 | + NSAttributedString *baseTextAttributedString = |
| 79 | + [baseTextShadowView attributedTextWithBaseTextAttributes:textAttributes]; |
| 80 | + [attributedText appendAttributedString:baseTextAttributedString]; |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + // Generic Case: Any RCTShadowView |
| 85 | + NSTextAttachment *attachment = [NSTextAttachment new]; |
| 86 | + NSMutableAttributedString *embeddedShadowViewAttributedString = [NSMutableAttributedString new]; |
| 87 | + [embeddedShadowViewAttributedString beginEditing]; |
| 88 | + [embeddedShadowViewAttributedString appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]]; |
| 89 | + [embeddedShadowViewAttributedString addAttribute:RCTBaseTextShadowViewEmbeddedShadowViewAttributeName |
| 90 | + value:shadowView |
| 91 | + range:(NSRange){0, embeddedShadowViewAttributedString.length}]; |
| 92 | + [embeddedShadowViewAttributedString endEditing]; |
| 93 | + [attributedText appendAttributedString:embeddedShadowViewAttributedString]; |
| 94 | + } |
| 95 | + |
| 96 | + [attributedText endEditing]; |
| 97 | + |
| 98 | + [self clearLayout]; |
| 99 | + |
| 100 | + _cachedAttributedText = [attributedText copy]; |
| 101 | + _cachedTextAttributes = textAttributes; |
| 102 | + |
| 103 | + return _cachedAttributedText; |
| 104 | +} |
| 105 | + |
| 106 | +- (void)dirtyLayout |
| 107 | +{ |
| 108 | + [super dirtyLayout]; |
| 109 | + _cachedAttributedText = nil; |
| 110 | + _cachedTextAttributes = nil; |
| 111 | +} |
| 112 | + |
| 113 | +- (void)didUpdateReactSubviews |
| 114 | +{ |
| 115 | + [super didUpdateReactSubviews]; |
| 116 | + [self dirtyLayout]; |
| 117 | +} |
| 118 | + |
| 119 | +- (void)didSetProps:(NSArray<NSString *> *)changedProps |
| 120 | +{ |
| 121 | + [super didSetProps:changedProps]; |
| 122 | + [self dirtyLayout]; |
| 123 | +} |
| 124 | + |
| 125 | +@end |
0 commit comments