FLEXFontListTableViewController.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #import "FLEXFontListTableViewController.h"
  2. #import "NSObject+FLEX_Reflection.h"
  3. @interface FLEXFontListTableViewController ()
  4. @property (nonatomic) NSArray *fonts;
  5. @end
  6. @implementation FLEXFontListTableViewController
  7. - (void)viewDidLoad {
  8. [super viewDidLoad];
  9. [self createAvailableFonts];
  10. //self.fonts = [self allFonts];
  11. [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseID"];
  12. // Uncomment the following line to preserve selection between presentations.
  13. // self.clearsSelectionOnViewWillAppear = NO;
  14. // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
  15. // self.navigationItem.rightBarButtonItem = self.editButtonItem;
  16. }
  17. - (void)viewWillAppear:(BOOL)animated {
  18. if ([self darkMode]){
  19. self.view.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.8];
  20. } else {
  21. self.view.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.8];
  22. }
  23. }
  24. - (void)didReceiveMemoryWarning {
  25. [super didReceiveMemoryWarning];
  26. // Dispose of any resources that can be recreated.
  27. }
  28. #pragma mark - Table view data source
  29. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  30. return 1;
  31. }
  32. - (void)createAvailableFonts {
  33. NSMutableArray<NSString *> *unsortedFontsArray = [NSMutableArray new];
  34. for (NSString *eachFontFamily in UIFont.familyNames) {
  35. for (NSString *eachFontName in [UIFont fontNamesForFamilyName:eachFontFamily]) {
  36. [unsortedFontsArray addObject:eachFontName];
  37. }
  38. }
  39. self.fonts = [NSMutableArray arrayWithArray:[unsortedFontsArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]];
  40. }
  41. - (NSArray *)allFonts
  42. {
  43. NSMutableArray *allFontArray = [[NSMutableArray alloc] init];
  44. NSArray *fontNames = [UIFont familyNames]; //all font family names
  45. for (NSString *fontFamily in fontNames) //cycle through
  46. {
  47. [allFontArray addObjectsFromArray:[UIFont fontNamesForFamilyName:fontFamily]]; //add all font names from the family names to the array
  48. }
  49. NSArray *sortedArray = [allFontArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; //sort alphabetically
  50. return sortedArray;
  51. }
  52. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  53. return [self fonts].count;
  54. }
  55. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  56. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseID" forIndexPath:indexPath];
  57. NSString *currentFont = [self fonts][indexPath.row];
  58. cell.textLabel.text = currentFont;
  59. cell.textLabel.font = [UIFont fontWithName:currentFont size:45];
  60. // Configure the cell...
  61. return cell;
  62. }
  63. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  64. NSString *currentFont = [self fonts][indexPath.row];
  65. if (self.itemSelectedBlock){
  66. self.itemSelectedBlock(currentFont);
  67. }
  68. }
  69. @end