iOSのバージョンによって処理を切り分けるための関数を書いた

昨日のシンタックスハイライトに必要なTextKitはiOS7以降でしか使えないので、iOS6以前は今までどおり普通のUITextViewを使っている。iOSのバージョンによって処理を切り分ける関数を書いたのでメモ。

iOS6以前 f:id:tuto0621:20140811024621p:plain

iOS7以降 f:id:tuto0621:20150517175305p:plain

isSyntaxHighlight関数

isSyntaxHighlight関数の定義にはNSFoundationVersionNumberを使う。

-(BOOL)isSyntaxHighlight
{
    return NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1; // 7.0 and above
}

下のように使う。7以上だったらTextStorage, LayoutManager, TextContainerを作ってICTextViewに渡して生成するけど、6以下の時は普通に作るだけ。

EditViewController.mm:49

    if ([self isSyntaxHighlight]) {
        // TextStorage
        mTextStorage = [RubyHighlightingTextStorage new];
        [mTextStorage replaceCharactersInRange:NSMakeRange(0, 0) withString:[FCFileManager readFileAtPath:mFileName]];

        // LayoutManager
        NSLayoutManager *textLayout = [[NSLayoutManager alloc] init];
        [mTextStorage addLayoutManager:textLayout]; 

        // TextContainer
        NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:self.view.bounds.size];
        [textLayout addTextContainer:textContainer];

        // TextView
        mTextView = [[ICTextView alloc] initWithFrame:self.view.bounds textContainer:textContainer];
    } else {
        // TextView
        mTextView = [[ICTextView alloc] initWithFrame:self.view.bounds];
        mTextView.text = [FCFileManager readFileAtPath:mFileName];
    }

参考: iOSのバージョンによる分岐 - Qiita