NSTextContainer、NSLayoutManager、NSTextStorage
iOS7 の新機能:
1) NSTextContainer:
NSTextContainer クラスは、テキストが配置される領域を定義します。NSTextContainer オブジェクトは四角形の領域を定義し、テキスト コンテナの境界四角形の内側に除外パスを定義して、テキストが除外パスの周りをレイアウトされたまま流れるようにすることができます。
2) NSLayoutManager:
NSLayoutManager オブジェクトは、NSTextStorage オブジェクトに保持されている文字のレイアウトと表示を調整します。Unicode 文字コードをグリフにマップし、一連の NSTextContainer オブジェクトにグリフを設定し、一連のテキスト ビュー オブジェクトに表示します。
3) NSTextStorage:
NSTextStorage は、一連のクライアント NSLayoutManager オブジェクトを管理する NSMutableAttributedString の半具象的なサブクラスであり、必要に応じてテキストをリレーおよび再表示できるように、その文字または属性の変更を通知します。
のテキストNSTextStorage
を保存および管理できることがわかります。これはのサブクラスです。属性を追加または変更できるため、 のテキストを保存および管理するのに適しています。UITextView
NSMutableAttributedString
UITextView
NSLayoutManager
NSTextStorage
のレイアウトのコンテンツを管理するために使用します。
NSTextContainer
レイアウトされたテキストを隠すための四角形を提供します。
それらを簡単に使用できます。
CGRect textViewRect = CGRectInset(self.view.bounds, 10.0, 20.0);
// NSTextContainer
NSTextContainer *container = [[NSTextContainer alloc] initWithSize:CGSizeMake(textViewRect.size.width, CGFLOAT_MAX)]; // new in iOS 7.0
container.widthTracksTextView = YES; // Controls whether the receiveradjusts the width of its bounding rectangle when its text view is resized
// NSLayoutManager
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init]; // new in iOS 7.0
[layoutManager addTextContainer:container];
// NSTextStorage subclass
self.textStorage = [[TextStorage alloc] init]; // new in iOS 7.0
[self.textStorage addLayoutManager:layoutManager];
まず、それらのインスタンスを作成し、それらの関係を作成します。メソッドで追加する必要がありますNSTextContainer
。UITextView
initWithFrame:textContainer:
// UITextView
UITextView *newTextView = [[UITextView alloc] initWithFrame:textViewRect textContainer:container];
newTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
newTextView.scrollEnabled = YES;
newTextView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
// newTextView.editable = NO;
newTextView.font = [UIFont fontWithName:self.textStorage.fontName size:18.0];
newTextView.dataDetectorTypes = UIDataDetectorTypeAll;
self.textView = newTextView;
[self.view addSubview:self.textView];
テキストの属性を変更するために使用したい場合はUITextStorage
、次を使用できます。
[_textStorage beginEditing]; // begin edit
[_textStorage endEditing]; // end edit
それらの間で、次のようなテキストを編集できます。
[_textStorage beginEditing];
NSDictionary *attrsDic = @{NSTextEffectAttributeName: NSTextEffectLetterpressStyle};
UIKIT_EXTERN NSString *const NSTextEffectAttributeName NS_AVAILABLE_IOS(7_0); // NSString, default nil: no text effect
NSMutableAttributedString *mutableAttrString = [[NSMutableAttributedString alloc] initWithString:@"Letterpress" attributes:attrsDic];
NSAttributedString *appendAttrString = [[NSAttributedString alloc] initWithString:@" Append:Letterpress"];
[mutableAttrString appendAttributedString:appendAttrString];
[_textStorage setAttributedString:mutableAttrString];
[_textStorage endEditing];
または色を変更します。
[_textStorage beginEditing];
/* Dynamic Coloring Text */
self.textStorage.bookItem = [[BookItem alloc] initWithBookName:@"Dynamic Coloring.rtf"];
self.textStorage.tokens = @{@"Alice": @{NSForegroundColorAttributeName: [UIColor redColor]},
@"Rabbit": @{NSForegroundColorAttributeName: [UIColor greenColor]},
DefaultTokenName: @{NSForegroundColorAttributeName: [UIColor blackColor]}
};
[_textStorage setAttributedString:_textStorage.bookItem.content];
[_textStorage endEditing];