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

效果图:



思路很简单就是截取我们获得的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.这样,我们在设置数据的时候就可以不用考虑字符串的截取了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值