iOS-富文本

富文本是什么?

普通的文本展示就是label.text = @"xxxxxx",只能设置颜色、大小、背景色等等。
但是在实际开发中,我们需要用到图片/表情和文字的混合排版,那么就需要用到富文本了。
OC中提供了一个富文本类,NSAttributedString。有了他之后我们就可以用它来实现在UILabel、UIButton、UITextView上的图文混排效果,需要用到这些空间的attributedText属性。

在UILabel中

- (void)richText1
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 40)];
    [self.view addSubview:label];
    
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"爱上帝啊商店大的撒"];
    
    //给指定范围的文字添加属性
    //[string addAttributes:@{NSFontAttributeName:} range:<#(NSRange)#>]
    
    //附件
    NSTextAttachment *attach1 = [[NSTextAttachment alloc] init];
    //调整图片位置
    attach1.bounds = CGRectMake(0, 0, 20, 20);
    //设置图片
    attach1.image = [UIImage imageNamed:@"001"];
    
    [string appendAttributedString:[NSAttributedString attributedStringWithAttachment:attach1]];
    
    label.attributedText = string;
}

效果如图:


1.png

在UITextView中

在UITextView中还可以识别电话、网址、地址等等

#pragma mark - textView的富文本
- (void)richText2
{
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"fsaada http://www.baidu.com 10086 13888888888 dawa"];
    
    //多行输入框
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 40, 300, 300)];
    
    textView.backgroundColor = [UIColor lightGrayColor];
    
    //禁用编辑
    textView.editable = NO;
    
    textView.delegate = self;
    //启动自动识别电话号码或网址、邮箱
    textView.dataDetectorTypes = UIDataDetectorTypeAll;
    
    [self.view addSubview:textView];
    
    //附件
    NSTextAttachment *attach1 = [[NSTextAttachment alloc] init];
    //调整图片位置
    attach1.bounds = CGRectMake(0, 0, 20, 20);
    //设置图片
    attach1.image = [UIImage imageNamed:@"001"];
    
    //添加图片
    [attributeString appendAttributedString:[NSAttributedString attributedStringWithAttachment:attach1]];
    
    //添加一个链接
    NSString *url = @"进入百度";
    
    NSMutableAttributedString *linkString = [[NSMutableAttributedString alloc] initWithString:url];
    //添加属性
    [linkString addAttribute:NSLinkAttributeName value:@"http://www.baidu.com" range:NSMakeRange(0, url.length)];
    
    [attributeString appendAttributedString:linkString];
    
    textView.attributedText = attributeString;
}

UITextView的代理方法

#pragma mark - UITextViewDelegate
//点击url会触发
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
    //NSString *urlString = URL.absoluteString;
    
    //可以跳转到浏览器
    return YES;
}

//点击添加到输入框的NSTextAttachment对象会触发
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange
{
    //获取点击的图片
    //UIImage *image = textAttachment.image;
    
    //返回NO,系统actionSheet不会弹出
    return NO;
}
2.png
3.png

YYText

!支持高性能的异步排版和渲染
!扩展了 CoreText 的属性以支持更多文字效果
!支持 UIImage、UIView、CALayer 作为图文混排元素

详情:
YYText-GitHub

- (void)YYText
{
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"12dk990k09edq啊实打实大声道安山东安山东安山东声道安山东安山东安山东声道安山东安山东安山东声道安山东安山东安山东声道安山东安山东安山东声道安山东安山东安山东声道安山东安山东安山东"];
    //string.yy_color = [UIColor redColor];
    [string yy_setColor:[UIColor redColor] range:NSMakeRange(0, 2)];
    string.yy_lineSpacing = 10;
    [string yy_setTextHighlightRange:NSMakeRange(3, 10) color:[UIColor cyanColor] backgroundColor:[UIColor orangeColor] userInfo:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        
        //
    } longPressAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        //
    }];
    
    //创建一个
    
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"卫星地图",@"普通地图"]];
    
    NSMutableAttributedString *attributeS1 = [NSMutableAttributedString yy_attachmentStringWithContent:segment contentMode:UIViewContentModeScaleAspectFill width:200 ascent:10 descent:10];
    
    [string appendAttributedString:attributeS1];
    
    YYLabel *label = [[YYLabel alloc] initWithFrame:CGRectMake(20, 40, 350, 400)];
    
    label.backgroundColor = [UIColor lightGrayColor];
    label.numberOfLines = 0;
    label.attributedText = string;
    
    [self.view addSubview:label];
}
1.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,198评论 6 514
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,334评论 3 398
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,643评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,495评论 1 296
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,502评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,156评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,743评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,659评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,200评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,282评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,424评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,107评论 5 349
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,789评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,264评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,390评论 1 271
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,798评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,435评论 2 359

推荐阅读更多精彩内容