//
Nsstring+range.h
String job
//
Created by Andezhou on 15/12/17.
COPYRIGHT©2015 year Zhou Ande. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSString (Range)
Intercept Topic # #
-(Nsarray *) querytopicfromstring: (NSString *) topic;
capturing emoticons [\]
-(Nsarray *) querysmiliesfromstring: (NSString *) fromstr
TOSTR: (NSString *) tostr;
Intercept @name
-(Nsarray *) queryname;
@end
/*************************************************/
//
Nsstring+range.m
String job
//
Created by Andezhou on 15/12/17.
COPYRIGHT©2015 year Zhou Ande. All rights reserved.
//
#import "Nsstring+range.h"
@implementation NSString (Range)
Intercept Topic # #
-(Nsarray *) querytopicfromstring: (NSString *) topic
{
Initialize an array that holds a range
Nsmutablearray *array = [Nsmutablearray array];
return [self querystringfrom:topic to:topic array:array];
}
capturing emoticons [\]
-(Nsarray *) querysmiliesfromstring: (NSString *) fromstr
TOSTR: (NSString *) tostr
{
Nsmutablearray *array = [Nsmutablearray array];
return [self querystringfrom:fromstr to:tostr array:array];
}
Intercept @name
-(Nsarray *) queryname
{
Nsmutablearray *array = [Nsmutablearray array];
return [self querystringfrom:@ "@" to:@ "" Array:array];
}
-(Nsarray *) Querystringfrom: (NSString *) from to: (NSString *) to array: (Nsmutablearray *) array
{
Gets the location where the start # is located
Nsrange Fromrange = [self rangeofstring:from];
if (fromrange.location! = nsnotfound) {
Get Start Index
Nsuinteger FromIndex = fromrange.location + fromrange.length;
To intercept a string starting at the index
NSString *fromstr = [self substringfromindex:fromindex];
Gets the position of # corresponding to #
Nsrange Torange = [Fromstr rangeofstring:to];
Determine if the corresponding # exists
if (torange.location! = nsnotfound) {
Get # #之间的标题
NSString *str = [Fromstr substringToIndex:toRange.location];
Get the complete title string, such as # University is the plastic Surgery House #
NSString *string = [NSString stringwithformat:@ "%@%@%@", from, str, to];
NSLog (@ "string:%@", string);
Gets the position of the completed caption string in the string
Nsrange range = [self rangeofstring:string];
First determine if the previous one exists
Nsvalue *value = [array lastobject];
if (value.rangeValue.length) {
Position in range of current topic add the position and length of range on the previous topic
Nsuinteger location = Value.rangeValue.length + value.rangeValue.location;
Range = Nsmakerange (range.location + location, range.length);
}
Convert range to nsvalue stored in array
[Array Addobject:[nsvalue valuewithrange:range];
Get the title # #后面的内容
NSString *nextstr = [fromstr substringFromIndex:toRange.location + torange.length];
Continue querying recursively
[Nextstr querystringfrom:from to:to Array:array];
}
}
return array;
}
@end
/*******************************************************************/
//
Viewcontroller.m
Homework
//
Created by Andezhou on 16/1/4.
Copyright (c) 2016 Zhou Ande. All rights reserved.
//
#import "ViewController.h"
#import "Nsstring+range.h"
@interface Viewcontroller ()
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
NSString *str = @ "Haha # The university is the plastic Surgery House # Years is to kill a pig knife or plastic knife [/embarrassing], recently netizens have/Sun out their own university before and after the contrast, simply the rhythm of collective plastic surgery [/happy]. @ Ball Ball You're right, aren't you? [/sinister] #开心一刻 # @ xiaoming #你会不会 # Oh, I can't. ";
Nsarray *topicarray = [str querytopicfromstring:@ "#"];
Nsarray *namearray = [Topicarray arraybyaddingobjectsfromarray:[str queryname];
Nsarray *smiliearray = [NameArray arraybyaddingobjectsfromarray:[str querysmiliesfromstring:@ "[/" toStr:@ "]"] ";
nsmutableattributedstring *mutstring = [[Nsmutableattributedstring alloc] initwithstring:str];
To add a color
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 (Ten, Size.width, size.height);
[Self.view Addsubview:lab];
Additional setup after loading the view, typically from a nib.
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}
@end
/**********************************************************************/
iOS common technology-interception of Weibo strings