轻松使用富文本
2013-07-11 11:47
99 查看
最后发现需要6.0以后,因为nsfontattributename之类的是6.0以后的api
长久以来,我以为富文本是一种在ios中使用特别麻烦的事情,但是不经意的研究发现,其实并没有那么难!
以下的代码实现了uilabel中放置富文本。
长久以来,我以为富文本是一种在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本文出自 “用心生活” 博客,请务必保留此出处http://derkin2jessie.blog.51cto.com/1009410/1246321
{
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;
}
相关文章推荐
- 轻松python文本专题-判断对象里面是否是类字符串(推荐使用isinstance(obj,str))
- 使用ClickableSpan轻松实现一段文本中的多个点击事件
- 使用 Apache Lucene 搜索文本——轻松为应用程序构建搜索和索引功能
- 轻松python文本专题-判断对象里面是否是类字符串(推荐使用isinstance(obj,str))
- 轻松python文本专题-判断对象里面是否是类字符串(推荐使用isinstance(obj,str))
- 使用iconv命令轻松实现linux下字符集编码的转换
- 文本消息中使用网页超链接
- 使用ITextSharp读取pdf的文本内容
- 「打」strike() //使用删除线来显示字符 20140823 ①文本处理
- 轻松掌握Ajax.net系列教程十二:使用TabContainer&TabPanel
- linux shell脚本如何将文本内容按空格分行、如何使用timit原始corpus制作wav的scp文件
- 使用 Linux/Unix 进行文本处理
- 轻松使用jquery解析XML
- 安卓文本样式-Spannable的使用
- 使用awk进行高级文本处理
- 使用AVSpeechSynthesizer添加"文本转语音"的功能
- 使用Nginx轻松实现开源负载均衡
- FCKeditor 一个开源的web页面文本编辑工具,在vs 2005中的使用方法
- Alamofire网络库基础教程:使用 Alamofire 轻松实现 Swift 网络请求
- 使用.ashx文件处理IHttpHandler实现发送文本及二进制数据的方法