NSMutableAttributedString的使用
1.设置字符串中数字字符串的颜色:
// 设置字符串中数字的颜色
- (void)setTextColor:(UILabel *)label FontNumber:(id)font AndRange:(NSRange)range AndColor:(UIColor *)vaColor
{
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:label.text]; NSCharacterSet* nonDigits =[[NSCharacterSet decimalDigitCharacterSet] invertedSet];
int remainSecond =[[label.text stringByTrimmingCharactersInSet:nonDigits] intValue];
NSLog(@" num %d ",remainSecond); NSString *labelText = label.text; for (int i = ; i < labelText.length; i ++) {
//这里的小技巧,每次只截取一个字符的范围
NSString *a = [labelText substringWithRange:NSMakeRange(i, )];
//判断装有0-9的字符串的数字数组是否包含截取字符串出来的单个字符,从而筛选出符合要求的数字字符的范围NSMakeRange
NSArray *number = @[@"",@"",@"",@"",@"",@"",@"",@"",@"",@""];
if ([number containsObject:a]) {
[str setAttributes:@{NSForegroundColorAttributeName:SELECT_PERSON_BUTTON_COLOR,NSFontAttributeName:FONT_13} range:NSMakeRange(i, )];
}
}
label.attributedText = str;
}
2.截取某个字符串段,设置字体颜色:
- (void)setBlueColorStringWithText:(NSMutableString *)text {
NSRange startRange = [text rangeOfString:@"("];
NSRange endRange = [text rangeOfString:@")"];
NSRange range = NSMakeRange(startRange.location + startRange.length -1, endRange.location - startRange.location - startRange.length + 2);
NSDictionary *attribs = @{NSForegroundColorAttributeName: SELECT_PERSON_BUTTON_COLOR,
NSFontAttributeName:FONT_13
};
NSMutableAttributedString *textStr = [[NSMutableAttributedString alloc] initWithString:text];
[textStr setAttributes:attribs range:range];
self.deadlineLabel.attributedText = textStr;
}
NSMutableAttributedString的使用的更多相关文章
- iOS - NSMutableAttributedString富文本的实现
NSMutableAttributedString继承于NSAttributedString(带属性的字符串)能够简单快速实现富文本的效果;不多说直接上效果图和代码,通俗易懂: (一)效果图: (二) ...
- iOS中富文本NSMutableAttributedString的用法
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc]initWithString:@"我是富文 ...
- NSMutableAttributedString(转)
NSMutableAttributedString计算高度的问题 _label_page2_1 = [[UILabel alloc] init]; _label_page2_1.numberOfLin ...
- NSMutableAttributedString常用代码
NSTextAttachment *attachment = [[NSTextAttachment alloc] init]; attachment.image = [UIImage imageNam ...
- NSMutableAttributedString 的使用
NSMutableAttributedString *msAttriStr = [[NSMutableAttributedString alloc] initWithString:[NSString ...
- objective-c NSMutableAttributedString
NSMutableAttributedString 是一个很强悍的富文本处理字符串,可以方便的实现一个字符串中某个字符的样式处理.我把我下面代码实现的功能步骤说一下:首先拼接两个字符串,然后给前前半部 ...
- iOS 学习 - 12.NSMutableAttributedString 计算高度
计算 NSMutableAttributedString 高度,必须要有两个属性 -(void)test{ UILabel *label = [[UILabel alloc]initWithFrame ...
- 新浪微博客户端(35)-使用NSMutableAttributedString实现多行文本的效果
DJComposeViewController.m import "DJComposeViewController.h" #import "DJAccountTool.h ...
- NSMutableAttributedString可变属性字符串的用法
适用于:当你想对一个字符串中的某几个字符更改颜色,字体... NSString *string = @"今日营养配餐提供热量1800千卡,需要饮食之外额外补充钙10mg,铁20mg,锌9.5 ...
随机推荐
- Unable to install breakpoint in
Unable to install breakpoint inXXXX due to missing line number attributes.modify compiler options to ...
- setFeatureInt、android 自定义标题栏
Android 自带的toolbar 往往不能很好的的满足我们的个性化要求.因此我们经常使用自定的的标题栏.而Android系统本身也允许我们自定以标题栏. 记录一下,自定义标题栏常遇到的问题.先上效 ...
- leetcode-【中等题】228. Summary Ranges
题目: 228. Summary Ranges Given a sorted integer array without duplicates, return the summary of its r ...
- 《Linux企业应用案例精解(第2版)》新书发售啦
本书在出版当年就获得了不错的销量,同时被中国科学院国家科学图书馆.中国国家图书馆.首都图书馆.清华大学.北京大学等上百所国内综合性大学图书馆收录为馆藏图书,在IT业界赢得了良好的口碑.随后2012年年 ...
- Bootstrap表单
Bootstrap 提供了下列类型的表单布局: 垂直表单(默认) -> 这个不好看,都是手机版了,PC版占一排不好看: 内联表单 -> 我相信这个才是你想要的,PC版响应横排,手机版响应竖 ...
- js数组的操作
很早之前整理的一篇文章,感觉比较清晰. 一.数组元素的操作 1.数组的创建 var arrayObj = new Array(); var arrayObj = new Array(size); // ...
- dojo GridX 用法
1. 表格的加载显示 function CreateGrid() { var store = new dojo.store.Memory({ data: [ { id: 1, UserName: &q ...
- [python学习] 介绍python的property,以及为什么要用setter,一个小栗子
python中的property是比较好用的. 先来一段代码 #-*- coding:utf-8 -*- class C(object): status_dict = { 1: 'accept', 2 ...
- C语言PIC16 serial bootloader和C#语言bootloader PC端串口通信程序
了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 新PIC16 Boot ...
- 9、SQL Server 操作数据
插入数据 使用Insert Into 插入 if(exists(select * from sys.databases where name = 'webDB')) drop database web ...