XYCountryCodeViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. //
  2. // XYCountryCodeViewController.m
  3. // XYCountryCode
  4. //
  5. // Created by 杨卢银 on 2018/8/16.
  6. // Copyright © 2018年 杨卢银. All rights reserved.
  7. //
  8. #import "XYCountryCodeViewController.h"
  9. #import "XYCountryCodeUtils.h"
  10. #import "XYColor+UIColor.h"
  11. @interface XYCountryCodeViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,UIPickerViewDataSource,UIPickerViewDelegate>
  12. {
  13. NSInteger pickerSelectIndex;
  14. }
  15. @property (strong , nonatomic)UITableView *tableView;
  16. @property (strong , nonatomic)NSArray *dataArray;
  17. @property (strong , nonatomic)UISearchBar *searchBar;
  18. @property (strong , nonatomic)NSArray *data;
  19. @property (strong , nonatomic)NSArray *searchArray;
  20. @property (strong , nonatomic)UIPickerView *pickerView;
  21. @property (strong , nonatomic)UIView *pickerBGView;
  22. @property (strong , nonatomic)UIImageView *pickerBGImageView;
  23. @property (strong , nonatomic)UIButton *pickerBGCloseBT;
  24. @property (strong , nonatomic)UILabel *pickerTitleLabel;
  25. @property (strong , nonatomic)UIButton *pickerBGDemoBT;
  26. @end
  27. @implementation XYCountryCodeViewController
  28. -(instancetype)initWithShowType:(XYCountryCodeShowType)aType{
  29. self = [super init];
  30. if (self) {
  31. self.type = aType;
  32. self.tintColor = UIColor.blackColor;
  33. self.cornerRadius = 5;
  34. if(self.textHighlightColor==nil){
  35. //默认颜色
  36. self.textHighlightColor = [UIColor colorWithRed:64.0/255.0 green:181.0/255.0 blue:132.0/255.0 alpha:1.0];
  37. }
  38. }
  39. return self;
  40. }
  41. -(void)showViewController:(UIViewController *)vc{
  42. if (_type==XYCountryCodeShowTypePicker) {
  43. //_showViewController= viewcontroller;
  44. self.view.backgroundColor = [UIColor clearColor];
  45. self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
  46. [vc presentViewController:self animated:NO completion:^{
  47. }];
  48. }else{
  49. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self];
  50. if (@available(iOS 15.0, *)) {
  51. UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
  52. appearance.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular];
  53. appearance.backgroundColor = kWhiteColor;
  54. nav.navigationBar.scrollEdgeAppearance = appearance;
  55. }
  56. nav.modalPresentationStyle = 0;
  57. [vc presentViewController:nav animated:YES completion:^{
  58. }];
  59. }
  60. }
  61. - (void)viewDidLoad {
  62. [super viewDidLoad];
  63. // Do any additional setup after loading the view.
  64. [self setup];
  65. if (_type==XYCountryCodeShowTypePicker) {
  66. CGRect rect = self.pickerBGView.frame;
  67. rect.origin.y = [UIScreen mainScreen].bounds.size.height;
  68. self.pickerBGView.frame = rect;
  69. self.pickerBGImageView.alpha = 0.0;
  70. pickerSelectIndex= 0;
  71. }
  72. }
  73. -(void)viewDidAppear:(BOOL)animated{
  74. [super viewDidAppear:animated];
  75. if (_type == XYCountryCodeShowTypePicker) {
  76. [UIView animateWithDuration:0.3 animations:^{
  77. CGRect rect = self.pickerBGView.frame;
  78. rect.origin.y = [UIScreen mainScreen].bounds.size.height - rect.size.height;
  79. self.pickerBGView.frame = rect;
  80. self.pickerBGImageView.alpha = 0.35;
  81. } completion:^(BOOL finished) {
  82. [self.pickerView reloadAllComponents];
  83. }];
  84. }
  85. }
  86. - (void)dismiss{
  87. if(_type == XYCountryCodeShowTypeNone){
  88. [self.navigationController dismissViewControllerAnimated:YES completion:^{
  89. }];
  90. }else{
  91. self.pickerBGImageView.alpha = 0.0;
  92. [self dismissViewControllerAnimated:YES completion:^{
  93. }];
  94. }
  95. }
  96. - (void)buildData{
  97. self.data = [[XYCountryCodeUtils shareUtils] countryArray];
  98. self.dataArray = [[XYCountryCodeUtils shareUtils] managers];
  99. }
  100. - (void)setup{
  101. [self buildData];
  102. if(_type == XYCountryCodeShowTypeNone){
  103. [self.view addSubview:self.tableView];
  104. self.tableView.dataSource = self;
  105. self.tableView.delegate = self;
  106. self.tableView.tableFooterView = [[UIView alloc] init];
  107. CGRect rect = [UIScreen mainScreen].bounds;
  108. UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, rect.size.width, 56)];
  109. searchBar.placeholder = ASLocalizedString(@"输入关键字");
  110. searchBar.delegate = self;
  111. self.tableView.tableHeaderView = searchBar;
  112. _searchBar = searchBar;
  113. }else if (_type == XYCountryCodeShowTypePicker){
  114. [self.view addSubview:self.pickerBGView];
  115. self.pickerView.delegate = self;
  116. self.pickerView.dataSource = self;
  117. }
  118. //xy_close
  119. NSString *bundlePath = [[NSBundle mainBundle]pathForResource:@"XYCountryCode"ofType:@"bundle"];
  120. NSBundle *resourceBundle = [NSBundle bundleWithPath:bundlePath];
  121. NSString *path = [resourceBundle pathForResource:@"xy_close" ofType:@"png"];
  122. if (_type==XYCountryCodeShowTypeNone) {
  123. path = [resourceBundle pathForResource:@"xy_close" ofType:@"png"];
  124. }
  125. UIImage *leftImag = [UIImage imageWithContentsOfFile:path];
  126. UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImag style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemSelect:)];
  127. self.navigationItem.leftBarButtonItem = leftBarButtonItem;
  128. UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:ASLocalizedString(@"确定") style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemSelect:)];
  129. self.navigationItem.rightBarButtonItem = rightBarButtonItem;
  130. self.navigationController.navigationBar.tintColor = self.tintColor;
  131. }
  132. - (void)leftBarButtonItemSelect:(UIBarButtonItem*)sender{
  133. [self dismiss];
  134. }
  135. - (void)rightBarButtonItemSelect:(UIBarButtonItem*)sender{
  136. }
  137. - (void)didReceiveMemoryWarning {
  138. [super didReceiveMemoryWarning];
  139. // Dispose of any resources that can be recreated.
  140. }
  141. -(void)demoCheck:(UIButton*)sender{
  142. XYCountry *c = _data[pickerSelectIndex];
  143. if (self.delegate && [self.delegate respondsToSelector:@selector(countryCodeViewController:chooseCode:)]) {
  144. [self.delegate countryCodeViewController:self chooseCode:c.code];
  145. }
  146. if (_chooseCodeRespose) {
  147. _chooseCodeRespose(c.code);
  148. }
  149. [self dismiss];
  150. }
  151. #pragma mark set
  152. -(UIImage*)closeBTImage{
  153. NSString *bundlePath = [[NSBundle mainBundle]pathForResource:@"XYCountryCode"ofType:@"bundle"];
  154. NSBundle *resourceBundle = [NSBundle bundleWithPath:bundlePath];
  155. NSString *path = [resourceBundle pathForResource:@"xy_close" ofType:@"png"];
  156. UIImage *leftImag = [UIImage imageWithContentsOfFile:path];
  157. return leftImag;
  158. }
  159. -(UIView *)pickerBGView{
  160. if (!_pickerBGView) {
  161. CGRect rect = [UIScreen mainScreen].bounds;
  162. _pickerBGImageView = [[UIImageView alloc] initWithFrame:rect];
  163. _pickerBGImageView.backgroundColor = [UIColor blackColor];
  164. _pickerBGImageView.alpha =0.35;
  165. [self.view addSubview:_pickerBGImageView];
  166. _pickerBGView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, rect.size.width, 260)];
  167. UIView *topmenuView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, rect.size.width, 44)];
  168. if (@available(iOS 13.0, *)) {
  169. topmenuView.backgroundColor = [UIColor systemBackgroundColor];
  170. } else {
  171. // Fallback on earlier versions
  172. topmenuView.backgroundColor = [UIColor whiteColor];
  173. }
  174. [_pickerBGView addSubview:topmenuView];
  175. _pickerBGCloseBT = [UIButton buttonWithType:UIButtonTypeCustom];
  176. _pickerBGCloseBT.frame = CGRectMake(10, 2, 40, 40);
  177. [_pickerBGCloseBT setImage:[self closeBTImage] forState:UIControlStateNormal];
  178. [_pickerBGCloseBT addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
  179. [topmenuView addSubview:_pickerBGCloseBT];
  180. _pickerTitleLabel = [[UILabel alloc] init];
  181. _pickerTitleLabel.text = self.title;
  182. [topmenuView addSubview:_pickerTitleLabel];
  183. [_pickerTitleLabel sizeToFit];
  184. _pickerTitleLabel.center = topmenuView.center;
  185. _pickerBGDemoBT = [UIButton buttonWithType:UIButtonTypeCustom];
  186. _pickerBGDemoBT.frame = CGRectMake(rect.size.width - 10 - 40, 2, 40, 40);
  187. [_pickerBGDemoBT setTitle:ASLocalizedString(@"确定") forState:UIControlStateNormal];
  188. if (@available(iOS 13.0, *)) {
  189. [_pickerBGDemoBT setTitleColor:[UIColor systemGrayColor] forState:UIControlStateNormal];
  190. } else {
  191. // Fallback on earlier versions
  192. [_pickerBGDemoBT setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  193. }
  194. [_pickerBGDemoBT addTarget:self action:@selector(demoCheck:) forControlEvents:UIControlEventTouchUpInside];
  195. [topmenuView addSubview:_pickerBGDemoBT];
  196. self.pickerView.frame = CGRectMake(0, 44, rect.size.width, _pickerBGView.bounds.size.height - topmenuView.frame.size.height);
  197. self.pickerView.backgroundColor = [UIColor clearColor];
  198. if (@available(iOS 13.0, *)) {
  199. self.pickerView.backgroundColor = [UIColor systemBackgroundColor];
  200. } else {
  201. // Fallback on earlier versions
  202. self.pickerView.backgroundColor = [UIColor whiteColor];
  203. }
  204. [_pickerBGView addSubview:self.pickerView];
  205. }
  206. return _pickerBGView;
  207. }
  208. -(UIPickerView *)pickerView{
  209. if (!_pickerView) {
  210. _pickerView = [[UIPickerView alloc] initWithFrame:CGRectZero];
  211. _pickerView.backgroundColor = [UIColor colorWithRed:239.0/255.0 green:239.0/255.0 blue:239.0/255.0 alpha:1.0];
  212. }
  213. return _pickerView;
  214. }
  215. -(UITableView *)tableView{
  216. if (!_tableView) {
  217. CGRect mainscreen = [UIScreen mainScreen].bounds;
  218. _tableView = [[UITableView alloc] initWithFrame:mainscreen style:UITableViewStylePlain];
  219. }
  220. return _tableView;
  221. }
  222. /*
  223. #pragma mark - Navigation
  224. // In a storyboard-based application, you will often want to do a little preparation before navigation
  225. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  226. // Get the new view controller using [segue destinationViewController].
  227. // Pass the selected object to the new view controller.
  228. }
  229. */
  230. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  231. if (_searchBar.text.length>0) {
  232. return self.searchArray.count;
  233. }
  234. return self.dataArray.count;
  235. }
  236. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  237. XYCountryManager *m;
  238. if (_searchBar.text.length>0) {
  239. m = self.searchArray[section];
  240. }else{
  241. m = self.dataArray[section];
  242. }
  243. return m.countrys.count;
  244. }
  245. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  246. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
  247. if (!cell) {
  248. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"];
  249. cell.imageView.layer.cornerRadius = self.cornerRadius;
  250. cell.imageView.layer.masksToBounds = YES;
  251. }
  252. XYCountryManager *m;
  253. if (self.searchBar.text.length==0) {
  254. m = self.dataArray[indexPath.section];
  255. }else{
  256. m = self.searchArray[indexPath.section];
  257. }
  258. XYCountry *c = m.countrys[indexPath.row];
  259. if (_searchBar.text.length>0) {
  260. // 原始搜索结果字符串.
  261. NSString *originResult1 = c.name;
  262. NSString *originResult2 = [@"+ " stringByAppendingString:c.code];
  263. // 获取关键字的位置
  264. NSRange range1 = [originResult1 rangeOfString:self.searchBar.text];
  265. NSRange range2 = [originResult2 rangeOfString:self.searchBar.text];
  266. // 转换成可以操作的字符串类型.
  267. NSMutableAttributedString *attribute1 = [[NSMutableAttributedString alloc] initWithString:originResult1];
  268. NSMutableAttributedString *attribute2 = [[NSMutableAttributedString alloc] initWithString:originResult2];
  269. // 添加属性(粗体)
  270. [attribute1 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:range1];
  271. [attribute2 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:range2];
  272. // 关键字高亮
  273. [attribute1 addAttribute:NSForegroundColorAttributeName value:self.textHighlightColor range:range1];
  274. [attribute2 addAttribute:NSForegroundColorAttributeName value:self.textHighlightColor range:range2];
  275. // 将带属性的字符串添加到cell.textLabel上.
  276. [cell.textLabel setAttributedText:attribute1];
  277. [cell.detailTextLabel setAttributedText:attribute2];
  278. }
  279. cell.textLabel.text = c.name;
  280. cell.detailTextLabel.text = [@"+ " stringByAppendingString:c.code];
  281. cell.imageView.image = c.image;
  282. return cell;
  283. }
  284. -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
  285. XYCountryManager *m;
  286. if (self.searchBar.text.length==0) {
  287. m = self.dataArray[section];
  288. }else{
  289. m = self.searchArray[section];
  290. }
  291. return m.title;
  292. }
  293. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  294. XYCountryManager *m ;
  295. if (self.searchBar.text.length==0) {
  296. m = self.dataArray[indexPath.section];
  297. }else{
  298. m = self.searchArray[indexPath.section];
  299. }
  300. XYCountry *c = m.countrys[indexPath.row];
  301. if (self.delegate && [self.delegate respondsToSelector:@selector(countryCodeViewController:chooseCode:)]) {
  302. [self.delegate countryCodeViewController:self chooseCode:c.code];
  303. }
  304. if (_chooseCodeRespose) {
  305. _chooseCodeRespose(c.code);
  306. }
  307. [self dismiss];
  308. }
  309. #pragma mark UIPickerViewDataSource
  310. -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
  311. return 1;
  312. }
  313. -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
  314. return _data.count;
  315. }
  316. -(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
  317. if(!view){
  318. view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
  319. }
  320. XYCountry *c = _data[row];
  321. UIImageView *imageView = [view viewWithTag:111];
  322. if (!imageView) {
  323. imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 7, 50, 30)];
  324. imageView.tag = 111;
  325. imageView.layer.cornerRadius = self.cornerRadius;
  326. imageView.layer.masksToBounds = YES;
  327. [view addSubview:imageView];
  328. }
  329. UILabel *label = [view viewWithTag:112];
  330. if (!label) {
  331. label = [[UILabel alloc] initWithFrame:CGRectMake(90, 7, 150, 30)];
  332. label.tag = 112;
  333. label.textColor = [UIColor xy_textColor];
  334. label.font = [UIFont systemFontOfSize:15.0];
  335. [view addSubview:label];
  336. }
  337. UILabel *sublabel = [view viewWithTag:113];
  338. if (!sublabel) {
  339. sublabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-10-100, 7, 100, 30)];
  340. sublabel.tag = 113;
  341. sublabel.textColor = [UIColor xy_textSubColor];
  342. sublabel.font = [UIFont systemFontOfSize:15.0];
  343. sublabel.textAlignment = NSTextAlignmentRight;
  344. [view addSubview:sublabel];
  345. }
  346. imageView.image = c.image;
  347. label.text = c.name;
  348. sublabel.text = [NSString stringWithFormat:@"+ %@",c.code];
  349. return view;
  350. }
  351. -(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
  352. return 44;
  353. }
  354. -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
  355. pickerSelectIndex = row;
  356. }
  357. #pragma mark serachBarDeleagte
  358. -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
  359. NSString *searchTextString = searchBar.text;
  360. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"zh contains[cd] %@ OR tw contains[cd] %@ OR en contains[cd] %@ OR code contains[cd] %@",searchTextString,searchTextString,searchTextString,searchTextString];
  361. NSMutableArray *list = [[NSMutableArray alloc] initWithArray:[_data filteredArrayUsingPredicate:predicate]];
  362. self.searchArray = [[XYCountryCodeUtils shareUtils] buildManagers:list showType:XYCountryShowTypeZH];
  363. [_tableView reloadData];
  364. }
  365. @end