iOS之UILabel和UITextView富文本操作

//
//  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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值