Skip to content

Commit 66fae55

Browse files
author
PonyCui
committed
feat: Add multiple line text layer support.
feat: Add text alignment support.
1 parent ed0a4a5 commit 66fae55

File tree

6 files changed

+73
-5
lines changed

6 files changed

+73
-5
lines changed

Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ DEPENDENCIES:
4747
- Yoga
4848

4949
SPEC REPOS:
50-
https://github.com/CocoaPods/Specs.git:
50+
https://github.com/cocoapods/specs.git:
5151
- Protobuf
5252
- SSZipArchive
5353
- Yoga
@@ -64,4 +64,4 @@ SPEC CHECKSUMS:
6464

6565
PODFILE CHECKSUM: 7f6714245d47e69d2933463289e4c4d6de65b831
6666

67-
COCOAPODS: 1.8.0.beta.2
67+
COCOAPODS: 1.7.4

SVGAPlayer/ViewController.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ - (IBAction)onChange:(id)sender {
5555
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
5656
if (videoItem != nil) {
5757
self.aPlayer.videoItem = videoItem;
58+
NSMutableParagraphStyle *para = [[NSMutableParagraphStyle alloc] init];
59+
[para setLineBreakMode:NSLineBreakByTruncatingTail];
60+
[para setAlignment:NSTextAlignmentCenter];
61+
NSAttributedString *str = [[NSAttributedString alloc]
62+
initWithString:@"Hello, World! Hello, World!"
63+
attributes:@{
64+
NSFontAttributeName: [UIFont systemFontOfSize:28],
65+
NSForegroundColorAttributeName: [UIColor whiteColor],
66+
NSParagraphStyleAttributeName: para,
67+
}];
68+
[self.aPlayer setAttributedText:str forKey:@"banner"];
5869
[self.aPlayer startAnimation];
5970
}
6071
} failureBlock:nil];

Source/SVGAContentLayer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@
2323
- (instancetype)initWithFrames:(NSArray<SVGAVideoSpriteFrameEntity *> *)frames;
2424

2525
- (void)stepToFrame:(NSInteger)frame;
26+
- (void)resetTextLayerProperties:(NSAttributedString *)attributedString;
2627

2728
@end

Source/SVGAContentLayer.m

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
@interface SVGAContentLayer ()
1515

1616
@property (nonatomic, strong) NSArray<SVGAVideoSpriteFrameEntity *> *frames;
17+
@property (nonatomic, assign) NSTextAlignment textLayerAlignment;
1718

1819
@end
1920

@@ -25,6 +26,7 @@ - (instancetype)initWithFrames:(NSArray *)frames {
2526
self.backgroundColor = [UIColor clearColor].CGColor;
2627
self.masksToBounds = NO;
2728
_frames = frames;
29+
_textLayerAlignment = NSTextAlignmentCenter;
2830
[self stepToFrame:0];
2931
}
3032
return self;
@@ -68,7 +70,20 @@ - (void)setFrame:(CGRect)frame {
6870
for (CALayer *sublayer in self.sublayers) {
6971
if ([sublayer isKindOfClass:[CATextLayer class]]) {
7072
CGRect frame = sublayer.frame;
71-
frame.origin.x = (self.frame.size.width - sublayer.frame.size.width) / 2.0;
73+
switch (self.textLayerAlignment) {
74+
case NSTextAlignmentLeft:
75+
frame.origin.x = 0.0;
76+
break;
77+
case NSTextAlignmentCenter:
78+
frame.origin.x = (self.frame.size.width - sublayer.frame.size.width) / 2.0;
79+
break;
80+
case NSTextAlignmentRight:
81+
frame.origin.x = self.frame.size.width - sublayer.frame.size.width;
82+
break;
83+
default:
84+
frame.origin.x = (self.frame.size.width - sublayer.frame.size.width) / 2.0;
85+
break;
86+
}
7287
frame.origin.y = (self.frame.size.height - sublayer.frame.size.height) / 2.0;
7388
sublayer.frame = frame;
7489
}
@@ -92,4 +107,34 @@ - (void)setDynamicHidden:(BOOL)dynamicHidden {
92107
self.hidden = dynamicHidden;
93108
}
94109

110+
- (void)resetTextLayerProperties:(NSAttributedString *)attributedString {
111+
NSDictionary *textAttrs = (id)[attributedString attributesAtIndex:0 effectiveRange:nil];
112+
NSParagraphStyle *paragraphStyle = textAttrs[NSParagraphStyleAttributeName];
113+
if (paragraphStyle == nil) {
114+
return;
115+
}
116+
if (paragraphStyle.lineBreakMode == NSLineBreakByTruncatingTail) {
117+
self.textLayer.truncationMode = kCATruncationEnd;
118+
[self.textLayer setWrapped:NO];
119+
}
120+
else if (paragraphStyle.lineBreakMode == NSLineBreakByTruncatingMiddle) {
121+
self.textLayer.truncationMode = kCATruncationMiddle;
122+
[self.textLayer setWrapped:NO];
123+
}
124+
else if (paragraphStyle.lineBreakMode == NSLineBreakByTruncatingHead) {
125+
self.textLayer.truncationMode = kCATruncationStart;
126+
[self.textLayer setWrapped:NO];
127+
}
128+
else {
129+
self.textLayer.truncationMode = kCATruncationNone;
130+
[self.textLayer setWrapped:YES];
131+
}
132+
if (paragraphStyle.alignment == NSTextAlignmentNatural) {
133+
self.textLayerAlignment = NSTextAlignmentCenter;
134+
}
135+
else {
136+
self.textLayerAlignment = paragraphStyle.alignment;
137+
}
138+
}
139+
95140
@end

Source/SVGAPlayer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ typedef void(^SVGAPlayerDynamicDrawingBlock)(CALayer *contentLayer, NSInteger fr
4343
- (void)setImageWithURL:(NSURL *)URL forKey:(NSString *)aKey;
4444
- (void)setImage:(UIImage *)image forKey:(NSString *)aKey referenceLayer:(CALayer *)referenceLayer; // deprecated from 2.0.1
4545
- (void)setAttributedText:(NSAttributedString *)attributedText forKey:(NSString *)aKey;
46+
- (void)setAttributedText:(NSAttributedString *)attributedText
47+
forKey:(NSString *)aKey
48+
lineBreakMode:(NSLineBreakMode)lineBreakMode
49+
wrapped:(BOOL)wrapped;
4650
- (void)setDrawingBlock:(SVGAPlayerDynamicDrawingBlock)drawingBlock forKey:(NSString *)aKey;
4751
- (void)setHidden:(BOOL)hidden forKey:(NSString *)aKey;
4852
- (void)clearDynamicObjects;

Source/SVGAPlayer.m

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,17 @@ - (void)draw {
202202
if (sprite.imageKey != nil) {
203203
if (self.dynamicTexts[sprite.imageKey] != nil) {
204204
NSAttributedString *text = self.dynamicTexts[sprite.imageKey];
205-
CGSize size = [text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:NULL].size;
205+
CGSize bitmapSize = CGSizeMake(self.videoItem.images[sprite.imageKey].size.width * self.videoItem.images[sprite.imageKey].scale, self.videoItem.images[sprite.imageKey].size.height * self.videoItem.images[sprite.imageKey].scale);
206+
CGSize size = [text boundingRectWithSize:bitmapSize
207+
options:NSStringDrawingUsesLineFragmentOrigin
208+
context:NULL].size;
206209
CATextLayer *textLayer = [CATextLayer layer];
207210
textLayer.contentsScale = [[UIScreen mainScreen] scale];
208211
[textLayer setString:self.dynamicTexts[sprite.imageKey]];
209212
textLayer.frame = CGRectMake(0, 0, size.width, size.height);
210213
[contentLayer addSublayer:textLayer];
211214
contentLayer.textLayer = textLayer;
215+
[contentLayer resetTextLayerProperties:text];
212216
}
213217
if (self.dynamicHiddens[sprite.imageKey] != nil &&
214218
[self.dynamicHiddens[sprite.imageKey] boolValue] == YES) {
@@ -428,7 +432,9 @@ - (void)setAttributedText:(NSAttributedString *)attributedText forKey:(NSString
428432
[mutableDynamicTexts setObject:attributedText forKey:aKey];
429433
self.dynamicTexts = mutableDynamicTexts;
430434
if (self.contentLayers.count > 0) {
431-
CGSize size = [attributedText boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:NULL].size;
435+
CGSize bitmapSize = CGSizeMake(self.videoItem.images[aKey].size.width * self.videoItem.images[aKey].scale, self.videoItem.images[aKey].size.height * self.videoItem.images[aKey].scale);
436+
CGSize size = [attributedText boundingRectWithSize:bitmapSize
437+
options:NSStringDrawingUsesLineFragmentOrigin context:NULL].size;
432438
CATextLayer *textLayer;
433439
for (SVGAContentLayer *layer in self.contentLayers) {
434440
if ([layer isKindOfClass:[SVGAContentLayer class]] && [layer.imageKey isEqualToString:aKey]) {
@@ -437,6 +443,7 @@ - (void)setAttributedText:(NSAttributedString *)attributedText forKey:(NSString
437443
textLayer = [CATextLayer layer];
438444
[layer addSublayer:textLayer];
439445
layer.textLayer = textLayer;
446+
[layer resetTextLayerProperties:attributedText];
440447
}
441448
}
442449
}

0 commit comments

Comments
 (0)