您的位置:首页 > 移动开发 > IOS开发

类似新浪微博来源显示-来自ipone手机

2015-07-27 09:34 471 查看
效果图:



思路很简单就是截取我们获得的source字符串 

// <a href="http://weibo.com/" rel="nofollow">微博 weibo.com</a>
可以这样做:
1.我们在设置数据的时候是这样的

//    来源
self.source.text = sModel.source;
CGFloat sourceX = CGRectGetMaxX(fModel.created_atF) + Padding;
CGFloat sourceY = timeY;
CGSize sourceSize = [Tools sizeWithText:sModel.source font:SourceFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
self.source.frame = CGRectMake(sourceX, sourceY, sourceSize.width, sourceSize.height);

第一种方法:

// <a href="http://weibo.com/" rel="nofollow">微博 weibo.com</a>
- (void)setSource:(NSString *)source
{

NSInteger loc = [source rangeOfString:@">"].location + 1;
NSInteger length = [source rangeOfString:@"</"].location - loc;

if (length > 0) {
source = [source substringWithRange:NSMakeRange(loc, length)];

_source = [NSString stringWithFormat:@"来自%@", source];

}

}

第二种方法

//第二种写法:导入 #import "RegexKitLite.h" 框架
// <a href="http://weibo.com/" rel="nofollow">微博 weibo.com</a>
- (void)setSource:(NSString *)sources
{

//微博来源的处理
//source  <a href="http://weibo.com/" rel="nofollow">微博 weibo.com</a>
//检索:>.+<
NSString *sourceRegex = @">.+<";
//>微博 weibo.com<
sources = [[sources componentsMatchedByRegex:sourceRegex] lastObject];
//>微博 weibo.com<  ->微博 weibo.com
NSRange rg = NSMakeRange(1, sources.length-2);
_source = [sources substringWithRange:rg];

}


2.我们可以吧截取的操作封装到source的set方法里,因为set方法在拖动表视图的时候只会调用一次,get方法会调用多次,为了性能着想,就复写source的set方法

可以在数据模型里面这么做

// <a href="http://weibo.com/" rel="nofollow">微博 weibo.com</a>
- (void)setSource:(NSString *)source
{

NSInteger loc = [source rangeOfString:@">"].location + 1;
NSInteger length = [source rangeOfString:@"</"].location - loc;
if (length < 0) {
return;
}
source = [source substringWithRange:NSMakeRange(loc, length)];

_source = [NSString stringWithFormat:@"来自%@", source];

}


3.这样,我们在设置数据的时候就可以不用考虑字符串的截取了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  IOS开发