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

时间:2015-07-11 12:29:01

标签: ios animation uilabel textkit nslayoutmanager

WWDC 2013的会话220(高级文字布局和文字工具包效果)中,他们明确指出NSLayoutManager可与NSTextStorageNSTextContainer结合使用以创建高级版文字动画。他们不怎么说。

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

NSLayoutManager似乎没有关于动画的专用方法和文档,也是我发现的is here问题的唯一教程。但是,它显示了如何将NSLayoutManager破解成动画,而不是如何以它应该使用的方式使用它(它们为每个单独的字形创建CATextLayer!)。

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

1 个答案:

答案 0 :(得分:1)

NSTextContainer、NSLayoutManager、NSTextStorage是iOS7的新功能:

1)NSTextContainer:

  

NSTextContainer类定义了一个布局文本的区域。   NSTextContainer对象定义了矩形区域,您可以在textcontainer的boundingrectangles内定义排除路径,以及排除类似于其中的文本流。

2)NSLayoutManager:

  

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

3)NSTextStorage:

  

NSTextStorage是NSMutableAttributedString的半独立子类,它管理一组客户端NSLayoutManagerobjects,通知所有更改字符,然后根据需要重新分配并重新显示文本。

我们可以知道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];

首先是make它们的实例,然后创建它们的关系。你必须在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];