Commit 9fc4eb9c authored by yuzheng.tz's avatar yuzheng.tz

新增preferredLanguage属性,可以设置首选语言,优先于系统语言

parent bdb5b4fe
...@@ -23,19 +23,9 @@ ...@@ -23,19 +23,9 @@
} }
+ (NSString *)tz_localizedStringForKey:(NSString *)key value:(NSString *)value { + (NSString *)tz_localizedStringForKey:(NSString *)key value:(NSString *)value {
static NSBundle *bundle = nil; NSBundle *bundle = [TZImagePickerConfig sharedInstance].languageBundle;
if (bundle == nil) {
NSString *language = [NSLocale preferredLanguages].firstObject;
if ([language rangeOfString:@"zh-Hans"].location != NSNotFound) {
language = @"zh-Hans";
} else if ([language rangeOfString:@"zh-Hant"].location != NSNotFound) {
language = @"zh-Hant";
} else {
language = @"en";
}
bundle = [NSBundle bundleWithPath:[[NSBundle tz_imagePickerBundle] pathForResource:language ofType:@"lproj"]];
}
NSString *value1 = [bundle localizedStringForKey:key value:value table:nil]; NSString *value1 = [bundle localizedStringForKey:key value:value table:nil];
return value1; return value1;
} }
@end @end
...@@ -33,9 +33,7 @@ ...@@ -33,9 +33,7 @@
- (void)setResult:(id)result { - (void)setResult:(id)result {
_result = result; _result = result;
BOOL allowPickingImage = [[[NSUserDefaults standardUserDefaults] objectForKey:@"tz_allowPickingImage"] isEqualToString:@"1"]; [[TZImageManager manager] getAssetsFromFetchResult:result completion:^(NSArray<TZAssetModel *> *models) {
BOOL allowPickingVideo = [[[NSUserDefaults standardUserDefaults] objectForKey:@"tz_allowPickingVideo"] isEqualToString:@"1"];
[[TZImageManager manager] getAssetsFromFetchResult:result allowPickingVideo:allowPickingVideo allowPickingImage:allowPickingImage completion:^(NSArray<TZAssetModel *> *models) {
_models = models; _models = models;
if (_selectedModels) { if (_selectedModels) {
[self checkSelectedModels]; [self checkSelectedModels];
......
...@@ -54,6 +54,7 @@ ...@@ -54,6 +54,7 @@
- (void)getAllAlbums:(BOOL)allowPickingVideo allowPickingImage:(BOOL)allowPickingImage completion:(void (^)(NSArray<TZAlbumModel *> *models))completion; - (void)getAllAlbums:(BOOL)allowPickingVideo allowPickingImage:(BOOL)allowPickingImage completion:(void (^)(NSArray<TZAlbumModel *> *models))completion;
/// Get Assets 获得Asset数组 /// Get Assets 获得Asset数组
- (void)getAssetsFromFetchResult:(id)result completion:(void (^)(NSArray<TZAssetModel *> *models))completion;
- (void)getAssetsFromFetchResult:(id)result allowPickingVideo:(BOOL)allowPickingVideo allowPickingImage:(BOOL)allowPickingImage completion:(void (^)(NSArray<TZAssetModel *> *models))completion; - (void)getAssetsFromFetchResult:(id)result allowPickingVideo:(BOOL)allowPickingVideo allowPickingImage:(BOOL)allowPickingImage completion:(void (^)(NSArray<TZAssetModel *> *models))completion;
- (void)getAssetFromFetchResult:(id)result atIndex:(NSInteger)index allowPickingVideo:(BOOL)allowPickingVideo allowPickingImage:(BOOL)allowPickingImage completion:(void (^)(TZAssetModel *model))completion; - (void)getAssetFromFetchResult:(id)result atIndex:(NSInteger)index allowPickingVideo:(BOOL)allowPickingVideo allowPickingImage:(BOOL)allowPickingImage completion:(void (^)(TZAssetModel *model))completion;
......
...@@ -229,6 +229,11 @@ static dispatch_once_t onceToken; ...@@ -229,6 +229,11 @@ static dispatch_once_t onceToken;
#pragma mark - Get Assets #pragma mark - Get Assets
/// Get Assets 获得照片数组 /// Get Assets 获得照片数组
- (void)getAssetsFromFetchResult:(id)result completion:(void (^)(NSArray<TZAssetModel *> *))completion {
TZImagePickerConfig *config = [TZImagePickerConfig sharedInstance];
return [self getAssetsFromFetchResult:result allowPickingVideo:config.allowPickingVideo allowPickingImage:config.allowPickingImage completion:completion];
}
- (void)getAssetsFromFetchResult:(id)result allowPickingVideo:(BOOL)allowPickingVideo allowPickingImage:(BOOL)allowPickingImage completion:(void (^)(NSArray<TZAssetModel *> *))completion { - (void)getAssetsFromFetchResult:(id)result allowPickingVideo:(BOOL)allowPickingVideo allowPickingImage:(BOOL)allowPickingImage completion:(void (^)(NSArray<TZAssetModel *> *))completion {
NSMutableArray *photoArr = [NSMutableArray array]; NSMutableArray *photoArr = [NSMutableArray array];
if ([result isKindOfClass:[PHFetchResult class]]) { if ([result isKindOfClass:[PHFetchResult class]]) {
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
@protocol TZImagePickerControllerDelegate; @protocol TZImagePickerControllerDelegate;
@interface TZImagePickerController : UINavigationController @interface TZImagePickerController : UINavigationController
#pragma mark -
/// Use this init method / 用这个初始化方法 /// Use this init method / 用这个初始化方法
- (instancetype)initWithMaxImagesCount:(NSInteger)maxImagesCount delegate:(id<TZImagePickerControllerDelegate>)delegate; - (instancetype)initWithMaxImagesCount:(NSInteger)maxImagesCount delegate:(id<TZImagePickerControllerDelegate>)delegate;
- (instancetype)initWithMaxImagesCount:(NSInteger)maxImagesCount columnNumber:(NSInteger)columnNumber delegate:(id<TZImagePickerControllerDelegate>)delegate; - (instancetype)initWithMaxImagesCount:(NSInteger)maxImagesCount columnNumber:(NSInteger)columnNumber delegate:(id<TZImagePickerControllerDelegate>)delegate;
...@@ -37,6 +38,7 @@ ...@@ -37,6 +38,7 @@
/// This init method for crop photo / 用这个初始化方法以裁剪图片 /// This init method for crop photo / 用这个初始化方法以裁剪图片
- (instancetype)initCropTypeWithAsset:(id)asset photo:(UIImage *)photo completion:(void (^)(UIImage *cropImage,id asset))completion; - (instancetype)initCropTypeWithAsset:(id)asset photo:(UIImage *)photo completion:(void (^)(UIImage *cropImage,id asset))completion;
#pragma mark -
/// Default is 9 / 默认最大可选9张图片 /// Default is 9 / 默认最大可选9张图片
@property (nonatomic, assign) NSInteger maxImagesCount; @property (nonatomic, assign) NSInteger maxImagesCount;
...@@ -84,6 +86,10 @@ ...@@ -84,6 +86,10 @@
/// 默认为YES,如果设置为NO,拍照按钮将隐藏,用户将不能选择照片 /// 默认为YES,如果设置为NO,拍照按钮将隐藏,用户将不能选择照片
@property(nonatomic, assign) BOOL allowTakePicture; @property(nonatomic, assign) BOOL allowTakePicture;
/// 首选语言,如果设置了就用该语言,不设则取当前系统语言。
/// 由于目前只支持中文、繁体中文、英文。故该属性只支持zh-Hans、zh-Hant、en三种值,其余值无效。
@property (copy, nonatomic) NSString *preferredLanguage;
/// Default is YES, if set NO, user can't preview photo. /// Default is YES, if set NO, user can't preview photo.
/// 默认为YES,如果设置为NO,预览按钮将隐藏,用户将不能去预览照片 /// 默认为YES,如果设置为NO,预览按钮将隐藏,用户将不能去预览照片
@property (nonatomic, assign) BOOL allowPreview; @property (nonatomic, assign) BOOL allowPreview;
...@@ -108,6 +114,8 @@ ...@@ -108,6 +114,8 @@
@property (nonatomic, assign) BOOL isStatusBarDefault __attribute__((deprecated("Use -statusBarStyle."))); @property (nonatomic, assign) BOOL isStatusBarDefault __attribute__((deprecated("Use -statusBarStyle.")));
/// statusBar的样式,默认为UIStatusBarStyleLightContent /// statusBar的样式,默认为UIStatusBarStyleLightContent
@property (assign, nonatomic) UIStatusBarStyle statusBarStyle; @property (assign, nonatomic) UIStatusBarStyle statusBarStyle;
#pragma mark -
/// Single selection mode, valid when maxImagesCount = 1 /// Single selection mode, valid when maxImagesCount = 1
/// 单选模式,maxImagesCount为1时才生效 /// 单选模式,maxImagesCount为1时才生效
@property (nonatomic, assign) BOOL showSelectBtn; ///< 在单选模式下,照片列表页中,显示选择按钮,默认为NO @property (nonatomic, assign) BOOL showSelectBtn; ///< 在单选模式下,照片列表页中,显示选择按钮,默认为NO
...@@ -121,6 +129,7 @@ ...@@ -121,6 +129,7 @@
@property (nonatomic, copy) void (^navLeftBarButtonSettingBlock)(UIButton *leftButton); ///< 自定义返回按钮样式及其属性 @property (nonatomic, copy) void (^navLeftBarButtonSettingBlock)(UIButton *leftButton); ///< 自定义返回按钮样式及其属性
#pragma mark -
- (id)showAlertWithTitle:(NSString *)title; - (id)showAlertWithTitle:(NSString *)title;
- (void)hideAlertView:(id)alertView; - (void)hideAlertView:(id)alertView;
- (void)showProgressHUD; - (void)showProgressHUD;
...@@ -128,6 +137,7 @@ ...@@ -128,6 +137,7 @@
@property (nonatomic, assign) BOOL isSelectOriginalPhoto; @property (nonatomic, assign) BOOL isSelectOriginalPhoto;
@property (assign, nonatomic) BOOL needShowStatusBar; @property (assign, nonatomic) BOOL needShowStatusBar;
#pragma mark -
@property (nonatomic, copy) NSString *takePictureImageName; @property (nonatomic, copy) NSString *takePictureImageName;
@property (nonatomic, copy) NSString *photoSelImageName; @property (nonatomic, copy) NSString *photoSelImageName;
@property (nonatomic, copy) NSString *photoDefImageName; @property (nonatomic, copy) NSString *photoDefImageName;
...@@ -136,6 +146,7 @@ ...@@ -136,6 +146,7 @@
@property (nonatomic, copy) NSString *photoPreviewOriginDefImageName; @property (nonatomic, copy) NSString *photoPreviewOriginDefImageName;
@property (nonatomic, copy) NSString *photoNumberIconImageName; @property (nonatomic, copy) NSString *photoNumberIconImageName;
#pragma mark -
/// Appearance / 外观颜色 + 按钮文字 /// Appearance / 外观颜色 + 按钮文字
@property (nonatomic, strong) UIColor *oKButtonTitleColorNormal; @property (nonatomic, strong) UIColor *oKButtonTitleColorNormal;
@property (nonatomic, strong) UIColor *oKButtonTitleColorDisabled; @property (nonatomic, strong) UIColor *oKButtonTitleColorDisabled;
...@@ -152,7 +163,7 @@ ...@@ -152,7 +163,7 @@
@property (nonatomic, copy) NSString *settingBtnTitleStr; @property (nonatomic, copy) NSString *settingBtnTitleStr;
@property (nonatomic, copy) NSString *processHintStr; @property (nonatomic, copy) NSString *processHintStr;
/// Public Method #pragma mark -
- (void)cancelButtonClick; - (void)cancelButtonClick;
// The picker should dismiss itself; when it dismissed these handle will be called. // The picker should dismiss itself; when it dismissed these handle will be called.
...@@ -243,3 +254,14 @@ ...@@ -243,3 +254,14 @@
+ (CGFloat)tz_statusBarHeight; + (CGFloat)tz_statusBarHeight;
@end @end
@interface TZImagePickerConfig : NSObject
+ (instancetype)sharedInstance;
@property (copy, nonatomic) NSString *preferredLanguage;
@property(nonatomic, assign) BOOL allowPickingImage;
@property (nonatomic, assign) BOOL allowPickingVideo;
/// 语言bundle,preferredLanguage变化时languageBundle会变化
/// 可通过设置bundle,让选择器支持更多的语言。欢迎提交PR把语言文件提交上来~
@property (strong, nonatomic) NSBundle *languageBundle;
@end
...@@ -495,16 +495,18 @@ ...@@ -495,16 +495,18 @@
- (void)setAllowPickingImage:(BOOL)allowPickingImage { - (void)setAllowPickingImage:(BOOL)allowPickingImage {
_allowPickingImage = allowPickingImage; _allowPickingImage = allowPickingImage;
NSString *allowPickingImageStr = _allowPickingImage ? @"1" : @"0"; [TZImagePickerConfig sharedInstance].allowPickingImage = allowPickingImage;
[[NSUserDefaults standardUserDefaults] setObject:allowPickingImageStr forKey:@"tz_allowPickingImage"];
[[NSUserDefaults standardUserDefaults] synchronize];
} }
- (void)setAllowPickingVideo:(BOOL)allowPickingVideo { - (void)setAllowPickingVideo:(BOOL)allowPickingVideo {
_allowPickingVideo = allowPickingVideo; _allowPickingVideo = allowPickingVideo;
NSString *allowPickingVideoStr = _allowPickingVideo ? @"1" : @"0"; [TZImagePickerConfig sharedInstance].allowPickingVideo = allowPickingVideo;
[[NSUserDefaults standardUserDefaults] setObject:allowPickingVideoStr forKey:@"tz_allowPickingVideo"]; }
[[NSUserDefaults standardUserDefaults] synchronize];
- (void)setPreferredLanguage:(NSString *)preferredLanguage {
_preferredLanguage = preferredLanguage;
[TZImagePickerConfig sharedInstance].preferredLanguage = preferredLanguage;
[self configDefaultBtnTitle];
} }
- (void)setSortAscendingByModificationDate:(BOOL)sortAscendingByModificationDate { - (void)setSortAscendingByModificationDate:(BOOL)sortAscendingByModificationDate {
...@@ -767,3 +769,36 @@ ...@@ -767,3 +769,36 @@
} }
@end @end
@implementation TZImagePickerConfig
+ (instancetype)sharedInstance {
static dispatch_once_t onceToken;
static TZImagePickerConfig *config = nil;
dispatch_once(&onceToken, ^{
if (config == nil) {
config = [[TZImagePickerConfig alloc] init];
config.preferredLanguage = nil;
}
});
return config;
}
- (void)setPreferredLanguage:(NSString *)preferredLanguage {
_preferredLanguage = preferredLanguage;
if (!preferredLanguage || !preferredLanguage.length) {
preferredLanguage = [NSLocale preferredLanguages].firstObject;
}
if ([preferredLanguage rangeOfString:@"zh-Hans"].location != NSNotFound) {
preferredLanguage = @"zh-Hans";
} else if ([preferredLanguage rangeOfString:@"zh-Hant"].location != NSNotFound) {
preferredLanguage = @"zh-Hant";
} else {
preferredLanguage = @"en";
}
_languageBundle = [NSBundle bundleWithPath:[[NSBundle tz_imagePickerBundle] pathForResource:preferredLanguage ofType:@"lproj"]];
}
@end
...@@ -105,7 +105,7 @@ static CGFloat itemMargin = 5; ...@@ -105,7 +105,7 @@ static CGFloat itemMargin = 5;
}]; }];
} else { } else {
if (_showTakePhotoBtn || !iOS8Later || _isFirstAppear) { if (_showTakePhotoBtn || !iOS8Later || _isFirstAppear) {
[[TZImageManager manager] getAssetsFromFetchResult:_model.result allowPickingVideo:tzImagePickerVc.allowPickingVideo allowPickingImage:tzImagePickerVc.allowPickingImage completion:^(NSArray<TZAssetModel *> *models) { [[TZImageManager manager] getAssetsFromFetchResult:_model.result completion:^(NSArray<TZAssetModel *> *models) {
_models = [NSMutableArray arrayWithArray:models]; _models = [NSMutableArray arrayWithArray:models];
[self initSubviews]; [self initSubviews];
}]; }];
...@@ -725,7 +725,7 @@ static CGFloat itemMargin = 5; ...@@ -725,7 +725,7 @@ static CGFloat itemMargin = 5;
TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController; TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
[[TZImageManager manager] getCameraRollAlbum:tzImagePickerVc.allowPickingVideo allowPickingImage:tzImagePickerVc.allowPickingImage completion:^(TZAlbumModel *model) { [[TZImageManager manager] getCameraRollAlbum:tzImagePickerVc.allowPickingVideo allowPickingImage:tzImagePickerVc.allowPickingImage completion:^(TZAlbumModel *model) {
_model = model; _model = model;
[[TZImageManager manager] getAssetsFromFetchResult:_model.result allowPickingVideo:tzImagePickerVc.allowPickingVideo allowPickingImage:tzImagePickerVc.allowPickingImage completion:^(NSArray<TZAssetModel *> *models) { [[TZImageManager manager] getAssetsFromFetchResult:_model.result completion:^(NSArray<TZAssetModel *> *models) {
[tzImagePickerVc hideProgressHUD]; [tzImagePickerVc hideProgressHUD];
TZAssetModel *assetModel; TZAssetModel *assetModel;
......
...@@ -292,9 +292,11 @@ ...@@ -292,9 +292,11 @@
// Deprecated, Use statusBarStyle // Deprecated, Use statusBarStyle
// imagePickerVc.isStatusBarDefault = NO; // imagePickerVc.isStatusBarDefault = NO;
imagePickerVc.statusBarStyle = UIStatusBarStyleLightContent; imagePickerVc.statusBarStyle = UIStatusBarStyleLightContent;
// 设置首选语言
imagePickerVc.preferredLanguage = @"zh-Hans";
#pragma mark - 到这里为止 #pragma mark - 到这里为止
// You can get the photos by block, the same as by delegate. // You can get the photos by block, the same as by delegate.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment