使用NSLayoutManager创建动画文本效果?

Ras*_*sto 14 animation nslayoutmanager uilabel ios textkit

WWDC 2013的会话220(高级文本布局和带有文本工具包的效果)中,他们明确地说NSLayoutManager可以与创建高级文本动画一起使用NSTextStorageNSTextContainer创建高级文本动画.他们没怎么说.

我想使用NSLayoutManager/ NSTextStorage/ NSTextContainer来创建自定义文本动画.简单地说,我想为各个字形的大小和位置设置动画,并淡化和解开特定的字形.

似乎没有专门的方法和动画文档,NSLayoutManager我发现的唯一教程就在这里.然而,它展示了如何破解NSLayoutManager动画,而不是如何以它应该使用的方式使用它们(它们CATextLayer为每个单独的字形创建!).

有人能指出我正确的方向吗?我知道如何使用NSLayoutManager/ NSTextStorage/ NSTextContainer来呈现静态文本.一些演示,显示动画文本的原理NSLayoutManager将是完美的.为了让我开始,我可以自己弄清楚细节.

air*_*aft 2

NSTextContainer\xe3\x80\x81NSLayoutManager\xe3\x80\x81NSTextStorageiOS7新功能:

\n\n

1)NSText容器:

\n\n
\n

NSTextContainer 类定义布局文本的区域。\n NSTextContainer 对象定义矩形区域,您可以在文本容器的边界矩形内定义排除路径,以便文本在布局时围绕排除路径流动。

\n
\n\n

2)NS布局管理器:

\n\n
\n

NSLayoutManager 对象协调 NSTextStorage 对象中保存的字符的布局和显示。它将 Unicode 字符代码映射到字形,在一系列 NSTextContainer 对象中设置字形,并将它们显示在一系列文本视图对象中。

\n
\n\n

3)NSTextStorage:

\n\n
\n

NSTextStorage 是 NSMutableAttributedString 的半具体子类,它管理一组客户端 NSLayoutManager 对象,通知它们的字符或属性的任何更改,以便它们可以根据需要中继和重新显示文本。

\n
\n\n

我们知道NSTextStorage它可以存储和管理UITextView文本,它是NSMutableAttributedString它的子类。我们可以添加或修改属性,所以它是存储和管理UITextView文本的一个不错的选择。

\n\n

NSLayoutManager用于管理NSTextStorage布局的内容。

\n\n

NSTextContainer提供一个矩形来隐藏布局的文本。

\n\n

我们可以简单地使用它们:

\n\n
CGRect textViewRect = CGRectInset(self.view.bounds, 10.0, 20.0);\n\n// NSTextContainer\nNSTextContainer *container = [[NSTextContainer alloc] initWithSize:CGSizeMake(textViewRect.size.width, CGFLOAT_MAX)]; // new in iOS 7.0\ncontainer.widthTracksTextView = YES; // Controls whether the receiveradjusts the width of its bounding rectangle when its text view is resized\n\n\n// NSLayoutManager\nNSLayoutManager *layoutManager = [[NSLayoutManager alloc] init]; // new in iOS 7.0\n[layoutManager addTextContainer:container];\n\n\n// NSTextStorage subclass\nself.textStorage = [[TextStorage alloc] init]; // new in iOS 7.0\n[self.textStorage addLayoutManager:layoutManager];\n
Run Code Online (Sandbox Code Playgroud)\n\n

首先是创建它们的实例,并创建它们的关系。您必须通过方法NSTextContainer添加。UITextViewinitWithFrame:textContainer:

\n\n
// UITextView\nUITextView *newTextView = [[UITextView alloc] initWithFrame:textViewRect textContainer:container];\nnewTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;\nnewTextView.scrollEnabled = YES;\nnewTextView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;\n// newTextView.editable = NO;\nnewTextView.font = [UIFont fontWithName:self.textStorage.fontName size:18.0];\nnewTextView.dataDetectorTypes = UIDataDetectorTypeAll;\nself.textView = newTextView;\n[self.view addSubview:self.textView];\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果想用来UITextStorage改变文本的属性,你可以使用:

\n\n
[_textStorage beginEditing];  // begin edit\n[_textStorage endEditing];  // end edit\n
Run Code Online (Sandbox Code Playgroud)\n\n

您可以在它们之间编辑文本,例如:

\n\n
[_textStorage beginEditing];\nNSDictionary *attrsDic = @{NSTextEffectAttributeName: NSTextEffectLetterpressStyle};\nUIKIT_EXTERN NSString *const NSTextEffectAttributeName NS_AVAILABLE_IOS(7_0);          // NSString, default nil: no text effect\nNSMutableAttributedString *mutableAttrString = [[NSMutableAttributedString alloc] initWithString:@"Letterpress" attributes:attrsDic];\nNSAttributedString *appendAttrString = [[NSAttributedString alloc] initWithString:@" Append:Letterpress"];\n[mutableAttrString appendAttributedString:appendAttrString];\n[_textStorage setAttributedString:mutableAttrString];\n[_textStorage endEditing];\n
Run Code Online (Sandbox Code Playgroud)\n\n

或者改变颜色:

\n\n
[_textStorage beginEditing];\n/* Dynamic Coloring Text */\nself.textStorage.bookItem = [[BookItem alloc] initWithBookName:@"Dynamic Coloring.rtf"];\nself.textStorage.tokens = @{@"Alice": @{NSForegroundColorAttributeName: [UIColor redColor]},\n                            @"Rabbit": @{NSForegroundColorAttributeName: [UIColor greenColor]},\n                            DefaultTokenName: @{NSForegroundColorAttributeName: [UIColor blackColor]}\n                            };\n[_textStorage setAttributedString:_textStorage.bookItem.content];\n[_textStorage endEditing];\n
Run Code Online (Sandbox Code Playgroud)\n