//
// NSString+Range.h
// 字符串作业
//
// Created by andezhou on 15/12/17.
// Copyright © 2015年 周安德. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSString (Range)
// 截取话题##
- (NSArray *)queryTopicFromString:(NSString *)topic;
// 截取表情符号[\]
- (NSArray *)querySmiliesFromString:(NSString *)fromStr
toStr:(NSString *)toStr;
// 截取@name
- (NSArray *)queryName;
@end
/*************************************************/
//
// NSString+Range.m
// 字符串作业
//
// Created by andezhou on 15/12/17.
// Copyright © 2015年 周安德. All rights reserved.
//
#import "NSString+Range.h"
@implementation NSString (Range)
// 截取话题##
- (NSArray *)queryTopicFromString:(NSString *)topic
{
// 初始化一个存放range的数组
NSMutableArray *array = [NSMutableArray array];
return [self queryStringFrom:topic to:topic array:array];
}
// 截取表情符号[\]
- (NSArray *)querySmiliesFromString:(NSString *)fromStr
toStr:(NSString *)toStr
{
NSMutableArray *array = [NSMutableArray array];
return [self queryStringFrom:fromStr to:toStr array:array];
}
// 截取@name
- (NSArray *)queryName
{
NSMutableArray *array = [NSMutableArray array];
return [self queryStringFrom:@"@" to:@" " array:array];
}
- (NSArray *)queryStringFrom:(NSString *)from to:(NSString *)to array:(NSMutableArray *)array
{
// 获取开始#所在的位置
NSRange fromRange = [self rangeOfString:from];
if (fromRange.location != NSNotFound) {
// 获取开始索引
NSUInteger fromIndex = fromRange.location + fromRange.length;
// 从索引开始截取字符串
NSString *fromStr = [self substringFromIndex:fromIndex];
// 获取与#所对应的#的位置
NSRange toRange = [fromStr rangeOfString:to];
// 判断所对应的#是否存在
if (toRange.location != NSNotFound) {
// 得到##之间的标题
NSString *str = [fromStr substringToIndex:toRange.location];
// 得到完整的标题字符串,如#大学是所整容院#
NSString *string = [NSString stringWithFormat:@"%@%@%@", from, str, to];
NSLog(@"string:%@", string);
// 得到完成的标题字符串在字符串中的位置
NSRange range = [self rangeOfString:string];
// 先判断上一个是否存在
NSValue *value = [array lastObject];
if (value.rangeValue.length) {
// 当前话题的range中的位置添加上上一个话题的range的位置和长度
NSUInteger location = value.rangeValue.length + value.rangeValue.location;
range = NSMakeRange(range.location + location, range.length);
}
// 把range转化为NSValue存放在数组中
[array addObject:[NSValue valueWithRange:range]];
// 获取标题##后面的内容
NSString *nextStr = [fromStr substringFromIndex:toRange.location + toRange.length];
// 递归继续查询
[nextStr queryStringFrom:from to:to array:array];
}
}
return array;
}
@end
/*******************************************************************/
//
// ViewController.m
// 作业
//
// Created by andezhou on 16/1/4.
// Copyright (c) 2016年 周安德. All rights reserved.
//
#import "ViewController.h"
#import "NSString+Range.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *str = @"哈哈#大学是所整容院# 岁月是把杀猪刀还是整容刀[/尴尬],最近网友纷纷/晒出自己大学前后的对比照,简直集体整容的节奏[/开心]。@球球 你说对不对呢?[/阴险] #开心一刻# @小明 #你会不会# 啊 我不会啊。";
NSArray *topicArray = [str queryTopicFromString:@"#"];
NSArray *nameArray = [topicArray arrayByAddingObjectsFromArray:[str queryName]];
NSArray *smilieArray = [nameArray arrayByAddingObjectsFromArray:[str querySmiliesFromString:@"[/" toStr:@"]"]];
NSMutableAttributedString *mutString = [[NSMutableAttributedString alloc] initWithString:str];
// 给添加颜色
for (NSValue *value in smilieArray) {
NSRange range = value.rangeValue;
[mutString addAttributes:@{
NSForegroundColorAttributeName : [UIColor blueColor]
}
range:range];
}
UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 0, 0)];
lab.numberOfLines = 0;
lab.attributedText = mutString;
CGSize size = [lab sizeThatFits:CGSizeMake(self.view.frame.size.width - 20, MAXFLOAT)];
lab.frame = CGRectMake(10, 100, size.width, size.height);
[self.view addSubview:lab];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
/**********************************************************************/