Skip to content

Commit 8fee2a4

Browse files
Tyler FoxTyler Fox
authored andcommitted
Add full support for Dynamic Type
1 parent c9d9385 commit 8fee2a4

3 files changed

Lines changed: 39 additions & 14 deletions

File tree

TableViewCellWithAutoLayout/TableViewController/RJTableViewCell.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,6 @@
3333
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
3434
@property (strong, nonatomic) IBOutlet UILabel *bodyLabel;
3535

36+
- (void)updateFonts;
37+
3638
@end

TableViewCellWithAutoLayout/TableViewController/RJTableViewCell.m

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
@interface RJTableViewCell ()
3030

31-
@property (nonatomic, assign) BOOL hasSetupConstraints;
31+
@property (nonatomic, assign) BOOL didSetupConstraints;
3232

3333
@end
3434

@@ -44,7 +44,6 @@ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reus
4444
[self.titleLabel setLineBreakMode:NSLineBreakByTruncatingTail];
4545
[self.titleLabel setNumberOfLines:1];
4646
[self.titleLabel setTextAlignment:NSTextAlignmentLeft];
47-
[self.titleLabel setFont: [UIFont fontWithName:@"Helvetica-Bold" size:21]];
4847
[self.titleLabel setTextColor:[UIColor blackColor]];
4948
[self.titleLabel setBackgroundColor:[UIColor clearColor]];
5049

@@ -54,12 +53,13 @@ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reus
5453
[self.bodyLabel setLineBreakMode:NSLineBreakByTruncatingTail];
5554
[self.bodyLabel setNumberOfLines:0];
5655
[self.bodyLabel setTextAlignment:NSTextAlignmentLeft];
57-
[self.bodyLabel setFont: [UIFont fontWithName:@"Helvetica" size:14]];
5856
[self.bodyLabel setTextColor:[UIColor darkGrayColor]];
5957
[self.bodyLabel setBackgroundColor:[UIColor clearColor]];
6058

6159
[self.contentView addSubview:self.titleLabel];
6260
[self.contentView addSubview:self.bodyLabel];
61+
62+
[self updateFonts];
6363
}
6464

6565
return self;
@@ -68,7 +68,7 @@ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reus
6868
- (void)updateConstraints {
6969
[super updateConstraints];
7070

71-
if (self.hasSetupConstraints) return;
71+
if (self.didSetupConstraints) return;
7272

7373
[self.contentView addConstraint:[NSLayoutConstraint
7474
constraintWithItem:self.titleLabel
@@ -135,8 +135,13 @@ - (void)updateConstraints {
135135
multiplier:1.0f
136136
constant:-(kLabelHorizontalInsets / 2)]];
137137

138-
self.hasSetupConstraints = YES;
139-
138+
self.didSetupConstraints = YES;
139+
}
140+
141+
- (void)updateFonts
142+
{
143+
self.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
144+
self.bodyLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCaption2];
140145
}
141146

142147
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

TableViewCellWithAutoLayout/TableViewController/RJTableViewController.m

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,27 @@ - (id)initWithStyle:(UITableViewStyle)style
5151
- (void)viewDidLoad
5252
{
5353
[super viewDidLoad];
54+
5455
[self.tableView registerClass:[RJTableViewCell class] forCellReuseIdentifier:CellIdentifier];
56+
}
5557

56-
// Uncomment the following line to preserve selection between presentations.
57-
// self.clearsSelectionOnViewWillAppear = NO;
58-
59-
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
60-
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
58+
- (void)viewDidAppear:(BOOL)animated
59+
{
60+
[super viewDidAppear:animated];
61+
62+
[[NSNotificationCenter defaultCenter] addObserver:self
63+
selector:@selector(contentSizeCategoryChanged:)
64+
name:UIContentSizeCategoryDidChangeNotification
65+
object:nil];
66+
}
67+
68+
- (void)viewDidDisappear:(BOOL)animated
69+
{
70+
[super viewDidDisappear:animated];
6171

72+
[[NSNotificationCenter defaultCenter] removeObserver:self
73+
name:UIContentSizeCategoryDidChangeNotification
74+
object:nil];
6275
}
6376

6477
- (void)didReceiveMemoryWarning
@@ -67,6 +80,11 @@ - (void)didReceiveMemoryWarning
6780
// Dispose of any resources that can be recreated.
6881
}
6982

83+
- (void)contentSizeCategoryChanged:(NSNotification *)notification
84+
{
85+
[self.tableView reloadData];
86+
}
87+
7088
#pragma mark - Table view data source
7189

7290
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
@@ -86,7 +104,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
86104

87105
RJTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
88106

89-
// Configure the cell...
107+
[cell updateFonts];
90108

91109
NSDictionary *dataSourceItem = [self.model.dataSource objectAtIndex:indexPath.row];
92110

@@ -104,6 +122,8 @@ - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPa
104122

105123
RJTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
106124

125+
[cell updateFonts];
126+
107127
NSDictionary *dataSourceItem = [self.model.dataSource objectAtIndex:indexPath.row];
108128
cell.titleLabel.text = [dataSourceItem valueForKey:@"title"];
109129
cell.bodyLabel.text = [dataSourceItem valueForKey:@"body"];
@@ -122,9 +142,7 @@ - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPa
122142

123143
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
124144
{
125-
126145
return 500.0f;
127-
128146
}
129147

130148
/*

0 commit comments

Comments
 (0)