iOS之UILabel和UITextView富文本操作

作者: 乐此不疲吶 | 来源:发表于2016-05-23 22:58 被阅读4293次

    由于最近遇到的项目需要用到富文本开发,主要的也就是这些,有些属性可以按着command键指着对应属性一枪进去看看其他的属性,在此部多赘述

    //
    //  ViewController.m
    //  NSMutableAttributedString
    //
    //  Created by WangLe on 16/5/23.
    //  Copyright © 2016年 WangLe. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()<UITextViewDelegate>
    
    @property (nonatomic, strong) UITextView * textView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        _textView = [[UITextView alloc]initWithFrame:CGRectMake(20, 50, 200, 200)];
        UILabel * myLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 300, 200, 200)];
        myLabel.backgroundColor = [UIColor yellowColor];
        _textView.backgroundColor = [UIColor yellowColor];
        [self.view addSubview:myLabel];
        [self.view addSubview:_textView];
        // 定义一个可变属性字符串对象
        NSMutableAttributedString * str = [[NSMutableAttributedString alloc]initWithString:@"缓缓飘落的枫叶像思念我点燃烛火温暖岁末的秋天激光掠过天边被风掠过想你的思念"];
        // 设置字体大小 range是设置范围,下同
        [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, 5)];
        // 设置字体颜色
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(2, 5)];
        // 设置下划线
        [str addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(3, 7)];
        // 设置字体样式
        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Geeza Pro" size:25] range:NSMakeRange(5, 5)];
        //NSLog(@"字体集合%@",[UIFont familyNames]);
        // 删除线 常用于划掉原价
        [str addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(8, 5)];
        // 删除线的颜色(先设置删除线再设置颜色)
        [str addAttribute:NSStrikethroughColorAttributeName value:[UIColor redColor] range:NSMakeRange(8, 5)];
        // 设置空心字
        [str addAttribute:NSStrokeWidthAttributeName value:@1 range:NSMakeRange(18, 5)];
        // 插入图片
        NSTextAttachment * att = [[NSTextAttachment alloc]init];
        att.image = [UIImage imageNamed:@"2"];
        NSAttributedString * attStr = [NSAttributedString attributedStringWithAttachment:att];
        [str insertAttributedString:attStr atIndex:25];
        // 添加链接
        [str addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"http://www.baidu.com"] range:NSMakeRange(30, 6)];
        
        // 创建字体段落 行间距 格式
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing = 50;
        paragraphStyle.firstLineHeadIndent = 30;// 设置为字体大小大两倍
    //    _textView.attributedText = [[NSAttributedString alloc] initWithString:str.string attributes:@{
    //                                                                        NSFontAttributeName:[UIFont systemFontOfSize:15],
    //                                                                        NSParagraphStyleAttributeName:paragraphStyle
    //                                                                                                }];
        [str addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, str.string.length)];
        // 这句不能写前面,不然没效果
        _textView.attributedText = [str copy];
        _textView.editable = NO;
        _textView.delegate = self;
        myLabel.attributedText = str;
        myLabel.numberOfLines = 0;
    }
    
    /**
     *  点击图片触发代理事件
     */
    - (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange
    {
        NSLog(@"图片%@", textAttachment);
        return NO;
    }
    
    /**
     *  点击链接,触发代理事件
     */
    - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
    {
        [[UIApplication sharedApplication] openURL:URL];
        return YES;
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    

    效果图如下


    05232225.png

    相关文章

      网友评论

        本文标题:iOS之UILabel和UITextView富文本操作

        本文链接:https://www.haomeiwen.com/subject/pfxzrttx.html