微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐