Skip to content

Commit 45274cb

Browse files
committed
improve: now setText can set at anytime.
1 parent afd206b commit 45274cb

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

Source/SVGAContentLayer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
@property (nonatomic, strong) NSString *imageKey;
1616
@property (nonatomic, strong) SVGABitmapLayer *bitmapLayer;
1717
@property (nonatomic, strong) SVGAVectorLayer *vectorLayer;
18+
@property (nonatomic, strong) CATextLayer *textLayer;
1819

1920
- (instancetype)initWithFrames:(NSArray<SVGAVideoSpriteFrameEntity *> *)frames;
2021

Source/SVGAPlayer.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ - (void)draw {
123123
[textLayer setString:self.dynamicTexts[sprite.imageKey]];
124124
textLayer.frame = CGRectMake(0, 0, size.width, size.height);
125125
[contentLayer addSublayer:textLayer];
126+
contentLayer.textLayer = textLayer;
126127
}
127128
}
128129
}];
@@ -297,6 +298,24 @@ - (void)setAttributedText:(NSAttributedString *)attributedText forKey:(NSString
297298
NSMutableDictionary *mutableDynamicTexts = [self.dynamicTexts mutableCopy];
298299
[mutableDynamicTexts setObject:attributedText forKey:aKey];
299300
self.dynamicTexts = mutableDynamicTexts;
301+
if (self.drawLayer.sublayers.count > 0) {
302+
CGSize size = [attributedText boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:NULL].size;
303+
CATextLayer *textLayer;
304+
for (SVGAContentLayer *layer in self.drawLayer.sublayers) {
305+
if ([layer isKindOfClass:[SVGAContentLayer class]] && [layer.imageKey isEqualToString:aKey]) {
306+
textLayer = layer.textLayer;
307+
if (textLayer == nil) {
308+
textLayer = [CATextLayer layer];
309+
[layer addSublayer:textLayer];
310+
}
311+
}
312+
}
313+
if (textLayer != nil) {
314+
textLayer.contentsScale = [[UIScreen mainScreen] scale];
315+
[textLayer setString:attributedText];
316+
textLayer.frame = CGRectMake(0, 0, size.width, size.height);
317+
}
318+
}
300319
}
301320

302321
- (void)clearDynamicObjects {

0 commit comments

Comments
 (0)