久々に仕事でiOSのコードを書いています。iOS6でNSAttributedStringがLabel等で使えるようになりました。その恩恵を地味に受けることはありましたが、今回の仕事はもろにそれが便利だなーと感じています。

iOS5のときは、UILabelとかではNSAttributedStringは受け付けてくれませんでした。なので、CALayerを使ったり、QuartzCoreを使って描画したりしてました。それが、iOS6では、こんな簡単な書き方でOK。

[self.label setAttributedText:attString];

画期的です。ただ、NSAttributedStringを作るのは結構面倒くさいです。今作ってるのは、展示施設で使う音声ガイダンスのアプリなので、事前に用意したテキストを表示するということが出来ればOK。なので、rtfでファイルを作っておいてそれを開けばいいかーと思ったら、それが出来ない。。。

Macだと、こういう書き方でいけるっぽいんだけど。

NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"rtf"];
NSAttributedString *attString = [[NSAttributedString alloc] initWithPath:path documentAttributes:nil];
[[self.textView textStorage] setAttributedString:attString];

これでいうと、initWithPath(ファイルから開くメソッド)がiOSのNSAttributedStringにない。。

作りたいリッチテキストは、シンプル。タイトル部分はHiraMinProN-W6で大きめ、本文はHiraMinProN-W3で小さめ、あとは行間調整。なので、データは標準テキストで持って、一行目をタイトル、それ以降を本文として読み込むことに。

まぁ、こんな感じ。

- (NSAttributedString*)titleAttStringWithString:(NSString*)str
{
    UIFont *numFont = [UIFont fontWithName:@"HiraMinProN-W6" size:23.0];

    //  paragraphSetting
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.paragraphSpacing = 8.f;
    paragraphStyle.alignment = NSTextAlignmentJustified;

    UIColor *color = [UIColor blackColor];

    //
    NSDictionary *attributeDic = @{ NSFontAttributeName:numFont,
                                    NSForegroundColorAttributeName:color,
                                    NSParagraphStyleAttributeName:paragraphStyle};

    NSAttributedString *repString = [[NSAttributedString alloc] initWithString:str attributes:attributeDic];

    return repString;
}

- (NSAttributedString*)textAttStringWithString:(NSString*)str
{
    UIFont *numFont = [UIFont fontWithName:@"HiraMinProN-W3" size:20.0];

    //  paragraphSetting
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineHeightMultiple = 1.1f;
    paragraphStyle.alignment = NSTextAlignmentJustified;

    UIColor *color = [UIColor blackColor];

    //
    NSDictionary *attributeDic = @{ NSFontAttributeName:numFont,
                                    NSForegroundColorAttributeName:color,
                                    NSParagraphStyleAttributeName:paragraphStyle};

    NSAttributedString *repString = [[NSAttributedString alloc] initWithString:str attributes:attributeDic];

    return repString;
}

- (void)load
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"txt"];

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

    NSRange returnRange = [string rangeOfString:@"\n"];

    NSString *title = [string substringToIndex:returnRange.location+1];
    NSString *text = [string substringFromIndex:returnRange.location+1];

    NSAttributedString *titleAttString = [self titleAttStringWithString:title];//このメソッドでタイトルの修飾設定
    NSAttributedString *textAttString = [self textAttStringWithString:text];//このメソッドで本文の修飾設定

    NSMutableAttributedString *repAttStr = [[NSMutableAttributedString alloc] initWithAttributedString:titleAttString];
    [repAttStr appendAttributedString:textAttString];

    [self.textView setAttributedText:repAttStr];
}

ふーむ。こうしてブログに書くとなんかコード長いな。