新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

轻松使用富文本-创新互联

创新互联服务项目包括定南网站建设、定南网站制作、定南网页制作以及定南网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,定南网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到定南省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

最后发现需要6.0以后,因为nsfontattributename之类的是6.0以后的api

  长久以来,我以为富文本是一种在ios中使用特别麻烦的事情,但是不经意的研究发现,其实并没有那么难!

  以下的代码实现了uilabel中放置富文本。


NSMutableAttributedString *richTask = [[NSMutableAttributedString alloc] initWithString:task];
    NSDictionary *urgentAttributes = @{NSFontAttributeName : [UIFont fontWithName:@"Courier" size:24],
                                       NSStrokeWidthAttributeName : @3.0,NSStrokeColorAttributeName:[UIColor greenColor]};
    [richTask setAttributes:urgentAttributes range:urgentRange];


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
                                                                   
    NSString *identifier = nil;
    NSString *task = [self.tasks objectAtIndex:indexPath.row];
    NSRange urgentRange = [task rangeOfString:@"URGENT"];
    if (urgentRange.location == NSNotFound) {
        identifier = @"plainCell";
    } else {
        identifier = @"attentionCell";
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
                                                                   
    // Configure the cell...
                                                                   
    UILabel *cellLabel = (UILabel *)[cell viewWithTag:1];
    NSMutableAttributedString *richTask = [[NSMutableAttributedString alloc] initWithString:task];
    NSDictionary *urgentAttributes = @{NSFontAttributeName : [UIFont fontWithName:@"Courier" size:24],
                                       NSStrokeWidthAttributeName : @3.0,NSStrokeColorAttributeName:[UIColor greenColor]};
    [richTask setAttributes:urgentAttributes range:urgentRange];
    if (urgentRange.location != NSNotFound) {
        NSRange otherPartRange = NSMakeRange(urgentRange.length+urgentRange.location, task.length-urgentRange.length);
        NSDictionary* otherPartAttributes = @{NSFontAttributeName : [UIFont fontWithName:@"Courier" size:12],
                                              NSStrokeWidthAttributeName : @0,NSStrokeColorAttributeName:[UIColor redColor]};
        [richTask setAttributes:otherPartAttributes range:otherPartRange];
    }
                                                                   
    cellLabel.attributedText = richTask;
                                                                   
    return cell;
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页标题:轻松使用富文本-创新互联
当前URL:http://www.wjwzjz.com/article/dceisj.html
在线咨询
服务热线
服务热线:028-86922220
TOP