From 019612800816be3bd1ff55f89b57bc71b3854529 Mon Sep 17 00:00:00 2001 From: CodeST <694468528@qq.com> Date: Wed, 19 Nov 2025 19:15:28 +0800 Subject: [PATCH] 1 --- CustomKeyboard/View/KBKeyButton.m | 2 +- keyBoard/Class/Manager/KBSkinService.m | 133 +++---------------------- 2 files changed, 14 insertions(+), 121 deletions(-) diff --git a/CustomKeyboard/View/KBKeyButton.m b/CustomKeyboard/View/KBKeyButton.m index a0501dd..116d98d 100644 --- a/CustomKeyboard/View/KBKeyButton.m +++ b/CustomKeyboard/View/KBKeyButton.m @@ -38,7 +38,7 @@ if (!self.iconView) { UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectZero]; // 作为按键的整块皮肤背景,铺满整个按钮区域 - iv.contentMode = UIViewContentModeScaleAspectFill; + iv.contentMode = UIViewContentModeScaleAspectFit; iv.clipsToBounds = YES; iv.translatesAutoresizingMaskIntoConstraints = NO; [self addSubview:iv]; diff --git a/keyBoard/Class/Manager/KBSkinService.m b/keyBoard/Class/Manager/KBSkinService.m index d43be87..6cc9d8f 100644 --- a/keyBoard/Class/Manager/KBSkinService.m +++ b/keyBoard/Class/Manager/KBSkinService.m @@ -163,133 +163,26 @@ return; } - // 1. 点击应用皮肤时,检查键盘启用 & 完全访问状态,并尽量给出友好提示。 - KBKeyboardPermissionManager *perm = [KBKeyboardPermissionManager shared]; - BOOL enabled = [perm isKeyboardEnabled]; - KBFARecord fa = [perm lastKnownFullAccess]; - BOOL hasFullAccess = (fa == KBFARecordGranted); - - if (!enabled || !hasFullAccess) { - // 引导页(内部有自己的展示策略,避免过度打扰) - [perm presentPermissionIfNeededFrom:presenting]; - - // 简单提示:皮肤可以应用,但未开启完全访问时扩展无法读取 App Group 中的图片。 - [KBHUD showInfo:KBLocalized(@"皮肤已应用,键盘需开启“允许完全访问”后才能显示图片")]; - } +// // 1. 点击应用皮肤时,检查键盘启用 & 完全访问状态,并尽量给出友好提示。 +// KBKeyboardPermissionManager *perm = [KBKeyboardPermissionManager shared]; +// BOOL enabled = [perm isKeyboardEnabled]; +// KBFARecord fa = [perm lastKnownFullAccess]; +// BOOL hasFullAccess = (fa == KBFARecordGranted); +// +// if (!enabled || !hasFullAccess) { +// // 引导页(内部有自己的展示策略,避免过度打扰) +// [perm presentPermissionIfNeededFrom:presenting]; +// +// // 简单提示:皮肤可以应用,但未开启完全访问时扩展无法读取 App Group 中的图片。 +// [KBHUD showInfo:KBLocalized(@"皮肤已应用,键盘需开启“允许完全访问”后才能显示图片")]; +// } #if KB_SKIN_ICON_USE_REMOTE [self kb_applySkinUsingRemoteIcons:skinJSON completion:completion]; #else - [self kb_applySkinUsingLocalIcons:skinJSON completion:completion]; #endif } -#pragma mark - Internal helpers - -/// 本地模式:key_icons 的 value 为 Assets 名称(主 App bundle 内),导出到 App Group 后再写入主题。 -- (void)kb_applySkinUsingLocalIcons:(NSDictionary *)skin completion:(KBSkinApplyCompletion)completion { - NSString *skinId = skin[@"id"] ?: @"local"; - NSString *name = skin[@"name"] ?: skinId; - - NSDictionary *iconNames = [skin[@"key_icons"] isKindOfClass:NSDictionary.class] ? skin[@"key_icons"] : @{}; - NSMutableDictionary *iconPathMap = [NSMutableDictionary dictionary]; - - NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:AppGroup]; - if (containerURL && iconNames.count > 0) { - NSString *skinsRoot = [containerURL.path stringByAppendingPathComponent:@"Skins"]; - NSString *skinRoot = [skinsRoot stringByAppendingPathComponent:skinId]; - NSString *iconsDir = [skinRoot stringByAppendingPathComponent:@"icons"]; - [[NSFileManager defaultManager] createDirectoryAtPath:iconsDir - withIntermediateDirectories:YES - attributes:nil - error:NULL]; - - [iconNames enumerateKeysAndObjectsUsingBlock:^(NSString *identifier, NSString *imageName, BOOL *stop) { - if (![imageName isKindOfClass:NSString.class] || imageName.length == 0) return; - UIImage *img = [UIImage imageNamed:imageName]; - if (!img) return; - NSData *data = UIImagePNGRepresentation(img); - if (data.length == 0) return; - NSString *fileName = [NSString stringWithFormat:@"%@.png", identifier]; - NSString *fullPath = [iconsDir stringByAppendingPathComponent:fileName]; - if ([data writeToFile:fullPath atomically:YES]) { - NSString *relative = [NSString stringWithFormat:@"Skins/%@/icons/%@", skinId, fileName]; - iconPathMap[identifier] = relative; - } - }]; - } - - NSMutableDictionary *themeJSON = [skin mutableCopy]; - if (iconPathMap.count > 0) { - themeJSON[@"key_icons"] = iconPathMap.copy; // value 改为 App Group 相对路径 - } - - // 先应用颜色 / hidden_keys / key_icons(此时为相对路径,扩展从 App Group 读取) - BOOL themeOK = [[KBSkinManager shared] applyThemeFromJSON:themeJSON]; - - // 再处理背景图片(background_image),与原逻辑一致 - NSString *bgURL = skin[@"background_image"] ?: @""; - if (bgURL.length == 0) { - if (completion) completion(themeOK); - if (themeOK) { - [KBHUD showInfo:KBLocalized(@"已应用皮肤(无背景图)")]; - } else { - [KBHUD showInfo:KBLocalized(@"应用皮肤失败")]; - } - return; - } - - [[KBNetworkManager shared] GET:bgURL parameters:nil headers:nil completion:^(id jsonOrData, NSURLResponse *response, NSError *error) { - NSData *data = ([jsonOrData isKindOfClass:NSData.class] ? (NSData *)jsonOrData : nil); - // 压缩尺寸,避免 Keychain 过大 - if (data && data.length > 0) { - UIImage *img = [UIImage imageWithData:data]; - if (img) { - CGFloat maxW = 1500.0; - if (img.size.width > maxW) { - CGFloat scale = maxW / img.size.width; - CGSize newSize = CGSizeMake(maxW, floor(img.size.height * scale)); - UIGraphicsBeginImageContextWithOptions(newSize, YES, 1.0); - [img drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; - UIImage *resized = UIGraphicsGetImageFromCurrentImageContext(); - UIGraphicsEndImageContext(); - img = resized ?: img; - } - data = UIImageJPEGRepresentation(img, 0.85) ?: data; - } - } - dispatch_async(dispatch_get_main_queue(), ^{ - NSData *payload = data; - if (payload.length == 0) { - // 兜底:生成一张简单渐变图片 - CGSize size = CGSizeMake(1200, 600); - UIGraphicsBeginImageContextWithOptions(size, YES, 1.0); - CGContextRef ctx = UIGraphicsGetCurrentContext(); - UIColor *c1 = [UIColor colorWithRed:0.76 green:0.91 blue:0.86 alpha:1]; - UIColor *c2 = [UIColor colorWithRed:0.93 green:0.97 blue:0.91 alpha:1]; - if ([skin[@"id"] hasPrefix:@"dark"]) { - c1 = [UIColor colorWithRed:0.1 green:0.12 blue:0.16 alpha:1]; - c2 = [UIColor colorWithRed:0.22 green:0.24 blue:0.28 alpha:1]; - } - CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); - NSArray *colors = @[(__bridge id)c1.CGColor, (__bridge id)c2.CGColor]; - CGFloat locs[] = {0,1}; - CGGradientRef grad = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, locs); - CGContextDrawLinearGradient(ctx, grad, CGPointZero, CGPointMake(size.width, size.height), 0); - CGGradientRelease(grad); CGColorSpaceRelease(space); - UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); - UIGraphicsEndImageContext(); - payload = UIImageJPEGRepresentation(img, 0.9); - } - BOOL ok = (payload.length > 0) - ? [[KBSkinManager shared] applyImageSkinWithData:payload skinId:skinId name:name] - : themeOK; - if (completion) completion(ok); - [KBHUD showInfo:(ok ? KBLocalized(@"已应用,切到键盘查看") : KBLocalized(@"应用皮肤失败"))]; - }); - }]; -} - /// 远程 Zip 模式:skinJSON 提供 zip_url,一套皮肤一个压缩包。 /// - Zip 解压路径:AppGroup/Skins//... /// * 图标:icons/.png,例如 icons/key_a.png