昨日のシンタックスハイライトに必要なTextKitはiOS7以降でしか使えないので、iOS6以前は今までどおり普通のUITextView
を使っている。iOSのバージョンによって処理を切り分ける関数を書いたのでメモ。
iOS6以前
iOS7以降
isSyntaxHighlight関数
isSyntaxHighlight関数の定義にはNSFoundationVersionNumber
を使う。
-(BOOL)isSyntaxHighlight { return NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1; // 7.0 and above }
下のように使う。7以上だったらTextStorage, LayoutManager, TextContainerを作ってICTextViewに渡して生成するけど、6以下の時は普通に作るだけ。
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]; }