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

WWDC 2013的会议220(高级文本布局和文本工具包效果)中,他们具体说 NSLayoutManager可以与NSTextStorage和NSTextContainer结合使用来创建高级文本动画.他们不说怎么样

我想使用NSLayoutManager / NSTextStorage / NSTextContainer创建自定义文本动画.简单来说,我想为个体字形的大小和位置设置动画,并且褪色并且不符合特定的字形.

似乎没有专门的方法和文档的NSLayoutManager的动画和唯一的教程,我发现is here.但是,它显示了如何将NSLayoutManager攻击到动画,而不是如何使用它应该被使用的方式(他们为每个单个字形创建CATextLayer!).

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

解决方法

NSTextContainer,NSLayoutManager,NSTextStorage是iOS7的新功能

1)NSTextContainer:

The NSTextContainer class defines a region in which text is laid out.
An NSTextContainer object defines rectangular regions,and you can define exclusion paths inside the textcontainer’sboundingrectanglesothattextflowsaroundtheexclusionpathasitislaidout.

2)NSLayoutManager

An NSLayoutManager object coordinates the layout and display of characters held in an NSTextStorage object. It maps Unicode character codes to glyphs,sets the glyphs in a series of NSTextContainer objects,and displays them in a series of text view objects.

3)NSTextStorage:

NSTextStorage is a semiconcrete subclass of NSMutableAttributedString that manages a set of client NSLayoutManagerobjects,notifyingthemofanychangestoitscharactersorattributessothattheycanrelay and redisplay the text as needed.

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

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...