iPhone漢字で日本列島

県の名前を県庁の緯度経度に置いて、県名の漢字でつくる日本列島を表示したりするiPhoneアプリのサンプルコードを描いてみます。


今回使った県名と緯度経度のファイル

→→→
File.txt

動かすとこんな感じになります

サンプルコード

#import “ViewController.h”

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidAppear:(BOOL)animated

{

    self.view.backgroundColor = [UIColor colorWithWhite:0.7 alpha:1.0];

    NSString *path = [[NSBundle mainBundle] pathForResource:@”File” ofType:@”txt”];

    NSString *file = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

    NSArray *list = [file componentsSeparatedByString:@”\n”];

    

    NSMutableArray *japanData = [NSMutableArray array];

    for (int i=0; i<[list count]; i++) {

        NSArray *items = [list[i] componentsSeparatedByString:@”\t”];

        NSString *prefecture = items[0];

        NSString *capital = items[1];

        NSNumber *latitude = items[2];

        NSNumber *longitude = items[3];

        NSDictionary *dict = @{@”prefecture”:prefecture, @”capital”:capital, @”latitude”:latitude, @”longitude”:longitude};

        [japanData addObject:dict];

    }

    

    float averageLon = [[japanData valueForKeyPath:@”@sum.longitude.floatValue”] floatValue] / japanData.count;

    float averageLat = [[japanData valueForKeyPath:@”@sum.latitude.floatValue”] floatValue] / japanData.count;

    

    for (NSDictionary *data in japanData) {

        UILabel *pref = [[UILabel alloc] init];

        pref.layer.name = @”prefectureName”;

        pref.text = data[@”prefecture”];

        pref.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];

        pref.textColor = [UIColor colorWithHue:(arc4random()%10)*0.1 saturation:0.8 brightness:1.0 alpha:1.0];

        [pref sizeToFit];

        [self.view addSubview:pref];

        

        float x = [data[@”longitude”] floatValue] – averageLon;

        float y = [data[@”latitude”] floatValue] – averageLat;

        x = 15 * x + CGRectGetMidX(self.view.bounds);

        y = CGRectGetMaxY(self.view.bounds) – (15 * y + CGRectGetMidY(self.view.bounds));

        pref.center = CGPointMake(x, y);

    }

    

    for (int i=0; i<3; i++) {

        UIButton *fontSizeChangeButton = [UIButton buttonWithType:UIButtonTypeSystem];

        fontSizeChangeButton.titleLabel.font = [[UIFont preferredFontForTextStyle:UIFontTextStyleCaption1] fontWithSize:(i * 5 + 10)];

        [fontSizeChangeButton setTitle:@”A” forState:UIControlStateNormal];

        [fontSizeChangeButton sizeToFit];

        [self.view addSubview:fontSizeChangeButton];

        

        float x = (i) * 50 + 350;

        fontSizeChangeButton.center = CGPointMake(x, 280);

        [fontSizeChangeButton addTarget:self action:@selector(changeFontSize:) forControlEvents:UIControlEventTouchUpInside];

    }

}

– (void)changeFontSize:(UIButton *)sender

{

    NSPredicate *pred = [NSPredicate predicateWithFormat:@”layer.name == %@”, @”prefectureName”];

    [[self.view.subviews filteredArrayUsingPredicate:pred] enumerateObjectsUsingBlock:^(UILabel *l, NSUInteger idx, BOOL *stop) {

        

        float rate =  sender.titleLabel.font.pointSize / l.font.pointSize;

        CGPoint o = l.center;

        [UIView animateWithDuration:1.0 animations:^{

            l.transform = CGAffineTransformMakeScale(rate, rate);

            l.center = o;

        } completion:^(BOOL finished) {

            l.transform = CGAffineTransformIdentity;

            l.font = sender.titleLabel.font;

            [l sizeToFit];

            l.center = o;

            

        }];

        

    }];

}

@end