[iOS]UILabel改变某个范围的字符颜色和字体

来源:互联网 发布:复制电梯卡软件 编辑:程序博客网 时间:2024/06/09 17:12

[iOS]UILabel改变某个范围的字符颜色和字体

Demo:http://download.csdn.net/detail/u012881779/8641749

#import "ViewController.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UILabel *calculateLab; //计算@property (weak, nonatomic) IBOutlet UILabel *calculateOne;@property (weak, nonatomic) IBOutlet UILabel *calculateTwo;@property (weak, nonatomic) IBOutlet UILabel *calculateThree;@property (weak, nonatomic) IBOutlet UILabel *messageOneLab;@property (weak, nonatomic) IBOutlet UILabel *messageTwoLab;@property (weak, nonatomic) IBOutlet UILabel *netLab;       //网站@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        [self calculateAction:_calculateLab];    [self calculateAction:_calculateOne];    [self calculateAction:_calculateTwo];    [self calculateAction:_calculateThree];    [self messageAction:_messageOneLab startString:@"有" endString:@"条"];    [self messageAction:_messageTwoLab startString:@"有" endString:@"条"];    [self messageAction:_netLab startString:@"接" endString:@"网"]; }// 有*条消息- (void)messageAction:(UILabel *)theLab startString:(NSString *)start endString:(NSString *)end {    NSString *tempStr = theLab.text;    NSMutableAttributedString *strAtt = [[NSMutableAttributedString alloc] initWithString:tempStr];    [strAtt addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, [strAtt length])];    //'条''有'字符的范围    NSRange tempRange = [tempStr rangeOfString:start];    NSRange tempRangeOne = [tempStr rangeOfString:end];    //更改字符颜色    [strAtt addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(tempRange.location+1, tempRangeOne.location-(tempRange.location+1))];    //更改字体    [strAtt addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:20] range:NSMakeRange(0, [strAtt length])];    theLab.attributedText = strAtt;    }// 计算- (void)calculateAction:(UILabel *)theLab {    NSMutableArray *rangeArr = [self specialCharacter:theLab.text];    //二则算法    NSRange rangeOne;    NSRange tempRangeOne;    NSRange rangeTwo;    NSRange tempRangeTwo;    NSRange rangeThree;    NSRange tempRangeThree;    for (int i = 0; i < rangeArr.count ; i ++) {        NSMutableArray *mutArr = [rangeArr  objectAtIndex:i];        if (i == 0) {            rangeOne = [[mutArr firstObject] rangeValue];            tempRangeOne = [[mutArr lastObject] rangeValue];        }        if (i == 1) {            rangeTwo = [[mutArr firstObject] rangeValue];            tempRangeTwo = [[mutArr lastObject] rangeValue];        }        if (i == 2) {            rangeThree = [[mutArr firstObject] rangeValue];            tempRangeThree = [[mutArr lastObject] rangeValue];        }    }    NSString *tempStr = theLab.text;    NSMutableAttributedString *strAtt = [[NSMutableAttributedString alloc] initWithString:tempStr];    [strAtt addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, [strAtt length])];        //更改字符颜色    [strAtt addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(rangeOne.location, tempRangeOne.location-(rangeOne.location))];    [strAtt addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(rangeTwo.location, tempRangeTwo.location-(rangeTwo.location))];    [strAtt addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(rangeThree.location, tempRangeThree.location-(rangeThree.location))];    //更改字体    [strAtt addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:20] range:NSMakeRange(0, [strAtt length])];    theLab.attributedText = strAtt;}// 判断是否包含字符- (NSMutableArray *)specialCharacter:(NSString *)theStr {    NSArray *markArr = [[NSArray alloc] initWithObjects:@"+",@"-",@"*",@"/",@"=", nil];    NSMutableArray *rangeArr = [[NSMutableArray alloc] init];    for (int j = 0 ; j < theStr.length ; j++ ) {        NSRange oneRange = NSMakeRange(j, 1);        NSString *oneStr = [theStr substringWithRange:oneRange];        for ( int i = 0 ; i < markArr.count ; i ++ ) {            NSString *markStr = [markArr objectAtIndex:i];            if([markStr isEqualToString:oneStr]){                [rangeArr addObject:[NSValue valueWithRange:oneRange]];            }        }    }        NSMutableArray *returnArr = [[NSMutableArray alloc] init];    NSString *markLocation = @"0";    if([[rangeArr firstObject] rangeValue].location == 0){        markLocation = @"1";        if([[rangeArr lastObject] rangeValue].location == theStr.length){            markLocation = @"2";        }    }    NSInteger tempCount = rangeArr.count;    if([markLocation isEqualToString:@"0"]){        tempCount = tempCount + 1;    }        for (int i = 0; i < tempCount ; i ++) {        NSMutableArray *tempArr = [[NSMutableArray alloc] init];        if(i == tempCount - 1){            NSRange tempRan = [[rangeArr objectAtIndex:i-1] rangeValue];            [tempArr addObject:[NSValue valueWithRange:NSMakeRange(tempRan.location+1, 1)]];            [tempArr addObject:[NSValue valueWithRange:NSMakeRange(theStr.length, 1)]];                    }else if(i == 0){            NSRange tempRan = [[rangeArr objectAtIndex:i] rangeValue];            [tempArr addObject:[NSValue valueWithRange:NSMakeRange(0, 1)]];            [tempArr addObject:[NSValue valueWithRange:tempRan]];                    }else{            NSRange startRan = [[rangeArr objectAtIndex:i-1] rangeValue];            NSRange endRan = [[rangeArr objectAtIndex:i] rangeValue];            [tempArr addObject:[NSValue valueWithRange:NSMakeRange(startRan.location+1, 1)]];            [tempArr addObject:[NSValue valueWithRange:endRan]];        }        [returnArr addObject:tempArr];    }      return returnArr;}@end 

特殊情况处理:

针对于“总人数:1024人” 这种字符串包含两个“人”的情况,改变字体颜色时需要换一种思路;

/** 改变UILabel中顺序第devStart个字符与倒序第devEnd个字符之间的字符的颜色&字体 */+ (void)messageAction:(UILabel *)theLab andDevStart:(NSInteger)devStart andDevEnd:(NSInteger)devEnd andAllColor:(UIColor *)allColor andMarkColor:(UIColor *)markColor andMarkFondSize:(float)fontSize {    NSString *tempStr = theLab.text;    NSMutableAttributedString *strAtt = [[NSMutableAttributedString alloc] initWithString:tempStr];    [strAtt addAttribute:NSForegroundColorAttributeName value:allColor range:NSMakeRange(0, [strAtt length])];    if(devStart > 0){        devStart = devStart - 1;    }else{        devStart = 0;    }    NSRange startRange = NSMakeRange(devStart, 1);    NSRange endRangeOne = NSMakeRange(tempStr.length-devEnd, 1);    if(startRange.location>=endRangeOne.location){        [theLab setTextColor:allColor];        return;    }    // 更改字符颜色    NSRange markRange = NSMakeRange(startRange.location+startRange.length, endRangeOne.location-(startRange.location+startRange.length));    [strAtt addAttribute:NSForegroundColorAttributeName value:markColor range:markRange];    // 更改字体    [strAtt addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue" size:fontSize] range:markRange];    theLab.attributedText = strAtt;} 


示图:



0 0
原创粉丝点击