Last active
December 24, 2015 07:29
-
-
Save quesera2/6763691 to your computer and use it in GitHub Desktop.
NSAttributedText&Dynamic Type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// OS7MainViewController.m | |
// iOS7Test | |
// | |
// Copyright (c) 2013年 glayash. All rights reserved. | |
// | |
#import "OS7MainViewController.h" | |
static NSString * const CellIdentifier = @"MainViewCell"; | |
static const CGFloat PADDING = 16; | |
static const CGFloat TABLEVIEWCELL_MARGIN = 40; | |
@interface OS7MainViewController () | |
@property (nonatomic) NSArray *tableTexts; | |
@property (nonatomic) NSString *detailText; | |
@property (nonatomic) NSDictionary *bodyAttribute; | |
@property (nonatomic) NSDictionary *subTitleAttribute; | |
@property (weak, nonatomic) IBOutlet UITableView *tableView; | |
@end | |
@interface CustomCell : UITableViewCell | |
@end | |
@implementation OS7MainViewController | |
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil | |
{ | |
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; | |
if (self) { | |
// Custom initialization | |
} | |
return self; | |
} | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view from its nib. | |
self.title = @"メイン"; | |
[self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:CellIdentifier]; | |
self.tableTexts = @[@"りんごは浮かんだお空に…\nりんごは落っこちた地べたに…, | |
@"星が生まれて\n歌が生まれて\nルルアメルは笑った\n常しえと", | |
@"星がキスして\n歌が眠って\かえるとこは どこでしょう…?\nかえるとこは どこでしょう…?", | |
@"りんごは落っこちた地べたに…\nりんごは浮かんだお空に…"]; | |
self.detailText = @"Apple (Symphogear)"; | |
[self _refreshFontAttributes]; | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(preferredContentSizeChanged:) name:UIContentSizeCategoryDidChangeNotification object:nil]; | |
} | |
/** | |
フォントサイズ変更の通知を受け取った際に、 | |
フォント属性を更新した上で、テーブルを再描画する | |
*/ | |
- (void)preferredContentSizeChanged:(NSNotification *)aNotification { | |
[self _refreshFontAttributes]; | |
[self.tableView reloadData]; | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
[[NSNotificationCenter defaultCenter] removeObserver:self]; | |
} | |
#pragma mark - UITableView Delegate & DataSource | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section | |
{ | |
return [self.tableTexts count]; | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; | |
[self _updateCell:cell atIndexPath:indexPath]; | |
return cell; | |
} | |
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
CGFloat cellWidth = self.tableView.bounds.size.width - TABLEVIEWCELL_MARGIN; | |
CGFloat mainTextHeight = [self.tableTexts[indexPath.row] boundingRectWithSize:CGSizeMake(cellWidth, CGFLOAT_MAX) | |
options:NSStringDrawingUsesLineFragmentOrigin | |
attributes:self.bodyAttribute | |
context:nil].size.height; | |
CGFloat detailTextHeight = [self.detailText boundingRectWithSize:CGSizeMake(cellWidth, CGFLOAT_MAX) | |
options:NSStringDrawingUsesLineFragmentOrigin | |
attributes:self.subTitleAttribute | |
context:nil].size.height; | |
return mainTextHeight + detailTextHeight + PADDING; | |
} | |
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
[tableView deselectRowAtIndexPath:indexPath animated:YES]; | |
} | |
#pragma mark - private methods | |
/** | |
フォント属性を更新する | |
*/ | |
- (void)_refreshFontAttributes | |
{ | |
self.bodyAttribute = @{NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleBody]}; | |
self.subTitleAttribute = @{NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline], | |
NSForegroundColorAttributeName : [UIColor darkGrayColor]}; | |
} | |
/** | |
セルを更新して返却する | |
*/ | |
- (void)_updateCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath | |
{ | |
cell.textLabel.attributedText = [[NSAttributedString alloc] initWithString:self.tableTexts[indexPath.row] | |
attributes:self.bodyAttribute]; | |
cell.detailTextLabel.attributedText = [[NSAttributedString alloc] initWithString:self.detailText | |
attributes:self.subTitleAttribute]; | |
} | |
@end | |
@implementation CustomCell | |
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier | |
{ | |
self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier]; | |
if(self){ | |
cell.textLabel.numberOfLines = 0; | |
} | |
return self; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment