FontSizeViewController.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2012-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "FontSizeViewController.h"
  21. #import "UserSettings.h"
  22. @interface FontSizeViewController ()
  23. @end
  24. @implementation FontSizeViewController {
  25. NSIndexPath *selectedIndexPath;
  26. }
  27. static int fontSizes[] = {12, 14, 16, 18, 20, 24, 28, 30, 36};
  28. - (void)viewWillAppear:(BOOL)animated {
  29. [super viewWillAppear:animated];
  30. [self.tableView reloadData];
  31. }
  32. #pragma mark - Table view data source
  33. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  34. return 1;
  35. }
  36. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  37. return (sizeof(fontSizes) / sizeof(int) + 1);
  38. }
  39. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  40. UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"FontSizeCell"];
  41. if (indexPath.row == sizeof(fontSizes) / sizeof(int)) {
  42. UIFontDescriptor *fontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
  43. cell.textLabel.font = [UIFont systemFontOfSize:fontDescriptor.pointSize];
  44. cell.textLabel.text = NSLocalizedString(@"use_dynamic_font_size", @"");
  45. if ([[UserSettings sharedUserSettings] useDynamicFontSize]) {
  46. selectedIndexPath = indexPath;
  47. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  48. } else {
  49. cell.accessoryType = UITableViewCellAccessoryNone;
  50. }
  51. } else {
  52. cell.textLabel.font = [UIFont systemFontOfSize:fontSizes[indexPath.row]];
  53. cell.textLabel.text = [NSString stringWithFormat:@"%d %@", fontSizes[indexPath.row], NSLocalizedString(@"font_point", @"")];
  54. if (roundf([UserSettings sharedUserSettings].chatFontSize) == fontSizes[indexPath.row] && ![[UserSettings sharedUserSettings] useDynamicFontSize]) {
  55. selectedIndexPath = indexPath;
  56. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  57. } else {
  58. cell.accessoryType = UITableViewCellAccessoryNone;
  59. }
  60. }
  61. return cell;
  62. }
  63. #pragma mark - Table view delegate
  64. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  65. {
  66. if (indexPath.row == sizeof(fontSizes) / sizeof(int)) {
  67. [UserSettings sharedUserSettings].useDynamicFontSize = YES;
  68. } else {
  69. [UserSettings sharedUserSettings].chatFontSize = fontSizes[indexPath.row];
  70. [UserSettings sharedUserSettings].useDynamicFontSize = NO;
  71. }
  72. if (selectedIndexPath != nil)
  73. [self.tableView cellForRowAtIndexPath:selectedIndexPath].accessoryType = UITableViewCellAccessoryNone;
  74. [self.tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
  75. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  76. selectedIndexPath = indexPath;
  77. }
  78. @end