iOS7 TextKit 框架的简单实用

Text Kit架构

下面,我们通过图1(此图来自WWDC2013 Session 210)来了解一下Text Kit的架构。图1是基于iOS 7绘制的,从图中,我们可以看到Text Kit是基于Core Text构建的,它通过Core Text与Core Graphics进行交互。而UI控件(UILabel、UITextField和UITextView)则构建于Text Kit之上,可见这些文本控件可以利用Text Kit提供的API来对文字进行排版和渲染处理。 从图中我们也可以看到SDK提供的UIWebView是基于WebKit的,它不能使用Text Kit提供的功能。

iOS7 TextKit 框架的简单实用_第1张图片


Text Kit特点
从上面的介绍中,我们可以了解到Text Kit在UIKit中的作用非常重要。Text Kit在实际开发中具有如下特点:
1.在UI控件中Text Kit完全掌控着文字的排版和渲染
2.UITextView、UITextField和UILabel是构建于Text Kit之上的
3.能够与动画、UICollectionView和UITableView做到无缝集成
4.Text Kit具有这样一些能力:Subclassing、Delegation和Notifcation。
 
Text Kit功能概述
下面我们看看通过Text Kit,都能实现那些功能(这里列出了是一些常用和重要功能):
1.对文字进行分页或多列排版
2.支持文字的换行、折叠和着色等处理
3.可以调整字与字之间的距离、行间距、文字大小、指定特定的字体
4.支持富文本编辑,可以自定义文字截断
5.支持凸版印刷效果(letterpress)
6.支持数据类型的检测(例如链接、附件等)

Text Kit示例

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UITextView *textView;

@property (nonatomic, strong) UIImageView *imageView;

@property (nonatomic, assign) CGPoint gestureStartingPoint;

@property (nonatomic, assign) CGPoint gestureStartingCenter;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    [self.view addSubview:self.textView];
    
    [self.view addSubview:self.imageView];
    
    self.textView.textContainer.exclusionPaths = @[[self translatedBezierPath]];
    
}

- (UIBezierPath *)translatedBezierPath
{
    CGRect butterflyImageRect = [self.textView convertRect:self.imageView.frame fromView:self.view];
    UIBezierPath *newButterflypath = [UIBezierPath bezierPathWithRect:butterflyImageRect];
    
    return newButterflypath;
}

/**
 *  图片平移操作
 *
 *  @param sender
 */
- (void)imagePanned:(id)sender
{
    if ([sender isKindOfClass:[UIPanGestureRecognizer class]]) {
        UIPanGestureRecognizer *localSender = sender;
        if (localSender.state == UIGestureRecognizerStateBegan) {
            self.gestureStartingPoint = [localSender translationInView:self.textView];
            
            self.gestureStartingCenter = self.imageView.center;
        } else if (localSender.state == UIGestureRecognizerStateChanged) {
            CGPoint currentPoint = [localSender translationInView:self.textView];
            
            CGFloat distanceX = currentPoint.x - self.gestureStartingPoint.x;
            CGFloat distanceY = currentPoint.y - self.gestureStartingPoint.y;
            
            CGPoint newCenter = self.gestureStartingCenter;
            
            newCenter.x += distanceX;
            newCenter.y += distanceY;
            
            self.imageView.center = newCenter;
            
            self.textView.textContainer.exclusionPaths = @[[self translatedBezierPath]];
        } else if (localSender.state == UIGestureRecognizerStateEnded) {
            self.gestureStartingCenter = CGPointZero;
            self.gestureStartingPoint = CGPointZero;
        }
    }
}

- (UITextView *)textView
{
    if (_textView == nil) {
        _textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 20, 375, 300)];
        _textView.backgroundColor = [UIColor cyanColor];
        _textView.font = [UIFont systemFontOfSize:15];
        _textView.editable = NO;
        _textView.text = @"Text Kit是基于Core Text构建的快速、先进的文本排版和渲染引擎,并且与UIKit很好的集合。UITextView,UITextField、UILabel都已经基于Text Kit重新构建,所以它们都支持分页文本、文本包装、富文本编辑、交互式文本着色、文本折叠和自定义截取等特性。所有这些UI控件现在都以同样的方式构建,在它们后面,一个NSTextStorage对象保存着文本的主要信息,它本身是NSMutableAttributedString的子类,支持分批编辑。这就意味着你可以改变一个范围内的字符的样式而不用整体替换文本内容";
        
    }
    return _textView;
}

- (UIImageView *)imageView
{
    if (_imageView == nil) {
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 84, 84)];
        _imageView.image = [UIImage imageNamed:@"pic_head_logo"];
        _imageView.userInteractionEnabled = YES;
        
        UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(imagePanned:)];
        [_imageView addGestureRecognizer:panGes];
    }
    return _imageView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


效果:

iOS7 TextKit 框架的简单实用_第2张图片

你可能感兴趣的:(移动开发-iOS)