FTCoreText图文混排框架

一 、FTCoreText介绍

         FTCoreText是采用类HTML的方式在Text view上实现丰富的文字效果,包括文字大小、颜色、字体、下划线,链接以及图文混排等等,以及对文字进行排版。实现类似于网页的文字效果。FTCoreText只有两个类FTCoreTextStyle和FTCoreTextView,FTCoreTextView用来显示排版视图,而FTCoreTextStyle用来设置字体的种类和大小、颜色等。具体字体的设置使用了CoreText的属性来设置。 



二 、使用方法

1 创建排版视图的方法

//创建一个滑动视图
 scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
//滑动视图自动调整大小
    scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//创建排版视图
    coreTextView = [[FTCoreTextView alloc] initWithFrame:CGRectMake(20, 20, 280, 0)];
//排版视图自动调整大小
    coreTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    // 载入文本
    [coreTextView setText:[self textForView]];
    // 设置格式
    [coreTextView addStyles:[self coreTextStyle]];
    // 设置代理
    [coreTextView setDelegate:self];
    //自动调整高度
    [coreTextView fitToSuggestedHeight];
    //将排版视图加入到滑动视图上
    [scrollView addSubview:coreTextView];
    [scrollView setContentSize:CGSizeMake(CGRectGetWidth(scrollView.bounds), CGRectGetHeight(coreTextView.frame) + 40)];
    //加入当前视图
    [self.view addSubview:scrollView];


2  创建排版格式的方法

三种创建方法

第一种 直接创建一个对象 然后设置对象名字、大小、属性等

FTCoreTextStyle *defaultStyle = [FTCoreTextStyle new];
    defaultStyle.name = FTCoreTextTagDefault;	//thought the default name is already set to FTCoreTextTagDefault
    defaultStyle.font = [UIFont fontWithName:@"TimesNewRomanPSMT" size:15.f];
    //defaultStyle.color=[UIColor redColor];
    defaultStyle.textAlignment = FTCoreTextAlignementJustified;
    [result addObject:defaultStyle];


第二种 创建的时候直接设置对象名称

FTCoreTextStyle *titleStyle = [FTCoreTextStyle styleWithName:@"title"]; // using fast method
    titleStyle.font = [UIFont fontWithName:@"TimesNewRomanPSMT" size:40.f];
    titleStyle.paragraphInset = UIEdgeInsetsMake(0, 0, 25, 0);
    titleStyle.textAlignment = FTCoreTextAlignementCenter;
    [result addObject:titleStyle];


第三种  copy一个已有对象,然后设置名称,属性等

FTCoreTextStyle *linkStyle = [defaultStyle copy];
    linkStyle.name = FTCoreTextTagLink;
    linkStyle.color = [UIColor orangeColor];
    [result addObject:linkStyle];
    [linkStyle release];


排版格式会作为一个数组加入到排版视图中。

3  常用的属性


name:

       就是要改变字体或图片的名称一般用括号隔开,类似于html中的写法,如<title>Giraffe</title>这样就是两个title中间字体的格式,或者图片用<_image>giraffe.png</_image>来表示。


font:

        字体的大小,默认的是12.


color: 字体颜色,默认是黑色的。


underlined:  下划线,默认是没有下划线的。


textAlignment:对齐方式,默认是左对齐,还有右对齐和中间对齐,还有两边对齐。


maxLineHeight: 每一行最大高度,默认为0,就是自适应的。


minLineHeight:每一行最小高度,也是默认为0,自适应的。


paragraphInset:  设置段落的大小和范围,默认是自适应的。


applyParagraphStyling: 是否使用段落排版风格,默认是YES。


leading:  设置相邻两行的距离


bulletCharacter: 设置并排格式的格式符号



4 代理:<FTCoreTextViewDelegate>


点击实现的方法

  • (void)coreTextView:(FTCoreTextView *)acoreTextView receivedTouchOnData:(NSDictionary *)data { }
代码展示部分
ViewController.h
#import <UIKit/UIKit.h>
#import "FTCoreTextView.h"
@interface ViewController : UIViewController<FTCoreTextViewDelegate>

@property (nonatomic,retain)UIScrollView *scrollView;
@property (nonatomic,retain)FTCoreTextView *coreTextView;
@end

ViewController.m
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface ViewController ()

@end

@implementation ViewController
@synthesize scrollView;
@synthesize coreTextView;

//载入文本资源
- (NSString *)textForView
{
    return [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"text" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
}

//设置格式
- (NSArray *)coreTextStyle
{
    NSMutableArray *result = [NSMutableArray array];
    
    //为不同种类的字体设置格式
    //为所有默认的文本进行设置
    FTCoreTextStyle *defaultStyle = [FTCoreTextStyle new];
    defaultStyle.name = FTCoreTextTagDefault;	//thought the default name is already set to FTCoreTextTagDefault
    defaultStyle.font = [UIFont fontWithName:@"TimesNewRomanPSMT" size:15.f];
    //defaultStyle.color=[UIColor redColor];
    defaultStyle.textAlignment = FTCoreTextAlignementJustified;
    //[defaultStyle setMaxLineHeight:12.0];
    //[defaultStyle setParagraphInset:UIEdgeInsetsMake(10, 10, 50, 500)];
    //[defaultStyle setApplyParagraphStyling:NO];
    //[defaultStyle setLeading:20];
    [result addObject:defaultStyle];
    
    //对标记为title的进行设置
    FTCoreTextStyle *titleStyle = [FTCoreTextStyle styleWithName:@"title"]; // using fast method
    titleStyle.font = [UIFont fontWithName:@"TimesNewRomanPSMT" size:40.f];
    titleStyle.paragraphInset = UIEdgeInsetsMake(0, 0, 25, 0);
    titleStyle.textAlignment = FTCoreTextAlignementCenter;
    [result addObject:titleStyle];
    
    //对所有的图片进行设置
    FTCoreTextStyle *imageStyle = [FTCoreTextStyle new];
    imageStyle.paragraphInset = UIEdgeInsetsMake(0,0,0,0);
    imageStyle.name = FTCoreTextTagImage;
    imageStyle.textAlignment = FTCoreTextAlignementCenter;
    [result addObject:imageStyle];
    [imageStyle release];
    
    //对所有的letter进行设置
    FTCoreTextStyle *firstLetterStyle = [FTCoreTextStyle new];
    firstLetterStyle.name = @"firstLetter";
    firstLetterStyle.font = [UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:30.f];
    [result addObject:firstLetterStyle];
    [firstLetterStyle release];
    
    //对所有文本中的link进行设置
    FTCoreTextStyle *linkStyle = [defaultStyle copy];
    linkStyle.name = FTCoreTextTagLink;
    linkStyle.color = [UIColor orangeColor];
    [result addObject:linkStyle];
    [linkStyle release];
    
    FTCoreTextStyle *subtitleStyle = [FTCoreTextStyle styleWithName:@"subtitle"];
    subtitleStyle.font = [UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:25.f];
    subtitleStyle.color = [UIColor brownColor];
    subtitleStyle.paragraphInset = UIEdgeInsetsMake(10, 0, 10, 0);
    [result addObject:subtitleStyle];
    
    FTCoreTextStyle *bulletStyle = [defaultStyle copy];
    bulletStyle.name = FTCoreTextTagBullet;
    bulletStyle.bulletFont = [UIFont fontWithName:@"TimesNewRomanPSMT" size:16.f];
    bulletStyle.bulletColor = [UIColor orangeColor];
    bulletStyle.bulletCharacter = @"❧";
    [result addObject:bulletStyle];
    [bulletStyle release];
    
    FTCoreTextStyle *italicStyle = [defaultStyle copy];
    italicStyle.name = @"italic";
    italicStyle.underlined = YES;
    italicStyle.font = [UIFont fontWithName:@"TimesNewRomanPS-ItalicMT" size:16.f];
    [result addObject:italicStyle];
    [italicStyle release];
    
    FTCoreTextStyle *boldStyle = [defaultStyle copy];
    boldStyle.name = @"bold";
    boldStyle.font = [UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:16.f];
    [result addObject:boldStyle];
    [boldStyle release];
    
    FTCoreTextStyle *coloredStyle = [defaultStyle copy];
    [coloredStyle setName:@"colored"];
    [coloredStyle setColor:[UIColor redColor]];
    [result addObject:coloredStyle];
    [defaultStyle release];
    
    return  result;
}

- (void)coreTextView:(FTCoreTextView *)acoreTextView receivedTouchOnData:(NSDictionary *)data {
    
    CGRect frame = CGRectFromString([data objectForKey:FTCoreTextDataFrame]);
    
    if (CGRectEqualToRect(CGRectZero, frame)) return;
    
    frame.origin.x -= 3;
    frame.origin.y -= 1;
    frame.size.width += 6;
    frame.size.height += 6;
    UIView *view = [[UIView alloc] initWithFrame:frame];
    [view.layer setCornerRadius:3];
    [view setBackgroundColor:[UIColor orangeColor]];
    [view setAlpha:0];
    [acoreTextView.superview addSubview:view];
    [UIView animateWithDuration:0.2 animations:^{
        [view setAlpha:0.4];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5 animations:^{
            [view setAlpha:0];
        }];
    }];
    
    return;
    
    NSURL *url = [data objectForKey:FTCoreTextDataURL];
    if (!url) return;
    [[UIApplication sharedApplication] openURL:url];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    coreTextView = [[FTCoreTextView alloc] initWithFrame:CGRectMake(20, 20, 280, 0)];
    coreTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    // set text
    [coreTextView setText:[self textForView]];
    // set styles
    [coreTextView addStyles:[self coreTextStyle]];
    // set delegate
    [coreTextView setDelegate:self];
    
    [coreTextView fitToSuggestedHeight];
    
    [scrollView addSubview:coreTextView];
    [scrollView setContentSize:CGSizeMake(CGRectGetWidth(scrollView.bounds), CGRectGetHeight(coreTextView.frame) + 40)];
    
    [self.view addSubview:scrollView];
    // Do any additional setup after loading the view, typically from a nib.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return (toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [scrollView setContentSize:CGSizeMake(CGRectGetWidth(scrollView.bounds), CGRectGetHeight(coreTextView.frame) + 40)];
}

- (void)dealloc
{
    [coreTextView release];
    [scrollView release];
    [super dealloc];
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值