text去掉乱码 设置不同颜色 行高 自定义大小

#import <Foundation/Foundation.h>

@interface TextsForRow : NSObject

@property(nonatomic,copy)NSString * string;

/**
文本包含了 标题+文本。 使用前设置内容的颜色
操作中:标题设置颜色。文本颜色 标题+文本字体大小 行间距 以及返回高度 @param stringTitle title文本
@param colorTitle title颜色
@param stringText 内容text
@return 返回数组3个。 1 返回的 NSMutableAttributedString * strAttebute;;2返回的 宽度0.2f的string,使用时转化 3:高度
*/
+(NSArray *)TextsForRowWithStringTitle:(NSString*)stringTitle ColorWith:(UIColor*)colorTitle textWithStringText:(NSString*)stringText;
@end
#import "TextsForRow.h"

@implementation TextsForRow

+(NSArray *)TextsForRowWithStringTitle:(NSString*)stringTitle ColorWith:(UIColor*)colorTitle textWithStringText:(NSString*)stringText{

    //title+text
NSString * str1 =[NSString stringWithFormat:@"%@%@",stringTitle,stringText]; //删除不需要的个别字符
NSString * str = [str1 stringByReplacingOccurrencesOfString:@"<DIV>" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@"</DIV>" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@"<BR>" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@"</BR>" withString:@""]; //删除讨厌的字符
NSRegularExpression * regu2 =[NSRegularExpression regularExpressionWithPattern:@"(?:<|</|>|&nbsp;)" options:NSRegularExpressionCaseInsensitive error:nil];
NSString *string3 = [regu2 stringByReplacingMatchesInString:str options:NSMatchingReportProgress range:NSMakeRange(, str.length) withTemplate:@""]; //去掉左右两边的空格
NSString * kongge = [string3 stringByReplacingOccurrencesOfString:@" " withString:@""]; //去掉左右两边的空格
NSString * string = [kongge stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; NSMutableAttributedString * strAttebute = [[NSMutableAttributedString alloc] initWithString:string ];
//设置title颜色
[strAttebute addAttribute:NSForegroundColorAttributeName value:colorTitle range:NSMakeRange(, stringTitle.length)]
;
//行间距
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc]init];
[paragraphStyle setLineSpacing:IPHONEHIGHT()];
[strAttebute addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(, string.length)]; //自定义大小
CGSize contentSize = [string boundingRectWithSize:CGSizeMake(ScreenWidth-IPHONEWIDTH(),MAXFLOAT ) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:IPHONEWIDTH()]} context:NULL].size; CGFloat height = contentSize.height; //进行返回
NSArray * array =@[strAttebute,[NSString stringWithFormat:@"%0.2f",contentSize.width],[NSString stringWithFormat:@"%0.2f",height]]; return array; } @end

案例1:修改文本字体大小、颜色属性

比如文本展示为姓名和性别,但是我们不能排除姓名会很长,所以此刻的lable宽度我们就不能写死,换句话说lable的宽度根据文本的内容来定

我经常用两种方式解决:

1.前面文章已经涉及:lable自适应http://blog.csdn.net/tuwanli125/article/details/51003798

2.就是使用NSMutableAttributedString属性给infoL设置文本

NSString *infoStr = [NSStringstringWithFormat:@"%@ %@",name,sex];

NSMutableAttributedString *infoAttStr = [[NSMutableAttributedStringalloc] initWithString:infoStr];

NSArray *colorArr =@[[UIColorcolorWithRed:0/255.0green:168/255.0blue:255/255.0alpha:1.0],[UIColorcolorWithRed:153/255.0green:153/255.0blue:153/255.0alpha:1.0]];

--------修改姓名的颜色,字体大小------

[infoAttStr addAttribute:NSForegroundColorAttributeNamevalue:colorArr[0]range:NSMakeRange(0,name.length)];

[infoAttStr addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:15]range:NSMakeRange(0,name.length)];

--------修改性别的颜色,字体大小------

[infoAttStr addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:12]range:NSMakeRange(name.length+1,sexStr.length)];

[infoAttStr addAttribute:NSForegroundColorAttributeNamevalue:colorArr[1]range:NSMakeRange(name.length+1,sexStr.length)];

[self.infoL setAttributedText:infoAttStr];

这样一个文本就可以了,简单快捷

案例2:文本行间距

remindLabel.text = @""(一堆文字,此处省略一万字)

NSMutableAttributedString *attributedString = [[NSMutableAttributedStringalloc]initWithString:remindLabel.text];;

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStylealloc]init];

[paragraphStyle setLineSpacing:9];

paragraphStyle.maximumLineHeight = 60;  //最大的行高

[paragraphStyle setFirstLineHeadIndent:30];//首行缩进

[attributedString addAttribute:NSParagraphStyleAttributeNamevalue:paragraphStylerange:NSMakeRange(0, remindLabel.text.length)];

remindLabel.attributedText = attributedString;

文本行间距 以及自定义高度

NSMutableAttributedString * strAttebute = [[NSMutableAttributedString alloc] initWithString:check1];

//设置行间距

NSMutableParagraphStyle * paragraphStlyle = [[NSMutableParagraphStyle alloc] init];

[paragraphStlyle setLineSpacing:IPHONEHIGHT(10)];

[strAttebute addAttribute:NSParagraphStyleAttributeName value:paragraphStlyle range:NSMakeRange(0, check1.length)];

c2ell.labelText.attributedText = strAttebute;

CGSize contentSize = [c2ell.labelText.text  boundingRectWithSize:CGSizeMake(ScreenWidth-IPHONEWIDTH(28*3+140),MAXFLOAT ) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:IPHONEWIDTH(30)],NSParagraphStyleAttributeName:paragraphStlyle} context:NULL].size;

c2ell.labelText.size =CGSizeMake(contentSize.width, contentSize.height);

案例3:添加下划线

我给按钮添加下滑线,比如按钮显示文本为电话号码,点击就可以拨打电话

NSMutableAttributedString *str = [[NSMutableAttributedStringalloc]initWithString:_phoneBtn.titleLabel.text];

NSRange strRange = {0,[strlength]};

[str addAttribute:NSUnderlineStyleAttributeNamevalue:[NSNumbernumberWithInteger:NSUnderlineStyleSingle]range:strRange];

[_phoneBtnsetAttributedTitle:strforState:UIControlStateNormal]

UILabletext去掉乱码 控制颜色 行高 自定义大小 。显示不同的字体颜色、字体大小、行间距、首行缩进、下划线等属性(NSMutableAttributedString)的更多相关文章

  1. 行高 line-height

    一.行高的定义 line-height(行高):两行文字基线之间的距离 1.什么是基线? 2.为何是基线? 3.需要两行吗? 1.什么是基线? 我们上学的时候都用过,抄写英文字母的时候.其中有一条红线 ...

  2. WEB学习-CSS行高、字体,链接的美化以及背景

    行高和字号 CSS中,所有的行,都有行高.盒模型的padding,绝对不是直接作用在文字上的,而是作用在“行”上的. 单行文本垂直居中 文本在行里面是居中 其中,行高:盒子高; 需要注意的是,这个小技 ...

  3. cssline-height行高 全解

    1.  基线.底线.顶线 2.  行距.行高 3.  内容区 4.  行内框 5.  行框 元素对行高的影响 扩展阅读 1.  基线.底线.顶线 行高指的是文本行的基线间的距离. 基线并不是汉字的下端 ...

  4. CSS行高line-height的一些深入理解及应用

    一.一些字面意思. “行高”大约是指:一行文字的高度.具体来说是指两行文字间基线之间的距离.基线是在英文字母中用到的一个概念,我们刚学英语使用的那个英语本子每行有四条线,其中底部第二条线就是基线,是a ...

  5. 【转】css行高line-height的一些深入理解及应用

    一.前言 前两天在腾讯ISD团队博客上看到一篇翻译的文章“深入理解css 行高”,是个不错的文章,学到了不少东西,建议您看看. 这里,我也要讲讲我对line-height的一些理解,所讲解的东西绝大多 ...

  6. 理解css行高(line-height)

    首先我们要明确 line-height 的定义,line-height指的是两条文字基线之间的距离. 行内框盒子模型 所有内联元素的样式表现都与行内框盒子模型有关.所以这个概念是非常重要的. < ...

  7. CSS——行高

    浏览器默认文字大小:16px 行高:是基线与基线之间的距离 行高=文字高度+上下边距 一行文字行高和父元素高度一致的时候,垂直居中显示. <!DOCTYPE html> <html& ...

  8. CSS行高line-height的学习

    一.定义和用法 line-height 属性设置行间的距离(行高). 可能的值 normal默认.设置合理的行间距. number设置数字,此数字会与当前的字体尺寸相乘来设置行间距. length设置 ...

  9. css行高的用法总结

    css没有提供一个直接设置行间距的方式,所以只能通过设置行高来间接的设置行间距,行高越大行间距就越大,用 line-height 来设置行高. .p1 { /* 设置行高 */ line-height ...

随机推荐

  1. 解决:org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error evaluating expression 'requestMap.maintenancename != null and requestMap.maintenance

    异常如下:org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.Builde ...

  2. install plugin group_replication ERROR 1126 (HY000)

    在给MySQL安装插件的时候,你可能会遇到如题所示的报错. 更详细的错误输出如下: mysql> INSTALL PLUGIN group_replication SONAME 'group_r ...

  3. 圆形的ImageView

    转载自gitHub的ImageView,因为本身就是可用的,也没什么好说的,拷贝回去用就是了,可以设置除了背景,还可以设置边框什么的,比起CardView设置圆角,功能更加强大. import and ...

  4. day01_HTML

    今日任务 网站信息页面案例 网站图片信息页面案例 网站友情链接页面案例 网站首页案例 网站后台页面案例 教学目标 了解什么是标记语言 了解HTML的框架标签 掌握HTML的主要标签(字体,图片,列表, ...

  5. 制作支持 BIOS+UEFI 的 U 盘 grub2+bootmgr 引导 + deepin_recovery + deepin_iso + win_pe

    网盘下载:https://pan.baidu.com/s/1c2GXPo0 U盘为 FAT32,MBR分区表 1.下载:U盘grub2+bootmgr引导2017.12.6.2.7z 2.解压到 U盘 ...

  6. 高可用的Spring FTP上传下载工具类(已解决上传过程常见问题)

    前言 最近在项目中需要和ftp服务器进行交互,在网上找了一下关于ftp上传下载的工具类,大致有两种. 第一种是单例模式的类. 第二种是另外定义一个Service,直接通过Service来实现ftp的上 ...

  7. Webstorm 快捷键大全 整理收录

    "工欲善其事,必先利其器" 作为一名开发人员,好用的工具能让你效率更高,剩下的时间用来偷懒吹牛逼吧... 以下整理Webstorm快捷键大全   Windows版本 注释(// 或 ...

  8. netty源码分析

    1.Netty是由JBOSS提供的一个java开源框架.Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服务器和客户端程序.也就是说,Netty 是一个基于N ...

  9. Git快速入门和常用命令

    一.快速入门 本地初始化一个项目 首先,你需要执行下面两条命令,作为 git 的基础配置,作用是告诉 git 你是谁,你输入的信息将出现在你创建的提交中. git config --global us ...

  10. 摘要算法---hashlib模块下MD5和SHA的使用

    作用: 任意长度的字符串内容通过摘要算法都可以生成唯一序列摘要值,通过摘要算法,可以校验某个文档或者某组字符串是否被修改. 应用: 1.文件内容一致性校验 2.用户登录验证 常用方法 update() ...