Merge pull request #107 from yyued/dev-multiple-line-text

feat: Add multiple line text layer support.
This commit is contained in:
PonyCui
2019-11-29 10:05:07 +08:00
committed by GitHub
5 changed files with 69 additions and 5 deletions

View File

@@ -47,7 +47,7 @@ DEPENDENCIES:
- Yoga
SPEC REPOS:
https://github.com/CocoaPods/Specs.git:
https://github.com/cocoapods/specs.git:
- Protobuf
- SSZipArchive
- Yoga
@@ -64,4 +64,4 @@ SPEC CHECKSUMS:
PODFILE CHECKSUM: 7f6714245d47e69d2933463289e4c4d6de65b831
COCOAPODS: 1.8.0.beta.2
COCOAPODS: 1.7.4

View File

@@ -55,6 +55,17 @@ static SVGAParser *parser;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if (videoItem != nil) {
self.aPlayer.videoItem = videoItem;
NSMutableParagraphStyle *para = [[NSMutableParagraphStyle alloc] init];
[para setLineBreakMode:NSLineBreakByTruncatingTail];
[para setAlignment:NSTextAlignmentCenter];
NSAttributedString *str = [[NSAttributedString alloc]
initWithString:@"Hello, World! Hello, World!"
attributes:@{
NSFontAttributeName: [UIFont systemFontOfSize:28],
NSForegroundColorAttributeName: [UIColor whiteColor],
NSParagraphStyleAttributeName: para,
}];
[self.aPlayer setAttributedText:str forKey:@"banner"];
[self.aPlayer startAnimation];
}
} failureBlock:nil];

View File

@@ -23,5 +23,6 @@
- (instancetype)initWithFrames:(NSArray<SVGAVideoSpriteFrameEntity *> *)frames;
- (void)stepToFrame:(NSInteger)frame;
- (void)resetTextLayerProperties:(NSAttributedString *)attributedString;
@end

View File

@@ -14,6 +14,7 @@
@interface SVGAContentLayer ()
@property (nonatomic, strong) NSArray<SVGAVideoSpriteFrameEntity *> *frames;
@property (nonatomic, assign) NSTextAlignment textLayerAlignment;
@end
@@ -25,6 +26,7 @@
self.backgroundColor = [UIColor clearColor].CGColor;
self.masksToBounds = NO;
_frames = frames;
_textLayerAlignment = NSTextAlignmentCenter;
[self stepToFrame:0];
}
return self;
@@ -68,7 +70,20 @@
for (CALayer *sublayer in self.sublayers) {
if ([sublayer isKindOfClass:[CATextLayer class]]) {
CGRect frame = sublayer.frame;
frame.origin.x = (self.frame.size.width - sublayer.frame.size.width) / 2.0;
switch (self.textLayerAlignment) {
case NSTextAlignmentLeft:
frame.origin.x = 0.0;
break;
case NSTextAlignmentCenter:
frame.origin.x = (self.frame.size.width - sublayer.frame.size.width) / 2.0;
break;
case NSTextAlignmentRight:
frame.origin.x = self.frame.size.width - sublayer.frame.size.width;
break;
default:
frame.origin.x = (self.frame.size.width - sublayer.frame.size.width) / 2.0;
break;
}
frame.origin.y = (self.frame.size.height - sublayer.frame.size.height) / 2.0;
sublayer.frame = frame;
}
@@ -92,4 +107,34 @@
self.hidden = dynamicHidden;
}
- (void)resetTextLayerProperties:(NSAttributedString *)attributedString {
NSDictionary *textAttrs = (id)[attributedString attributesAtIndex:0 effectiveRange:nil];
NSParagraphStyle *paragraphStyle = textAttrs[NSParagraphStyleAttributeName];
if (paragraphStyle == nil) {
return;
}
if (paragraphStyle.lineBreakMode == NSLineBreakByTruncatingTail) {
self.textLayer.truncationMode = kCATruncationEnd;
[self.textLayer setWrapped:NO];
}
else if (paragraphStyle.lineBreakMode == NSLineBreakByTruncatingMiddle) {
self.textLayer.truncationMode = kCATruncationMiddle;
[self.textLayer setWrapped:NO];
}
else if (paragraphStyle.lineBreakMode == NSLineBreakByTruncatingHead) {
self.textLayer.truncationMode = kCATruncationStart;
[self.textLayer setWrapped:NO];
}
else {
self.textLayer.truncationMode = kCATruncationNone;
[self.textLayer setWrapped:YES];
}
if (paragraphStyle.alignment == NSTextAlignmentNatural) {
self.textLayerAlignment = NSTextAlignmentCenter;
}
else {
self.textLayerAlignment = paragraphStyle.alignment;
}
}
@end

View File

@@ -202,13 +202,17 @@
if (sprite.imageKey != nil) {
if (self.dynamicTexts[sprite.imageKey] != nil) {
NSAttributedString *text = self.dynamicTexts[sprite.imageKey];
CGSize size = [text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:NULL].size;
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);
CGSize size = [text boundingRectWithSize:bitmapSize
options:NSStringDrawingUsesLineFragmentOrigin
context:NULL].size;
CATextLayer *textLayer = [CATextLayer layer];
textLayer.contentsScale = [[UIScreen mainScreen] scale];
[textLayer setString:self.dynamicTexts[sprite.imageKey]];
textLayer.frame = CGRectMake(0, 0, size.width, size.height);
[contentLayer addSublayer:textLayer];
contentLayer.textLayer = textLayer;
[contentLayer resetTextLayerProperties:text];
}
if (self.dynamicHiddens[sprite.imageKey] != nil &&
[self.dynamicHiddens[sprite.imageKey] boolValue] == YES) {
@@ -428,7 +432,9 @@
[mutableDynamicTexts setObject:attributedText forKey:aKey];
self.dynamicTexts = mutableDynamicTexts;
if (self.contentLayers.count > 0) {
CGSize size = [attributedText boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:NULL].size;
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);
CGSize size = [attributedText boundingRectWithSize:bitmapSize
options:NSStringDrawingUsesLineFragmentOrigin context:NULL].size;
CATextLayer *textLayer;
for (SVGAContentLayer *layer in self.contentLayers) {
if ([layer isKindOfClass:[SVGAContentLayer class]] && [layer.imageKey isEqualToString:aKey]) {
@@ -437,6 +443,7 @@
textLayer = [CATextLayer layer];
[layer addSublayer:textLayer];
layer.textLayer = textLayer;
[layer resetTextLayerProperties:attributedText];
}
}
}