This commit is contained in:
2025-11-06 14:02:22 +08:00
parent d7874829d9
commit 15fc9621cd
15 changed files with 337 additions and 16 deletions

View File

@@ -0,0 +1,14 @@
//
// KBLangTestVC.h
// 多语言测试页:点击按钮在中英文之间切换,并刷新文案。
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface KBLangTestVC : UIViewController
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,60 @@
//
// KBLangTestVC.m
//
#import "KBLangTestVC.h"
@interface KBLangTestVC ()
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UIButton *toggleBtn;
@end
@implementation KBLangTestVC
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = UIColor.whiteColor;
[self buildUI];
[self refreshTexts];
//
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshTexts) name:KBLocalizationDidChangeNotification object:nil];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)buildUI {
CGFloat w = UIScreen.mainScreen.bounds.size.width;
self.label = [[UILabel alloc] initWithFrame:CGRectMake(24, 120, w - 48, 60)];
self.label.textAlignment = NSTextAlignmentCenter;
self.label.numberOfLines = 0;
[self.view addSubview:self.label];
self.toggleBtn = [UIButton buttonWithType:UIButtonTypeSystem];
self.toggleBtn.frame = CGRectMake(40, CGRectGetMaxY(self.label.frame) + 24, w - 80, 48);
self.toggleBtn.layer.cornerRadius = 8;
self.toggleBtn.backgroundColor = [UIColor colorWithRed:0.22 green:0.49 blue:0.96 alpha:1];
[self.toggleBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
[self.toggleBtn addTarget:self action:@selector(onToggle) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.toggleBtn];
}
- (void)refreshTexts {
self.title = KBLocalized(@"lang_test_title");
NSString *code = [KBLocalizationManager shared].currentLanguageCode ?: @"";
NSString *fmt = KBLocalized(@"current_lang");
self.label.text = [NSString stringWithFormat:fmt.length ? fmt : @"当前:%@", code];
[self.toggleBtn setTitle:KBLocalized(@"lang_toggle") forState:UIControlStateNormal];
}
- (void)onToggle {
KBLocalizationManager *mgr = [KBLocalizationManager shared];
NSString *next = [mgr.currentLanguageCode.lowercaseString hasPrefix:@"zh"] ? @"en" : @"zh-Hans";
[mgr setCurrentLanguageCode:next persist:YES];
}
@end

View File

@@ -0,0 +1,11 @@
//
// KBSkinCenterVC.h
// 简单的皮肤中心:展示两款示例皮肤,点击“下载并应用”后立即同步到键盘扩展。
//
#import <UIKit/UIKit.h>
@interface KBSkinCenterVC : UIViewController
@end

View File

@@ -0,0 +1,132 @@
//
// KBSkinCenterVC.m
//
#import "KBSkinCenterVC.h"
#import "Masonry.h"
#import "KBNetworkManager.h"
#import "KBSkinManager.h"
#import "KBHUD.h"
@interface KBSkinCell : UITableViewCell
@property (nonatomic, strong) UIButton *applyBtn;
@end
@implementation KBSkinCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.selectionStyle = UITableViewCellSelectionStyleNone;
_applyBtn = [UIButton buttonWithType:UIButtonTypeSystem];
[_applyBtn setTitle:@"下载并应用" forState:UIControlStateNormal];
_applyBtn.layer.cornerRadius = 6; _applyBtn.layer.borderWidth = 1;
_applyBtn.layer.borderColor = [UIColor colorWithWhite:0.85 alpha:1].CGColor;
[self.contentView addSubview:_applyBtn];
[_applyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.contentView).offset(-16);
make.centerY.equalTo(self.contentView);
make.width.mas_equalTo(110);
make.height.mas_equalTo(34);
}];
}
return self;
}
@end
@interface KBSkinCenterVC () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy) NSArray<NSDictionary *> *skins; // id, name, img(url relative to KB_BASE_URL)
@end
@implementation KBSkinCenterVC
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"皮肤中心";
self.view.backgroundColor = [UIColor whiteColor];
// URL KB_BASE_URL
// 使 picsum.photos id访
self.skins = @[
@{ @"id": @"aurora", @"name": @"极光", @"img": @"https://picsum.photos/id/1018/1600/900.jpg" },
@{ @"id": @"alps", @"name": @"雪山", @"img": @"https://picsum.photos/id/1016/1600/900.jpg" },
@{ @"id": @"lake", @"name": @"湖面", @"img": @"https://picsum.photos/id/1039/1600/900.jpg" },
];
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleInsetGrouped];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.tableView.delegate = self; self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
}
#pragma mark - UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.skins.count; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cid = @"skin.cell";
KBSkinCell *cell = [tableView dequeueReusableCellWithIdentifier:cid];
if (!cell) { cell = [[KBSkinCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cid]; }
NSDictionary *skin = self.skins[indexPath.row];
cell.textLabel.text = skin[@"name"]; cell.detailTextLabel.text = skin[@"id"];
[cell.applyBtn removeTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
[cell.applyBtn addTarget:self action:@selector(onApplyBtn:) forControlEvents:UIControlEventTouchUpInside];
cell.applyBtn.tag = indexPath.row;
return cell;
}
- (void)onApplyBtn:(UIButton *)sender {
NSInteger idx = sender.tag;
if (idx < 0 || idx >= self.skins.count) return;
NSDictionary *skin = self.skins[idx];
NSString *path = skin[@"img"] ?: @""; // KB_BASE_URL
// JSON NSData
[[KBNetworkManager shared] GET:path parameters:nil headers:nil completion:^(id jsonOrData, NSURLResponse *response, NSError *error) {
NSData *data = ([jsonOrData isKindOfClass:NSData.class] ? (NSData *)jsonOrData : nil);
// Keychain 1500px
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; // JPEG
}
}
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:skin[@"id"] name:skin[@"name"]] : NO;
[KBHUD showInfo:(ok ? @"已应用,切到键盘查看" : @"应用失败")];
});
}];
}
@end

View File

@@ -0,0 +1,16 @@
//
// KBTestVC.h
// keyBoard
//
// Created by Mac on 2025/11/6.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface KBTestVC : UIViewController
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,94 @@
//
// KBTestVC.m
// keyBoard
//
// Created by Mac on 2025/11/6.
//
#import "KBTestVC.h"
#import "KBGuideVC.h"
#import "KBLangTestVC.h"
@interface KBTestVC ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITextView *textView; //
@property (nonatomic, strong) BaseTableView *tableView; //
@property (nonatomic, copy) NSArray<NSString *> *items; //
@end
@implementation KBTestVC
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
CGFloat width = [UIScreen mainScreen].bounds.size.width;
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 220)];
CGRect frame = CGRectMake(16, 16, width - 32, 188);
self.textView = [[UITextView alloc] initWithFrame:frame];
self.textView.text = KBLocalized(@"home_input_placeholder");
self.textView.layer.borderColor = [UIColor blackColor].CGColor;
self.textView.layer.borderWidth = 0.5;
[header addSubview:self.textView];
//
self.tableView = [[BaseTableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleInsetGrouped];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.tableHeaderView = header;
[self.view addSubview:self.tableView];
self.items = @[ KBLocalized(@"home_item_lang_test"), KBLocalized(@"home_item_keyboard_permission"), @"皮肤中心" ];
dispatch_async(dispatch_get_main_queue(), ^{ [self.textView becomeFirstResponder]; });
[[KBNetworkManager shared] GET:@"app/config" parameters:nil headers:nil completion:^(id _Nullable jsonOrData, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"====");
}];
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.title = KBLocalized(@"home_title");
self.items = @[ KBLocalized(@"home_item_lang_test"), KBLocalized(@"home_item_keyboard_permission"), @"皮肤中心" ];
[self.tableView reloadData];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// KBGuideVC *vc = [[KBGuideVC alloc] init];
// [self.navigationController pushViewController:vc animated:true];
// [KBHUD showInfo:@"加载中..."];
// [KBHUD show];
// [KBHUD showAllowTapToDismiss:true];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.items.count; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = @"home.cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId]; }
cell.textLabel.text = self.items[indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.row == 0) {
//
KBLangTestVC *vc = [KBLangTestVC new];
[self.navigationController pushViewController:vc animated:YES];
} else if (indexPath.row == 1) {
//
KBGuideVC *vc = [KBGuideVC new];
[self.navigationController pushViewController:vc animated:YES];
} else if (indexPath.row == 2) {
//
UIViewController *vc = [NSClassFromString(@"KBSkinCenterVC") new];
if (vc) { [self.navigationController pushViewController:vc animated:YES]; }
}
}
@end