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 = 0; i < labelText.length; i ++) {
        //这里的小技巧,每次只截取一个字符的范围
        NSString *a = [labelText substringWithRange:NSMakeRange(i, 1)];
        //判断装有0-9的字符串的数字数组是否包含截取字符串出来的单个字符,从而筛选出符合要求的数字字符的范围NSMakeRange
        NSArray *number = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
        if ([number containsObject:a]) {
            [str setAttributes:@{NSForegroundColorAttributeName:SELECT_PERSON_BUTTON_COLOR,NSFontAttributeName:FONT_13} range:NSMakeRange(i, 1)];
        }
    }
    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;
}

  

时间: 2024-08-29 01:21:14

NSMutableAttributedString的使用的相关文章

NSAttributedString可以强制转换为NSMutableAttributedString类型吗?下面这代码有什么问题 为什么报错

1 -(void)insetEmotion:(EmotionModel*)emotionModel{ 2 if(emotionModel.code){ 3 /** 4 在TextView中插入图片首选要知道光标的位置 比如在一段文字中间 或着 一段文字末尾 5 6 */ 7 //self.statusTextView.text = emotionModel.chs; 8 /** 9 这个方法会自动将传入的参数插入到光标的位置 10 现在插入的是对应的文字信息 以后会把这个信息发送给新浪 11 现

NSMutableAttributedString(改变文字颜色)

//类型 //创建一个label   UILabel *label1=[[UILabel alloc]initWithFrame:CGRectMake(130, 60,250, 150)]; [self.window addSubview:label1];     [email protected]"评分:7.1   (4925评论)\n20140901        date\n126min         runtime\n动作/冒险/惊悚        genres\n美国|法国     

iOS使用NSMutableAttributedString 实现富文本(不同颜色字体、下划线等)

在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘UILabel的textLayer,有的是用html5实现的,都比较麻烦,而且很多UILabel的属性也不起作用了,效果都不理想.后来了解到NSMuttableAttstring(带属性的字符串),上面的一些需求都可以很简便的实现. 实例化方法和使用方法 实例化方法: 使用字符串初始化 - (id)initWithString:(NSString *)str; 例: N

iOS UI基础-17.0 UILable之NSMutableAttributedString

在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘UILabel的textLayer,有的是用html5实现的,都比较麻烦,而且很多UILabel的属性也不起作用了,效果都不理想.后来了解到NSMuttableAttstring(带属性的字符串),上面的一些需求都可以很简便的实现. NSMuttableAttstring 方法 为某一范围内文字设置多个属性 - (void)setAttributes:(NSDictio

带属性的字符串 NSMutableAttributedString/NSAttributedString

由于iOS7新出的NSTextStorge是NSMutableAttributedString的子类,所以要用好NSTextStorage,首先要学好NSMutableAttributedString和NSAttributedString. 按个人的理解,NSAttributedString是一个带有属性的字符串,通过该类可以灵活地操作和呈现多种样式的文字数据. alignment //对齐方式 firstLineHeadIndent //首行缩进 headIndent //缩进 tailInd

NSMutableAttributedString可变字符串使用

//原始数据字符串 NSString *string = @"我是好人我是好人我是个好人额鹅鹅鹅鹅鹅鹅饿哇哇哇哇哇哇哇"; // 创建可变属性化字符串 NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string]; //改变字符串当中从第18位置向后的10位数的字体 UIFont *smallFont = [UIFont systemFontOfSize

NSMutableAttributedString 设置不同颜色,不同字体的String

UILabel *infoLabel = [[UILabel alloc]initWithFrame:CGRectMake(95, 20, 190, 70)]; infoLabel.backgroundColor = [UIColor clearColor]; infoLabel.textAlignment = NSTextAlignmentLeft; infoLabel.font = [UIFont systemFontOfSize:13]; infoLabel.numberOfLines =

iOS NSMutableAttributedString 简单使用

NSMutableAttributedString 部分属性介绍 /** NSFontAttributeName --- 设置字体大小 *//** NSForegroundColorAttributeName --- 设置字体颜色 *//** NSParagraphStyleAttributeName --- 设置段落格式 (暂无明显变化) *//** NSBackgroundColorAttributeName --- 设置字体的背景颜色 *//** NSLigatureAttributeNa

NSMutableAttributedString和NSAttributedString富文本的使用

- (NSMutableAttributedString *)setPromptTitle:(NSString *)title { NSString * textmessage = [NSString stringWithFormat:@"%@", title]; NSMutableAttributedString *fontString = [[NSMutableAttributedString alloc] initWithString:textmessage]; [fontStr

NSMutableAttributedString 的使用方法,设置格式

NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:title]; NSRange range = NSMakeRange(0, [attrString length]); [attrString beginEditing]; [attrString addAttribute:NSLinkAttributeName value:aURL range:range]; //