Skip to content

Instantly share code, notes, and snippets.

@quesera2
Last active December 24, 2015 07:29

Revisions

  1. glayash revised this gist Oct 4, 2013. 1 changed file with 5 additions and 6 deletions.
    11 changes: 5 additions & 6 deletions OS7MainViewController.m
    Original file line number Diff line number Diff line change
    @@ -46,10 +46,10 @@ - (void)viewDidLoad

    [self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:CellIdentifier];

    self.tableTexts = @[@"りんごは浮かんだ お空に\nりんごは落っこちた 地べたに",
    @"星が生まれて\n歌が生まれて\nルルアメルは笑った\nとこしえと",
    @"星がキスして\n歌が眠って\n帰るとこは 何処でしょう\n帰るとこは 何処でしょう",
    @"りんごは落っこちた 地べたに\nりんごは浮かんだ お空に"];
    self.tableTexts = @[@"りんごは浮かんだお空に…\nりんごは落っこちた地べたに…,
    @"星が生まれて\n歌が生まれて\nルルアメルは笑った\n常しえと",
    @"星がキスして\n歌が眠って\かえるとこは どこでしょう…?\nかえるとこは どこでしょう…?",
    @"りんごは落っこちた地べたに…\nりんごは浮かんだお空に…"];
    self.detailText = @"Apple (Symphogear)";
    [self _refreshFontAttributes];
    @@ -82,7 +82,6 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.textLabel.numberOfLines = 0;
    [self _updateCell:cell atIndexPath:indexPath];
    return cell;
    }
    @@ -141,7 +140,7 @@ -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuse
    self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
    if(self){
    //do nothing
    cell.textLabel.numberOfLines = 0;
    }
    return self;
  2. glayash created this gist Sep 30, 2013.
    150 changes: 150 additions & 0 deletions OS7MainViewController.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,150 @@
    //
    // 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帰るとこは 何処でしょう",
    @"りんごは落っこちた 地べたに\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];
    cell.textLabel.numberOfLines = 0;
    [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){
    //do nothing
    }

    return self;
    }

    @end