Ios 使用NSLayoutManager创建动画文本效果?
在第220课时(使用文本工具包的高级文本布局和效果)中,他们特别指出,可以与Ios 使用NSLayoutManager创建动画文本效果?,ios,animation,uilabel,textkit,nslayoutmanager,Ios,Animation,Uilabel,Textkit,Nslayoutmanager,在第220课时(使用文本工具包的高级文本布局和效果)中,他们特别指出,可以与NSTextStorage和NSTextContainer结合使用,以创建高级文本动画。他们没有说怎么做 我想使用NSLayoutManager/NSTextStorage/NSTextContainer创建自定义文本动画。简单地说,我想为单个标志符号的大小和位置以及特定标志符号的淡入淡出和取消淡出设置动画 对于NSLayoutManager的动画,似乎没有专门的方法或文档,这也是我找到的唯一一个关于这个问题的教程。但是
NSTextStorage
和NSTextContainer
结合使用,以创建高级文本动画。他们没有说怎么做
我想使用NSLayoutManager
/NSTextStorage
/NSTextContainer
创建自定义文本动画。简单地说,我想为单个标志符号的大小和位置以及特定标志符号的淡入淡出和取消淡出设置动画
对于NSLayoutManager
的动画,似乎没有专门的方法或文档,这也是我找到的唯一一个关于这个问题的教程。但是,它展示了如何将NSLayoutManager
编入动画,而不是如何按预期的方式使用它(它们为每个字形创建CATextLayer
)
谁能给我指一下正确的方向吗?我知道如何使用
NSLayoutManager
/NSTextStorage
/NSTextContainer
呈现静态文本。一些演示,展示使用NSLayoutManager
制作文本动画的原理将是完美的。为了让我开始,我可以自己弄清楚细节。NSTextContainer、NSLayoutManager、NSTextStorage是iOS7的新功能:
1) NSTextContainer:
NSTextContainer类定义了文本布局的区域。
NSTextContainer对象定义矩形区域,您可以在textcontainer的边界矩形内定义排除路径,以使文本流位于ExclusionPathAsitislaidout
2) NSLayoutManager:
NSLayoutManager对象协调NSTextStorage对象中保存的字符的布局和显示。它将Unicode字符代码映射到glyph,在一系列NSTextContainer对象中设置glyph,并在一系列文本视图对象中显示它们
3) NSTextStorage:
NSTextStorage是NSMutableAttributedString的一个半具体子类,它管理一组客户端NSLayoutManagerobjects,通知FanyChangesToitsCharactersAttributesTotheyCanRelay并根据需要重新显示文本
我们可以知道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];
首先是创建它们的实例,并创建它们之间的关系。您必须通过initWithFrame:textContainer:
方法在UITextView
中添加NSTextContainer
// 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];
你找到更多关于这个的信息了吗?@DannyZlobinsky不太清楚。这里有一个来自高声誉用户的答案,但被删除了(?)。我还在等待一些好的答案。这是一个很好的视频教程,展示了NSTextStorage的原理。我希望它能帮助你,但它看起来很有趣: