1.简单设置带属性的字符串

定义一个NSMutableAttributedString带属性的字符串

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"hello[1_1] world![哈哈][微笑]"];

设置属性

[str setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:60], NSForegroundColorAttributeName:[UIColor redColor], NSBackgroundColorAttributeName:[UIColor greenColor], NSUnderlineColorAttributeName:[UIColor blueColor], NSUnderlineStyleAttributeName:@(NSUnderlineStyleDouble)} range:NSMakeRange(0, 5)];

显示

_label.attributedText = str;

2. 用表情代替带[]的文字(qq会话,微信会话)

定义正则表达式

NSString *pattern = @"\\[[\u4E00-\u9FA5]+\\]";

NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];

NSString *text = @"hello[1_1] world![哈哈]";

得到符合表达式的数组NSTextCheckingResult类型的

NSArray *resultArray = [regular matchesInString:text options:0 range:NSMakeRange(0, text.length)];

定义一个带附件的字符串

NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];

for (NSTextCheckingResult *result in resultArray) {

位置

NSRange range = result.range;

得到附件

NSTextAttachment *attach = [[NSTextAttachment alloc] init];

设置附件的图片

attach.image = [UIImage imageNamed:@"d_guzhang"];

得到附件生成的字符串

NSAttributedString *imageStr = [NSAttributedString attributedStringWithAttachment:attach];

把文字替换成表情

[attStr replaceCharactersInRange:range withAttributedString:imageStr];

}

_label.attributedText = attStr;

 

图文混排——用表情代替"[文字]"的更多相关文章

  1. UILabel图文混排显示图片和文字

    //传入文字 自动图片放在左边文字紧接着后排排布 -(void)setAttrDetailLabelWithTitle:(NSString *)title { NSMutableAttributedS ...

  2. HTML5[8]: 图文混排,图片与文字居中对齐

    <img src="image.png"><span>999</span> img { /* ...  */ vertical-align: t ...

  3. 使用android SpannableStringBuilder实现图文混排,看到许多其他

    项目开发需要达到这种效果 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZmFuY3lsb3ZlamF2YQ==/font/5a6L5L2T/fontsiz ...

  4. ios开发--图文混排(富文本)

    最近准备接一个编辑类的app,所以就查了下相关的功能,并自己试验了下: /** iOS 6之前:CoreText,纯C语言,极其蛋疼 iOS 6开始:NSAttributedString,简单易用 i ...

  5. javaWeb css图文混排

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. [Swift通天遁地]八、媒体与动画-(15)使用TextKit实现精美的图文混排效果

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  7. IOS实现UIButton图文混排、自定义按钮按下和正常状态下不同的背景颜色、根据文字长度自定义UIButton长度

    在一些项目中,我们需要自定义自己的UIButton,使Button上面同时具有图片和文字描述,实现自定义UIButton的图文混排. 首先我们需要定义一个继承自UIButton的类,同时实现自己的in ...

  8. Unity UGUI图文混排源码(三) -- 动态表情

    这里是根据图文混排源码(二)进一步修改的,其他链接也不贴了,就贴一个链接就好了,第一次看这文章的同学可以先去看看其他几篇文章 Unity UGUI图文混排源码(二):http://blog.csdn. ...

  9. CoreText实现图文混排之文字环绕及点击算法

    系列文章: CoreText实现图文混排:http://www.jianshu.com/p/6db3289fb05d CoreText实现图文混排之点击事件:http://www.jianshu.co ...

随机推荐

  1. Playground

    题意 :求被两点分割的凸包面积的较小值    题意已经给出顺时针啦 就是求以某一个点 和其他所有相邻点组成三角形的面积,然后sum存和求两点的时候就求出那两点的之间所有三角形的和再减掉0点和那两点的面 ...

  2. 参数解析argparse模块

    argparse,python的一个命令行解析模块 import argparse #创建一个命令行解析器 parser = argparse.ArgumentParser() #增添参数 parse ...

  3. ios开发之常用宏的定义

    有些时候,我们需要将代码简洁化,这样便于读代码.我们可以将一些不变的东东抽取出来,将变化的东西作为参数.定义为宏,这样在写的时候就简单多了. 下面例举了一些常用的宏定义和大家分享: 1. 判断设备的操 ...

  4. IOS 播放音频流媒体

    #pragma mark - 加载播放数据 - (void)loadData:(NSString *)musicUrl { NSURL *playURL = [NSURL URLWithString: ...

  5. 《javascript权威指南》第9章 例9-8源码

    //创建一个新的枚举类型 //不能使用它来创建该类型的新实例 function Enumeration(nameToValues){ var Enumeration = function(){thro ...

  6. Log4j MDC Tomcat下报异常org.apache.log4j.helpers.ThreadLocalMap

    严重: The web application [/qdgswx] created a ThreadLocal with key of type [org.apache.log4j.helpers.T ...

  7. Android相关类关系

    Activity Window.WindowManager View. interface----ViewManager LayoutInflater Components Activity.Serv ...

  8. PhoneGap and Titanium

    http://mobile.51cto.com/web-338270.htm http://www.udpwork.com/item/6117.html http://blog.cnbang.net/ ...

  9. python中的嵌套类(内部类调用外部类中的方法函数)

    在为书中版本是3.X的,但2.X不太支持直接调用. 所以,在PYTHON2.X中,要在内部类中调用外部类的方法,就必须得实例化外部类,然后,传入实例进行调用. 花了我两个小时啊,资料没找到,自己一个一 ...

  10. 根据SVN的MESSAGE进行多版本输出,反向排序,真是曲折~~~啊

    import os,sys,time file = open('svnlog') revList = [] tempList = [] linen = 1 for line in file: if ( ...