天天看点

IOS 中自定义字符串,又叫属性字符串NSMutableAttributedString

第一种普通的属性字符串:

  NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", ((MyDataModel *)self.myDataArr[0]).friend_num]];

                        [str1 setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} range:NSMakeRange(0, [[((MyDataModel *)self.myDataArr[0]).friend_num description] length])];

//这里需要说明的是,首先我们先要创建或是定义一个属性字符串str1,并且用我们要改变一些属性的字符串初始化这个属性字符串,我这里是用我请求回来的((MyDataModel *)self.myDataArr[0]).friend_num这个字符串初始化了,(因为我要改变请求回来数据的样式)。

//第一步我们仅仅是给我们定义的属性字符串分配内存和初始化了,简而言之就是它和我们一般的字符串还是一样的,没有什么不一样,为了让它和一般的NSString字符串不一样,我们就要赋予它一些属性(改变它的一些属性),达到我们想要的效果。

//setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]这句代码就是我们要让字符串达到的颜色(我这里是让请求回来的数据显示为红色),

range:NSMakeRange(0, [[((MyDataModel *)self.myDataArr[0]).friend_num description] length])这句代码的意思是我们要让我们[[((MyDataModel *)self.myDataArr[0]).friend_num这个字符串从哪个位置开始变位红色,(我这里因为friend_num就是一个数字,所以我让它从第一位就变红,到最后,即这个字符串的长度).

第二种属性字符串(复杂一些的有两种颜色的字符串):

例 :NSString *str =  “ 文章6/微博9”;//这里的“6”和“9”是网络请求回来的,因为我是用storyboard开发的,我拖出来的属性这个string是一个label,当然也可以拖出4个lable,

            NSString *s1 = [NSString stringWithFormat:@"%@", ((MyDataModel *)self.myDataArr[0]).collect_article_num];//这里的collect_article_num就是上面的“6”

            NSString *s2 = [NSString stringWithFormat:@"%@", ((MyDataModel *)self.myDataArr[0]).collect_twitter_num];//这里的collect_twitter_num就是上面的“9”

            NSInteger l1 = [s1 length];

            NSInteger l2 = [s2 length];

            NSMutableArray *muArr = [NSMutableArray arrayWithObjects:@[@(2), @(l1)], @[@(5+l1), @(l2)], nil];//这里的@(2),表示的是下面“文章”的长度, @(5+l1)表示的是“文章6/微博”的长度加上"6"( 即l1,l1的长度不确定,因为这个字符串是请求回来的,可能为1,也可能为10,)

            NSMutableAttributedString *muStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@/%@%@",@"文章",((MyDataModel *)self.myDataArr[0]).collect_article_num,@"微博",[NSString stringWithFormat:@"%@",((MyDataModel *)self.myDataArr[0]).collect_twitter_num]]];

            NSMutableArray *myMuArr = [NSMutableArray arrayWithObjects:muStr, nil];

            for (int i = 1; i < 2; i++) {

                switch (i) {

                        break;

                    case 1:

                    {

                        s1 = [NSString stringWithFormat:@"%@", ((MyDataModel *)self.myDataArr[0]).collect_article_num];

                        s2 = [NSString stringWithFormat:@"%@", ((MyDataModel *)self.myDataArr[0]).collect_twitter_num];

                        l1 = [s1 length];

                        l2 = [s2 length];

                        [muArr replaceObjectsInRange:NSMakeRange(0, muArr.count) withObjectsFromArray:@[@[@(2), @(l1)], @[@(5+l1),@(l2)]]];//这行代码的意思就是用现在这个数组完全代替上面的muArr

                    }

                        break;

                }

                [strs replaceObjectAtIndex:i withObject:[self changeString:,myMuStr[i] array:muArr]];//这行代码的意思是调用下面封装的方法,用muArr这个数组传进去,通过方法改变字符串中的数字,代替上面NSMutableArray *myMuArr这个可变数组中得muArr。

            }

//封装的方法(传入一个数组即可达到我们想要达到的效果)

- (NSMutableAttributedString *)changeString:(NSAttributedString *)str array:(NSArray*)array

{

    NSMutableAttributedString *mutable = [[NSMutableAttributedString alloc] initWithAttributedString:str];

    for (int i = 0; i < array.count; i++) {

        NSRange range = NSMakeRange([array[i][0] integerValue], [array[i][1] integerValue]);//这里用到了二维数组(@[@(2), @(l1)]),array[0][0]就是@(2),也就是“文章”两字,[array[1][1]就是@(l1)。

//补充一点的是,这里定义的NSRange 就是未来让传进来的数组里的元素从那个位置开始改变颜色。因为下面的setAttributes方法要求传入一个NSRange变量。

        [mutable setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} range:range];

    }

OK,以上是我最近开发项目中遇到的属性字符串,希望能帮到大家。  

    return mutable;

}