首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用NSLayoutManager创建动画文本效果?

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

Stack Overflow用户
提问于 2015-07-11 12:29:01
回答 1查看 1.1K关注 0票数 14

WWDC 2013的第220节(使用文本工具包的高级文本布局和效果)中,他们特别指出,NSLayoutManager可以与NSTextStorageNSTextContainer一起使用来创建高级文本动画。他们没说怎么做。

我想使用NSLayoutManager/NSTextStorage/NSTextContainer来创建自定义文本动画。简单地说,我想要动画的大小和位置的个别图形和褪色和不褪色特定的图形。

对于使用NSLayoutManager的动画,似乎没有专门的方法或文档,也是我找到就在这里的唯一教程。然而,它展示了如何将NSLayoutManager黑入动画中,而不是如何按照它应该使用的方式使用它(它们为每个单独的字形创建CATextLayer!)

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

EN

回答 1

Stack Overflow用户

发布于 2016-12-28 01:02:52

NSTextContainer、NSLayoutManager、NSTextStorage是iOS7的新成员:

1) NSTextContainer:

NSTextContainer类定义了一个布局文本的区域。textcontainer'sboundingrectanglesothattextflowsaroundtheexclusionpathasitislaidout.对象定义矩形区域,您可以在NSTextContainer中定义排除路径。

2) NSLayoutManager:

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

3) NSTextStorage:

是NSMutableAttributedString的一个半连续子类,它管理一组客户端NSLayoutManagerobjects、NSMutableAttributedString并根据需要重新显示文本。

我们可以知道NSTextStorage可以存储和管理UITextView的文本,而NSMutableAttributedString的subclass.We可以添加或修改属性,因此存储和管理UITextView文本是一个很好的选择。

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

NSTextContainer提供一个矩形来存储布局的文本。

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

代码语言:javascript
复制
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];

首先是它们的实例,并创建它们的relationship.You必须通过initWithFrame:textContainer:方法在UITextView中添加initWithFrame:textContainer:

代码语言:javascript
复制
// 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更改文本的属性,可以使用:

代码语言:javascript
复制
[_textStorage beginEditing];  // begin edit
[_textStorage endEditing];  // end edit

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

代码语言:javascript
复制
[_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];

或改变颜色:

代码语言:javascript
复制
[_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];
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31357193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档