Skip to content

Commit

Permalink
优化渐变色实现,支持rgb(a)和16进制
Browse files Browse the repository at this point in the history
  • Loading branch information
jingcheng1988 committed Jan 2, 2024
1 parent 1756551 commit ec21140
Showing 1 changed file with 70 additions and 44 deletions.
114 changes: 70 additions & 44 deletions GaiaXiOS/GaiaXiOS/Utilities/GXGradientHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

#import "GXGradientHelper.h"
#import "NSDictionary+GX.h"
#import "GXCacheCenter.h"
#import "NSArray+GX.h"
#import "UIColor+GX.h"
#import "GXCache.h"
#import "GXUtils.h"

@interface GXGradientView () {
Expand Down Expand Up @@ -251,60 +253,84 @@ + (UIImage *)renderImageFromLayer:(CALayer *)layer{
#pragma mark - 解析渐变色

+ (NSDictionary *)parserLinearGradient:(NSString *)linearGradient{
if (![linearGradient hasPrefix:@"linear-gradient("] || ![linearGradient hasSuffix:@")"]) {
return nil;
}
NSMutableDictionary *dict = nil;
// 掐头去尾
NSString *linearString = [linearGradient substringWithRange:NSMakeRange(16, [linearGradient length]-17)];
NSArray *linearArray = [linearString componentsSeparatedByString:@","];
if (linearArray.count) {
//构建渐变数据源
dict = [NSMutableDictionary dictionaryWithCapacity:2];
NSMutableArray *locations = [NSMutableArray arrayWithCapacity:2];
NSMutableArray *colors = [NSMutableArray arrayWithCapacity:2];
for (NSUInteger i = 0; i < [linearArray count]; i++) {
NSString *linear = linearArray[i];
if (i == 0) {
//解析location
linear = [linear stringByReplacingOccurrencesOfString:@" " withString:@""];
[dict gx_setValue:linear forKey:@"direction"];
} else {
//解析颜色
linear = [linear stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([linear hasSuffix:@"%"]) {
//包含百分比,设置location
NSArray *linears = [linear componentsSeparatedByString:@" "];
for (int j = 0; j < linears.count; j++) {
NSString *tmpLinear = linears[j];
if (j == 0) {
linear = tmpLinear;
} else {
//添加location
if (tmpLinear.length > 1) {
NSNumber *location = @([[tmpLinear substringToIndex:tmpLinear.length-1] floatValue] / 100.0f);
[locations gx_addObject:location];
}
}
}
// 解析内容
NSRegularExpression *regex = [self linearGradientRegular];
if (regex) {
NSArray *matches = [regex matchesInString:linearString options:0 range:NSMakeRange(0, [linearString length])];
if (matches.count > 0) {
dict = [NSMutableDictionary dictionaryWithCapacity:2];
NSMutableArray *locations = [NSMutableArray array];
NSMutableArray *colors = [NSMutableArray array];
NSString *direction = @"";

for (NSTextCheckingResult *match in matches) {
// direction
NSRange directionRange = [match rangeAtIndex:1];
if (directionRange.location != NSNotFound) {
direction = [linearString substringWithRange:directionRange];
direction = [direction stringByReplacingOccurrencesOfString:@" " withString:@""];
continue;
}

// colors
NSRange colorRange = [match rangeAtIndex:2];
if (colorRange.location != NSNotFound) {
NSString *color = [linearString substringWithRange:colorRange];
[colors addObject:color];
}

// locations
NSRange locationRange = [match rangeAtIndex:3];
if (locationRange.location != NSNotFound) {
NSString *locationString = [linearString substringWithRange:locationRange];
locationString = [locationString stringByReplacingOccurrencesOfString:@"" withString:@"%"];
NSNumber *location = @([locationString floatValue] / 100.0f);
[locations addObject:location];
}

//添加颜色
[colors gx_addObject:linear];
}

// direction
[dict gx_setValue:direction forKey:@"direction"];

// location和color一致时才会设置
if (locations.count && (locations.count == colors.count)) {
[dict gx_setObject:locations forKey:@"locations"];
}

// 设置color
[dict gx_setObject:colors forKey:@"colors"];
}

//location和color一致时才会设置
if (locations.count && (locations.count == colors.count)) {
[dict gx_setObject:locations forKey:@"locations"];
}

//设置color
[dict gx_setObject:colors forKey:@"colors"];
}

return dict;
}

+ (NSRegularExpression *)linearGradientRegular{
NSString *key = @"linear-gradient";
GXCache *cahche = [GXCacheCenter defaulCenter].regularCahche;
NSRegularExpression *regular = [cahche objectForKey:key];
// 缓存为空创建表达式
if (regular == nil) {
// 获取正则的string
NSError *error = NULL;
NSString *regexStr = @"(to\\s+\\w+)|(rgba?\\([^)]+\\)|#\\w{3,8})\\s*(\\d+%)*";
regular = [NSRegularExpression regularExpressionWithPattern:regexStr
options:NSRegularExpressionCaseInsensitive
error:&error];
if (regular) {
[cahche setObject:regular forKey:key];
}

}
// 返回
return regular;
}



@end

0 comments on commit ec21140

Please sign in to comment.