14

WWDC 2013のセッション 220 (テキスト キットによる高度なテキスト レイアウトと効果) では、高度なテキスト アニメーションと組み合わせて使用​​し、作成することNSLayoutManagerができると具体的に述べています。彼らは方法を言いません。NSTextStorageNSTextContainer

NSLayoutManager/ NSTextStorage/NSTextContainerを使用して、カスタム テキスト アニメーションを作成したいと考えています。簡単に言うと、個々のグリフのサイズと位置をアニメーション化し、特定のグリフをフェードおよびフェード解除したいと考えています。

を使用したアニメーション専用の方法やドキュメントはないようです。NSLayoutManager私が見つけた問題に関する唯一のチュートリアルは hereです。ただし、アニメーションを想定どおりに使用する方法ではNSLayoutManagerなく、アニメーションにハックする方法を示しています (個々のグリフごとに作成されます!)。CATextLayer

誰かが私を正しい方向に向けることができますか? NSLayoutManager/ NSTextStorage/NSTextContainerを使用して静的テキストをレンダリングする方法を知っています。テキストをアニメーション化する原理を示すいくつかのデモはNSLayoutManager完璧でしょう。始めるためだけに、自分で詳細を把握できます。

4

1 に答える 1

2

NSTextContainer、NSLayoutManager、NSTextStorageiOS7 の新機能:

1) NSTextContainer:

NSTextContainer クラスは、テキストが配置される領域を定義します。NSTextContainer オブジェクトは四角形の領域を定義し、テキスト コンテナの境界四角形の内側に除外パスを定義して、テキストが除外パスの周りをレイアウトされたまま流れるようにすることができます。

2) NSLayoutManager:

NSLayoutManager オブジェクトは、NSTextStorage オブジェクトに保持されている文字のレイアウトと表示を調整します。Unicode 文字コードをグリフにマップし、一連の NSTextContainer オブジェクトにグリフを設定し、一連のテキスト ビュー オブジェクトに表示します。

3) NSTextStorage:

NSTextStorage は、一連のクライアント NSLayoutManager オブジェクトを管理する NSMutableAttributedString の半具象的なサブクラスであり、必要に応じてテキストをリレーおよび再表示できるように、その文字または属性の変更を通知します。

のテキストNSTextStorageを保存および管理できることがわかります。これはのサブクラスです。属性を追加または変更できるため、 のテキストを保存および管理するのに適しています。UITextViewNSMutableAttributedStringUITextView

NSLayoutManagerNSTextStorageのレイアウトのコンテンツを管理するために使用します。

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];

まず、それらのインスタンスを作成し、それらの関係を作成します。メソッドで追加する必要がありますNSTextContainerUITextViewinitWithFrame: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];
于 2016-12-28T01:02:52.137 に答える