字符串拆分

作用:将一段文本根据url拆分出几个字符串存放在一个数组中,比如:xxxhttp://abc.com xxxxxx拆分成xxxhttp://abc.comxxxxxx3个字符串存放在数组中<wbr></wbr>

<wbr><span style="word-wrap:normal; word-break:normal; font-family:'Heiti sC Light'">参数:</span>source<span style="word-wrap:normal; word-break:normal; font-family:'Heiti sC Light'">,源文本</span></wbr>

<wbr></wbr>返回:拆分的子字符串

<wbr></wbr>

- (NSMutableArray*)splitStringByUrl:(NSString*)source;



<wbr></wbr>作用:根据一个给定的宽度拆分字符串,如果字符串的宽度小于等于给定的宽度,返回数组只有一个source字符串元素,否则返回拆分的两个子字符串

<wbr></wbr>


- (NSMutableArray*)splitStringBylimitWidth:(CGFloat)width source:(NSString*)source;



<wbr></wbr>作用:找到一个字符串在label中的最后一个行显示的部分的子字符串在原字符串中的起点位置。比如一个字符串为“123456789abcdefghijk”,

<wbr></wbr>经过换行后在一个label中换成两行,最后一行显示“hijk”,则这个函数返回h在字符串中的index,值为16,根据这个index可以取出字符串“hijk”

- (NSInteger)findStartIndexOfLastLine<wbr>Text:(<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(115,64,163)">NSString</span>*)source;</wbr>



<wbr></wbr>返回字符串的size

- (CGSize)sizeForString:(NSString*)string;



<wbr></wbr>返回给定字体的字符所站的高度

- (CGFloat)getHeightWithFontSize:(CGFloat)fontSize;



<wbr></wbr>根据拆分出的字符串布局URLView视图

- (void)layoutURLViewWithElement<wbr>s:(<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(115,64,163)">NSMutableArray</span>*)elements;</wbr>



<wbr><span style="word-wrap:normal; word-break:normal; font-family:'Heiti sC Light'">设置</span>URLView<span style="word-wrap:normal; word-break:normal; font-family:'Heiti sC Light'">视图的</span>Frame</wbr>

- (void)setFrame;



<wbr><span style="word-wrap:normal; word-break:normal; font-family:'Heiti sC Light'">根据</span>url<span style="word-wrap:normal; word-break:normal; font-family:'Heiti sC Light'">找出</span>url<span style="word-wrap:normal; word-break:normal; font-family:'Heiti sC Light'">相同的</span>URLLabel<span style="word-wrap:normal; word-break:normal; font-family:'Heiti sC Light'">,并设置颜色</span></wbr>


- (void)setUrlLabelTextColorWith<wbr>UrlString:(<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(115,64,163)">NSString</span>*)url color:(<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(115,64,163)">UIColor</span>*)color;</wbr>

-------------------**********-------------


- (NSMutableArray*)splitStringByUrl:(NSString*)source {

[sourceretain];

<wbr></wbr>

NSMutableArray*elementsArray = [[[NSMutableArrayalloc]init]autorelease];

NSIntegerindex =0;

while(index < source.length) {

NSRangesearchRange =NSMakeRange(index, source.length- index);

NSRangestartRange = [sourcerangeOfString:@"http://"options:NSCaseInsensitiveSearch range:searchRange];

if(startRange.location==NSNotFound) {

DSStyleString*currentElement = [[DSStyleStringalloc]init];

currentElement.isUrl=NO;

currentElement.string= [sourcesubstringWithRange:searchRange];

[elementsArrayaddObject:currentElement];

break;

}else{

NSRangebeforeRange =NSMakeRange(searchRange.location, startRange.location- searchRange.location);

if(beforeRange.length) {

DSStyleString*beforeElement = [[DSStyleStringalloc]init];

beforeElement.isUrl=NO;

beforeElement.string= [sourcesubstringWithRange:beforeRange];

[elementsArrayaddObject:beforeElement];

}

<wbr></wbr>

NSRangesearchRange =NSMakeRange(startRange.location, source.length- startRange.location);

NSRangeendRange = [sourcerangeOfString:@" "options:NSCaseInsensitiveSearch range:searchRange];

if(endRange.location==NSNotFound) {

DSStyleString*urlElement = [[DSStyleStringalloc]init];

urlElement.isUrl=YES;

urlElement.string= [sourcesubstringWithRange:searchRange];

[elementsArrayaddObject:urlElement];

break;

}else{

NSRangeurlRange =NSMakeRange(startRange.location, endRange.location- startRange.location);

DSStyleString*urlElement = [[DSStyleStringalloc]init];

urlElement.isUrl=YES;

urlElement.string= [sourcesubstringWithRange:urlRange];

[elementsArrayaddObject:urlElement];

index = endRange.location;

}

}

}

<wbr></wbr>

[sourcerelease];

returnelementsArray;

}


- (void)layoutURLViewWithElement<wbr>s:(<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(115,64,163)">NSMutableArray</span>*)elements {</wbr>

[elementsretain];

NSIntegercount = [elementscount];

if(count ==0)return;

BOOLhaveHttp =NO;

for(DSStyleString*styleStringinelements) {

if(styleString.isUrl==YES) {

haveHttp =YES;

break;

}

}

<wbr></wbr>

if(haveHttp ==YES) {

for(inti =0; i < count; i ++) {

DSStyleString*styleString = (DSStyleString*)[elementsobjectAtIndex:i];

NSArray*existSubViews = [selfsubviews];

if([existSubViewscount] >0) {

UIView*lastSubView = [existSubViewslastObject];

NSString*forwardSourceString =nil;

NSString*lastLineStringOfForwardS<wbr>ourceString =<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(191,46,157)">nil</span>;</wbr>

NSIntegerlastLineStringWidthOfFor<wbr>wardSourceString =<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(40,50,207)">0</span>;</wbr>

NSIntegerleaveWidth =0;

CGFloatoriginX =0;

CGFloatoriginY =0;

CGFloatwidth =0;

CGFloatheight =0;

CGFloatcharacterHeight = [selfgetHeightWithFontSize:FontSize];

<wbr></wbr>

if([[lastSubViewclass]isSubclassOfClass:[UILabelclass]]) {

forwardSourceString = [(UILabel*)lastSubViewtext];

}elseif([[lastSubViewclass]isSubclassOfClass:[DSURLLabelclass]]) {

forwardSourceString = [[(DSURLLabel*)lastSubViewurlLabel]text];

}

<wbr></wbr>

CGSizeforwardSourceStringSize = [selfsizeForString:forwardSourceString];

lastLineStringOfForwardS<wbr>ourceString = [forwardSourceString<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(63,33,124)">substringFromIndex</span>:[<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(191,46,157)">self</span><span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(46,89,93)">findStartIndexOfLastLine<wbr>Text</wbr></span>:forwardSourceString]];</wbr>

CGSizelastLineStringOfForwardS<wbr>ourceStringSize = [<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(191,46,157)">self</span><span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(46,89,93)">sizeForString</span>:lastLineStringOfForwardS<wbr>ourceString];</wbr></wbr>

lastLineStringWidthOfFor<wbr>wardSourceString = lastLineStringOfForwardS<wbr>ourceStringSize.<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(115,64,163)">width</span>;</wbr></wbr>

if(forwardSourceStringSize.height> characterHeight) {

leaveWidth =_frameWidth- lastLineStringOfForwardS<wbr>ourceStringSize.<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(115,64,163)">width</span>;</wbr>

}else{

leaveWidth =_frameWidth- lastSubView.frame.origin.x- lastSubView.frame.size.width;

}

<wbr></wbr>

NSMutableArray*splitedSubStringByLimitW<wbr>idthArray = [[<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(115,64,163)">NSMutableArray</span><span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(63,33,124)">alloc</span>]<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(63,33,124)">init</span>];</wbr>

[splitedSubStringByLimitW<wbr>idthArray<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(63,33,124)">addObjectsFromArray</span>:[<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(191,46,157)">self</span><span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(46,89,93)">splitStringBylimitWidth</span>:leaveWidth<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(46,89,93)">source</span>:styleString.<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(75,129,134)">string</span>]];</wbr>

<wbr></wbr>

if([splitedSubStringByLimitW<wbr>idthArray<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(63,33,124)">count</span>] ==<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(40,50,207)">1</span>) {</wbr>

if(_needNewLine) {

originX =0;

originY = lastSubView.frame.origin.y+ lastSubView.frame.size.height;

}else{

if(forwardSourceStringSize.height> characterHeight) {

originX = lastLineStringOfForwardS<wbr>ourceStringSize.<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(115,64,163)">width</span>;</wbr>

}else{

originX = lastSubView.frame.origin.x+ lastSubView.frame.size.width;

}

originY = lastSubView.frame.origin.y+ lastSubView.frame.size.height- characterHeight;

}

<wbr></wbr>

CGSizenewLabelSize = [selfsizeForString:styleString.string];

width = newLabelSize.width;

height = newLabelSize.height;

<wbr></wbr>

if(styleString.isUrl==YES) {

DSURLLabel*urlLabel = [[DSURLLabelalloc]initWithFrame:CGRectMake(originX, originY, width, height)];

urlLabel.backgroundColor= [UIColorclearColor];

urlLabel.urlString= styleString.string;

urlLabel.urlLabel.text= styleString.string;

urlLabel.urlLabel.numberOfLines=0;

urlLabel.urlLabel.lineBreakMode=LineBreakMode;

urlLabel.urlLabel.font= [UIFontsystemFontOfSize:FontSize];

urlLabel.delegate=self;

[selfaddSubview:urlLabel];

[urlLabelrelease];

}else{

UILabel*textLabel = [[UILabelalloc]initWithFrame:CGRectMake(originX, originY, width, height)];

textLabel.numberOfLines=0;

textLabel.lineBreakMode=LineBreakMode;

textLabel.font= [UIFontsystemFontOfSize:FontSize];

textLabel.backgroundColor= [UIColorclearColor];

textLabel.text= styleString.string;

[selfaddSubview:textLabel];

[textLabelrelease];

}

<wbr></wbr>

}elseif([splitedSubStringByLimitW<wbr>idthArray<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(63,33,124)">count</span>] ==<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(40,50,207)">2</span>) {</wbr>

for(inti =0; i <2; i++) {

NSString*currentSubString = [splitedSubStringByLimitW<wbr>idthArray<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(63,33,124)">objectAtIndex</span>:i];</wbr>

CGSizenewLabelSize = [selfsizeForString:currentSubString];

if(i ==0) {

if(forwardSourceStringSize.height> characterHeight) {

originX = lastLineStringOfForwardS<wbr>ourceStringSize.<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(115,64,163)">width</span>;</wbr>

}else{

originX = lastSubView.frame.origin.x+ lastSubView.frame.size.width;

}

originY = lastSubView.frame.origin.y+ lastSubView.frame.size.height- characterHeight;

width =_frameWidth- originX;

}elseif(i ==1) {

originX =0;

originY = lastSubView.frame.origin.y+ lastSubView.frame.size.height;

width = newLabelSize.width;

<wbr></wbr>

}

height = newLabelSize.height;

<wbr></wbr>

<wbr></wbr>

if(styleString.isUrl==YES) {

DSURLLabel*urlLabel = [[DSURLLabelalloc]initWithFrame:CGRectMake(originX, originY, width, height)];

urlLabel.backgroundColor= [UIColorclearColor];

urlLabel.urlString= styleString.string;

urlLabel.urlLabel.text= currentSubString;

urlLabel.urlLabel.font= [UIFontsystemFontOfSize:FontSize];

urlLabel.urlLabel.lineBreakMode=LineBreakMode;

if(i ==0) {

urlLabel.urlLabel.numberOfLines=1;

}elseif(i ==1) {

urlLabel.urlLabel.numberOfLines=0;

}

urlLabel.delegate=self;

[selfaddSubview:urlLabel];

[urlLabelrelease];

}else{

UILabel*textLabel = [[UILabelalloc]initWithFrame:CGRectMake(originX, originY, width, height)];

textLabel.backgroundColor= [UIColorclearColor];

textLabel.font= [UIFontsystemFontOfSize:FontSize];

textLabel.lineBreakMode=LineBreakMode;

if(i ==0) {

textLabel.numberOfLines=1;

}elseif(i ==1) {

textLabel.numberOfLines=0;

}

textLabel.text= currentSubString;

[selfaddSubview:textLabel];

[textLabelrelease];

}

}

}

<wbr></wbr>

[splitedSubStringByLimitW<wbr>idthArray<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(63,33,124)">release</span>];</wbr>

<wbr></wbr>

}else{

CGSizenewLabelSize = [selfsizeForString:styleString.string];

if(styleString.isUrl==YES) {

DSURLLabel*urlLabel = [[DSURLLabelalloc]initWithFrame:CGRectMake(0,0, newLabelSize.width, newLabelSize.height)];

urlLabel.backgroundColor= [UIColorclearColor];

urlLabel.urlLabel.font= [UIFontsystemFontOfSize:FontSize];

urlLabel.urlLabel.numberOfLines=0;

urlLabel.urlLabel.lineBreakMode=LineBreakMode;

urlLabel.urlString= styleString.string;

urlLabel.urlLabel.text= styleString.string;

urlLabel.delegate=self;

[selfaddSubview:urlLabel];

[urlLabelrelease];

}else{

UILabel*label = [[UILabelalloc]initWithFrame:CGRectMake(0,0, newLabelSize.width, newLabelSize.height)];

label.backgroundColor= [UIColorclearColor];

label.text= styleString.string;

label.font= [UIFontsystemFontOfSize:FontSize];

label.numberOfLines=0;

label.lineBreakMode=LineBreakMode;

[selfaddSubview:label];

[labelrelease];

}

}

}

}else{

DSStyleString*styleString = [elementsobjectAtIndex:0];

CGSizetextSize = [selfsizeForString:styleString.string];

UILabel*textLabel = [[UILabelalloc]initWithFrame:CGRectMake(0,0, textSize.width, textSize.height)];

textLabel.numberOfLines=0;

textLabel.backgroundColor= [UIColorclearColor];

textLabel.lineBreakMode=LineBreakMode;

textLabel.text=_sourceText;

textLabel.font= [UIFontsystemFontOfSize:FontSize];

[selfaddSubview:textLabel];

[textLabelrelease];

}

[selfsetFrame];

[elementsrelease];

}


- (void)setFrame {

UIView*lastSubView = (UIView*)[[selfsubviews]lastObject];

CGFloatheigh = lastSubView.frame.origin.y+ lastSubView.frame.size.height;

self.frame=CGRectMake(_frameOriginX,_frameOriginY,_frameWidth, heigh);

}


- (NSInteger)findStartIndexOfLastLine<wbr>Text:(<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(115,64,163)">NSString</span>*)source {</wbr>

[sourceretain];

CGSizesourceTextSize = [selfsizeForString:source];

NSIntegerlines = sourceTextSize.height/ [selfgetHeightWithFontSize:FontSize];

NSIntegerstartIndex =0;

if(lines >1) {

NSIntegerlength = [sourcelength];

for(inti = length; i >0; i --) {

CGSizetextSize = [selfsizeForString:[sourcesubstringToIndex:i]];

if(textSize.height< sourceTextSize.height) {

startIndex = i;

break;

}

}

}

[sourcerelease];

returnstartIndex;

}


- (NSMutableArray*)splitStringBylimitWidth:(CGFloat)width source:(NSString*)source {

[sourceretain];

NSMutableArray*subStrings = [[[NSMutableArrayalloc]init]autorelease];

NSIntegerlength = [sourcelength];

for(inti = length; i >0; i--) {

CGSizetextSize = [selfsizeForString:[sourcesubstringToIndex:i]];

if(textSize.width<= width && i == length) {

[subStringsaddObject:source];

_needNewLine=NO;

break;

}

if((textSize.width< width) && (textSize.height== [selfgetHeightWithFontSize:FontSize])) {

[subStringsaddObject:[sourcesubstringToIndex:i]];

[subStringsaddObject:[sourcesubstringFromIndex:i]];

break;

}

if(i ==1) {

[subStringsaddObject:source];

_needNewLine=YES;

break;

}

}

[sourcerelease];

returnsubStrings;

}


- (CGSize)sizeForString:(NSString*)string {

CGSizetextSize = [stringsizeWithFont:[UIFontsystemFontOfSize:FontSize]constrainedToSize:CGSizeMake(_frameWidth,10000.0f)lineBreakMode:LineBreakMode];

returntextSize;

}


- (CGFloat)getHeightWithFontSize:(CGFloat)fontSize {

NSString*character =@" ";

CGSizecharacterSize = [selfsizeForString:character];

returncharacterSize.height;

}


- (void)setUrlLabelTextColorWith<wbr>UrlString:(<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(115,64,163)">NSString</span>*)url color:(<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(115,64,163)">UIColor</span>*)color {</wbr>

NSArray*subViews = [selfsubviews];

for(UIView*subViewinsubViews) {

if([[subViewclass]isSubclassOfClass:[DSURLLabelclass]]) {

if([((DSURLLabel*)subView).urlStringisEqualToString:url]) {

((DSURLLabel*)subView).urlLabel.textColor= color;

}

}

}

}



#pragma mark -

#pragma mark DSURLLabelDelegate methods


- (void)urlTouchesBegan:(DSURLLabel*)urlLabel {

[selfsetUrlLabelTextColorWith<wbr>UrlString<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(0,0,0)">:urlLabel.</span><span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(75,129,134)">urlString</span>color<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(0,0,0)">:[</span><span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(115,64,163)">UIColor</span><span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(63,33,124)">redColor</span><span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(0,0,0)">]];</span></wbr>

}


- (void)urlTouchesEnd:(DSURLLabel*)urlLabel {

[selfsetUrlLabelTextColorWith<wbr>UrlString<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(0,0,0)">:urlLabel.</span><span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(75,129,134)">urlString</span>color<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(0,0,0)">:[</span><span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(115,64,163)">UIColor</span><span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(63,33,124)">blueColor</span><span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(0,0,0)">]];</span></wbr>

if(_delegate&& [(NSObject*)_delegaterespondsToSelector:@selector(urlWasClicked:urlString:)]) {

[_delegateurlWasClicked:selfurlString:urlLabel.urlString];

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值