[英]Text garbled when using NSTextContainer/NSLayoutManager with UITextView
[英]Creating animated text effects using NSLayoutManager?
在WWDC 2013的第 220节(带有文本工具包的高级文本布局和效果)中,他们特别指出NSLayoutManager
可以与NSTextStorage
和NSTextContainer
结合使用以创建高级文本动画。 他们不说怎么做。
我想使用NSLayoutManager
/ NSTextStorage
/ NSTextContainer
创建自定义文本动画。 简而言之,我想对单个字形的大小和位置进行动画处理,并淡化和淡化特定字形。
似乎没有专用的方法或NSLayoutManager
动画文档,有关此问题的唯一教程在这里 。 但是,它显示了如何将NSLayoutManager
为动画,而不是如何按预期的方式使用它 (它们为每个单独的字形创建CATextLayer
!)。
有人可以指出我正确的方向吗? 我知道如何使用NSLayoutManager
/ NSTextStorage
/ NSTextContainer
呈现静态文本。 一些演示,展示使用NSLayoutManager
动画文本的原理,将是完美的。 为了让我入门,我可以自己弄清楚细节。
NSTextContainer、NSLayoutManager、NSTextStorage
是NSTextContainer、NSLayoutManager、NSTextStorage
的新增功能:
1)NSTextContainer:
NSTextContainer类定义在其中放置文本的区域。 NSTextContainer对象定义矩形区域,您可以在文本容器的边界矩形内定义排除路径,以使文本流围绕排除路径布局。
2)NSLayoutManager:
NSLayoutManager对象协调NSTextStorage对象中包含的字符的布局和显示。 它将Unicode字符代码映射到字形,在一系列NSTextContainer对象中设置字形,并在一系列文本视图对象中显示它们。
3)NSTextStorage:
NSTextStorage是NSMutableAttributedString的一个半混凝土子类,它管理一组客户端NSLayoutManager对象,并通知mofany更改其字符或属性,以便它们可以根据需要重新设置并重新显示文本。
我们知道NSTextStorage
可以存储和管理UITextView
的文本,它是NSMutableAttributedString
的子类。我们可以添加或修改属性,因此它是存储和管理UITextView
的文本的不错选择。
NSLayoutManager
用于管理NSTextStorage
的布局内容。
NSTextContainer
提供了一个矩形来存储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];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.