程序员

iOS 富文本排版(一)

2018-03-09  本文已影响137人  十一遥

富文本排版样式丰富详细,适合专业的文字展示.

下面就简单介绍下iOS 富文本的使用:

一 、 纯文字排版

(1)逐次排版
-(void)addAttribute:(NSAttributedStringKey)name value:(id)value range:(NSRange)range;

(2)统一排版
-(void)addAttributes:(NSDictionary<NSAttributedStringKey, id> *)attrs range:(NSRange)range;

统一排版,就是把需要设置的属性,一次性添加到字典里

    NSString *str = @"用户当前资源 t = 13是获取月票,14是获取评价票,15是获取鲜花,16获取打赏,17获取催更 ";
    
    // 创建一个富文本
    NSMutableAttributedString * attriStr = [[NSMutableAttributedString alloc] initWithString:str];
    
    //逐次排版
    // 前五个字的文字颜色
    [attriStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 5)];
    // 前五个字的背景颜色
    [attriStr addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(0, 5)];
    // 前五个字的字号
    [attriStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 5)];
    
    
    //统一排版
    NSDictionary * attriBute = @{NSForegroundColorAttributeName:[UIColor redColor],
                                 NSBackgroundColorAttributeName:[UIColor yellowColor],
                                 NSFontAttributeName:[UIFont systemFontOfSize:20]};
    
    [attriStr addAttributes:attriBute range:NSMakeRange(0, 5)];
    
    //添加样式
    lab.attributedText = attriStr;

纯文字排版

二 、 图文混排

我们要使用这个类添加图片NSTextAttachment

    //添加图片
    NSTextAttachment *attchImage = [[NSTextAttachment alloc] init];
    
    attchImage.image = [UIImage imageNamed:@"Pikachu"];
    attchImage.bounds = CGRectMake(0, 0, 100, 70);
    
    NSAttributedString *stringImage = [NSAttributedString attributedStringWithAttachment:attchImage];
    
    //若不添加下面这句话,默认会把图片排在文字末尾
    [attriStr insertAttributedString:stringImage atIndex:30];

图文混排

三 、 超链接

我是实现textView的下面代理方法 ,来执行的

-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {

    //添加超链接
    [attriStr addAttribute:NSLinkAttributeName value:@"homePage://" range:[[attriStr string] rangeOfString:@"个人主页"]];
//代理方法
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
   
    NSLog(@"url :%@",URL);
    
    if ([[URL scheme] isEqualToString:@"homePage"]) {
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://www.jianshu.com/u/a79a9392d831"]]];

        return NO;
    }
    
    return YES;
}

超链接
完整代码(太简单,就不传git了)
#import "ViewController.h"

#define  WIDTH  [UIScreen mainScreen].bounds.size.width
#define  HEIGHT  [UIScreen mainScreen].bounds.size.height

@interface ViewController () <UITextViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *str = @"用户当前资源 t = 13是获取月票,14是获取评价票,15是获取鲜花,16获取打赏,17获取催更  这是我的《个人主页》";
    
    // 创建一个富文本
    NSMutableAttributedString * attriStr = [[NSMutableAttributedString alloc] initWithString:str];
    
    //文字排版
    NSDictionary * attriBute = @{NSForegroundColorAttributeName:[UIColor redColor],
                                 NSBackgroundColorAttributeName:[UIColor yellowColor],
                                 NSFontAttributeName:[UIFont systemFontOfSize:20]};
    
    [attriStr addAttributes:attriBute range:NSMakeRange(0, 5)];
   
    //添加图片到指定的位置
    NSTextAttachment *attchImage = [[NSTextAttachment alloc] init];
    
    attchImage.image = [UIImage imageNamed:@"Pikachu"];
    attchImage.bounds = CGRectMake(0, 0, 100, 70);
    
    NSAttributedString *stringImage = [NSAttributedString attributedStringWithAttachment:attchImage];
    //若不添加下面这句话,默认会把图片排在文字末尾
    [attriStr insertAttributedString:stringImage atIndex:20];
    
    //添加超链接
    [attriStr addAttribute:NSLinkAttributeName value:@"homePage://" range:[[attriStr string] rangeOfString:@"《个人主页》"]];

    
    UITextView * textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 100, WIDTH - 40, HEIGHT - 200)];
    
    textView.backgroundColor =[UIColor whiteColor];
    textView.delegate = self;
    textView.editable = NO;
    [self.view addSubview:textView];

    textView.attributedText = attriStr;
}

//代理方法
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
   
    NSLog(@"url :%@",URL);
    
    if ([[URL scheme] isEqualToString:@"homePage"]) {
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://www.jianshu.com/u/a79a9392d831"]]];

        return NO;
    }
    
    return YES;
}

上一篇下一篇

猜你喜欢

热点阅读