Files
peko-ios/YuMi/Modules/YMMine/View/Setting/XPMineSwitchLanguageVC.m
eggmanQQQ ca7051f34e #761
修复多语言布局问题
2024-06-19 00:00:11 +08:00

165 lines
6.2 KiB
Objective-C

//
// XPMineSwitchLanguageVC.m
// YuMi
//
// Created by duoban on 2024/4/8.
//
#import "XPMineSwitchLanguageVC.h"
#import "XPMineSettingItemModel.h"
#import "XPMineSwitchLanguageCell.h"
@interface XPMineSwitchLanguageVC ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic,strong) UIButton *saveBtn;
@property (nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) XPMineSettingItemModel * chooseModel;
///数据源
@property (nonatomic,strong) NSArray<XPMineSettingItemModel *> *datasource;
@end
@implementation XPMineSwitchLanguageVC
- (void)viewDidLoad {
[super viewDidLoad];
[self installUI];
[self installConstraints];
}
-(void)installUI{
self.title = YMLocalizedString(@"XPMineSwitchLanguageVC0");
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.saveBtn];
[self.view addSubview:self.tableView];
}
-(void)installConstraints{
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(kGetScaleWidth(20));
make.leading.trailing.equalTo(self.view).inset(kGetScaleWidth(16));
make.height.mas_equalTo(kGetScaleWidth(153));
}];
}
-(void)saveBtnAction{
NSString *language ;
if ([self.chooseModel.title isEqualToString:@"English"]){
language = @"en";
}else if ([self.chooseModel.title isEqualToString:@"繁體中文"]){
language = @"zh-Hant";
}else{
language = @"ar";
}
if ([language isEqualToString:@"ar"]) {
[UIView appearance].semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
[UISearchBar appearance].semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
} else {
[UIView appearance].semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
[UISearchBar appearance].semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
}
[NSBundle setLanguageText:language];
[[NSNotificationCenter defaultCenter] postNotificationName:@"kSwitchLanguage" object:language];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter]postNotificationName:@"kChanecNavView" object:language];
[self.navigationController popViewControllerAnimated:YES];
});
}
#pragma mark - UITableViewDelegate And UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return kGetScaleWidth(51);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.datasource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
XPMineSwitchLanguageCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPMineSwitchLanguageCell class])];
XPMineSettingItemModel * model = [self.datasource xpSafeObjectAtIndex:indexPath.row];
cell.model = model;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
XPMineSettingItemModel * model = [self.datasource xpSafeObjectAtIndex:indexPath.row] ;
for (XPMineSettingItemModel *obj in self.datasource) {
obj.isChoose = NO;
if ([model.title isEqualToString:obj.title]){
model.isChoose = YES;
}
}
self.chooseModel = model;
[self.tableView reloadData];
}
#pragma mark - 懒加载
- (NSArray<XPMineSettingItemModel *> *)datasource{
if(!_datasource){
XPMineSettingItemModel *enItme = [XPMineSettingItemModel new];
enItme.title = @"English";
XPMineSettingItemModel *zhItme = [XPMineSettingItemModel new];
zhItme.title = @"繁體中文";
XPMineSettingItemModel *arItme = [XPMineSettingItemModel new];
arItme.title = @"العربية";
NSString *language = [NSBundle getLanguageText];
if ([language isEqualToString:@"en"]){
enItme.isChoose = YES;
self.chooseModel = enItme;
}else if ([language isEqualToString:@"ar"]){
arItme.isChoose = YES;
self.chooseModel = arItme;
}else{
zhItme.isChoose = YES;
self.chooseModel = zhItme;
}
_datasource = @[enItme,zhItme,arItme];
}
return _datasource;
}
- (UIButton *)saveBtn {
if (!_saveBtn) {
_saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_saveBtn setFrame:CGRectMake(0, 0, 50, 30)];
[_saveBtn setTitle:YMLocalizedString(@"XPMineSwitchLanguageVC1") forState:UIControlStateNormal];
[_saveBtn setTitleColor:[DJDKMIMOMColor mainTextColor] forState:UIControlStateNormal];
_saveBtn.titleLabel.font = [UIFont systemFontOfSize:13];
[_saveBtn addTarget:self action:@selector(saveBtnAction) forControlEvents:UIControlEventTouchUpInside];
}
return _saveBtn;
}
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.showsVerticalScrollIndicator = NO;
_tableView.backgroundColor = [UIColor whiteColor];
_tableView.layer.cornerRadius = kGetScaleWidth(8);
_tableView.layer.masksToBounds = YES;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.backgroundColor = [UIColor clearColor];
[_tableView registerClass:[XPMineSwitchLanguageCell class] forCellReuseIdentifier:NSStringFromClass([XPMineSwitchLanguageCell class])];
}
return _tableView;
}
@end