美文网首页
你是如何创建常用的属性文本?

你是如何创建常用的属性文本?

作者: arnoldxiao | 来源:发表于2018-12-10 18:07 被阅读3次

假设我们定义了一个 UILabel 对象,它的 frame 属性在初始化的时候就给定了,前提是有足够大的宽高显示其内容,设置其内容可换行显示。

UILabel 对象加入到视图中,并给其赋值。

1. 直接使用系统API——NSMutableAttributedString

self.textLabel.attributedText = ({
    NSString *prefix = @"我是";
    NSString *middle = @"张三";
    NSString *suffix = @"不是李四";
    NSString *introduce = [[[prefix stringByAppendingString:middle] stringByAppendingString:@"\n"] stringByAppendingString:suffix];

    NSRange prefixRange = [introduce rangeOfString:prefix];
    NSRange middleRange = [introduce rangeOfString:middle];
    NSRange suffixRange = [introduce rangeOfString:suffix];

    NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:introduce];
    [mutableAttributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:prefixRange];
    [mutableAttributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30] range:middleRange];
    [mutableAttributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:suffixRange];

    [mutableAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:prefixRange];
    [mutableAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:middleRange];
    [mutableAttributedString addAttribute:NSForegroundColorAttributeName value:UIColor.lightGrayColor range:suffixRange];

    mutableAttributedString;
});

我们可以直接运行代码看效果如何:


NSMutableAttributedString-UI.png

效果在我们预想中的那样,但是这样的代码有哪些不好的地方呢?

  1. 代码量大,这只是一个很简单的属性文本,如果稍微复杂点,代码量更是很难控制;

  2. 阅读不便,多次调用 addAttribute: value: range:,使代码阅读者眼花缭乱;

  3. NSRange的使用容易出错,如果文本中存在重复内容的文本,指定其NSRange的时候尤其要注意;

2. 使用AXAttributedString

这是我使用原生NSAttributedString封装的一个代码,采用类似 Masonry 链式语法,代码简单,阅读方便,下面贴代码:

self.textLabel.attributedText = [AXAttributedString makeAttributedString:^(AXAttributedStringMaker *make) {
    make.text(@"我不是").systemFontSize(14).foregroundColor([UIColor grayColor]);
    make.text(@"李四").foregroundColor(UIColorFromRGB(0xFF0000)).font([UIFont systemFontOfSize:30]);
    make.text(@"\n");
    make.text(@"不是王老五").systemFontSize(12).foregroundColor(UIColor.lightGrayColor);
}];

来看看效果如何:


AXAttributedString-UI.png

和直接使用NSMutableAttributedString相比,最直接的变化就是取消了NSRange的参与,这个在属性文本中最容易出Bug的地方。

当然,以上是基于静态文本的例子~

如果大家有兴趣,可以学习使用这个代码~
地址:https://github.com/arnoldxiao/AXAttributedString

欢迎大家给我提建议,如果喜欢,给我个star最好啦~

相关文章

  • 你是如何创建常用的属性文本?

    假设我们定义了一个 UILabel 对象,它的 frame 属性在初始化的时候就给定了,前提是有足够大的宽高显示其...

  • UILabel

    1.功能:UILabel是一个用来展示纯文本的UI控件 2.如何创建 & 基本属性设置: 3.其他常用属性:

  • CSS-多列布局1-概述

    1、多列布局概述 通过 CSS3,您能够创建多个列来对文本进行布局 - 就像报纸那样! 2、常用属性 3、常用属性...

  • DOM操作

    增 可以增加 Element,元素Attribute,属性Text,文本 DOM节点创建最常用的便是documen...

  • 2018-07-10 CSS样式表

    常用的样式属性(文本属性): 常用的样式属性(背景属性): 常用的样式属性(方框属性): 常用的样式属性: 内嵌样...

  • 关于objectiveC 中的 label

    UILable是iPhone界面最基本的控件,主要用来显示文本信息。 常用属性和方法有: 1、创建 CGRect ...

  • UILabel详解

    ·UILable是iPhone界面最基本的控件,主要用来显示文本信息。 ·常用属性和方法有: 1、创建 CGRec...

  • UILabel的详细使用

    ·UILable是iPhone界面最基本的控件,主要用来显示文本信息。 ·常用属性和方法有: 1、创建 CGRec...

  • UILabel

    UILable是iPhone界面最基本的控件,主要用来显示文本信息。 常用属性和方法有: 1、创建 2、text ...

  • UILabel的使用

    简介 UILabel是iOS中最基本的控件,主要用来显示文本信息。 常用的属性和方法 1.创建 OC Swift ...

网友评论

      本文标题:你是如何创建常用的属性文本?

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