RTLabel下载地址:点击打开链接
1、效果如下:
2.源码解读
先说说框架。一个tableView显示了多个RTLabel。分别显示了不同的功能。
关于布局和设计。
就对于DemoTableViewCell 来说,我们的设计和布局通过两部来完成。
1)cell的布局和设计。
i:首先 使用 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 初始化了我们的各个控件,但是并没有指定控件的位置。
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code.
self.rtLabel = [DemoTableViewCell textLabel];
[self.contentView addSubview:self.rtLabel];
[self.rtLabel release];
[self.rtLabel setBackgroundColor:[UIColor clearColor]];
[self setSelectionStyle:UITableViewCellEditingStyleNone];
}
return self;
}让我们看看其中的初始化,我们只是穿件了内存,并添加了控件所属父类和颜色等属性,但是并没有设置 我们的rtlabel的具体位置。在类方法中我们也只是设置了RTLAbel的大小。
ii:其中还使用勒Cell的一个类方法 textLabel。
+ (RTLabel*)textLabel
{
RTLabel *label = [[RTLabel alloc] initWithFrame:CGRectMake(10,10,300,100)];
//[label setFont:[UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:20]];
[label setParagraphReplacement:@""];
return [label autorelease];
}
iii.然后 使用布局设计方法来定位控件的位置。
- (void)layoutSubviews
{
[super layoutSubviews];
CGSize optimumSize = [self.rtLabel optimumSize];
CGRect frame = [self.rtLabel frame];
frame.size.height = (int)optimumSize.height+5; // +5 to fix height issue, this should be automatically fixed in iOS5
[self.rtLabel setFrame:frame];
}只有在 布局设计器中我们才设置了RTlable在cell中的位置。
4.tableView的init方法--初始化cell数据
row1:
- (id)initWithStyle:(UITableViewStyle)style
{
// Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
self = [super initWithStyle:style];
if (self) {
//1设置 topbar 的item:NavigationBar
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,150,30)];
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:20]];
[titleLabel setText:@"RTLabel"];
[self.navigationItem setTitleView:titleLabel];
[titleLabel release];
//[titleLabel setTextAlignment:UITextAlignmentCenter];
self.dataArray = [NSMutableArray array];
//2 ROW1 设置的是一个不一样的字体。使用html格式设置。
NSMutableDictionary *row1 = [NSMutableDictionary dictionary];
[row1 setObject:@"<b>bold</b> and <i>italic</i> style" forKey:@"text"];
[self.dataArray addObject:row1];
//3。设置带下划线的字体,依据html格式
NSMutableDictionary *row2 = [NSMutableDictionary dictionary];
[row2 setObject:@"<font face='HelveticaNeue-CondensedBold' size=20><u color=blue>underlined</u> <uu color=red>text</uu></font>" forKey:@"text"];
[self.dataArray addObject:row2];
//4。设置超链接。
NSMutableDictionary *row3 = [NSMutableDictionary dictionary];
[row3 setObject:@"clickable link - <a href='http://store.apple.com'>link to apple store</a> <a href='http://www.google.com'>link to google</a> <a href='http://www.yahoo.com'>link to yahoo</a> <a href='https://github.com/honcheng/RTLabel'>link to RTLabel in GitHub</a> <a href='http://www.wiki.com'>link to wiki.com website</a>" forKey:@"text"];
[self.dataArray addObject:row3];
//5.设置字体的颜色
NSMutableDictionary *row4 = [NSMutableDictionary dictionary];
[row4 setObject:@"<font face='HelveticaNeue-CondensedBold' size=20 color='#CCFF00'>Text with</font> <font face=AmericanTypewriter size=16 color=purple>different colours</font> <font face=Futura size=32 color='#dd1100'>and sizes</font>" forKey:@"text"];
[self.dataArray addObject:row4];
NSMutableDictionary *row5 = [NSMutableDictionary dictionary];
[row5 setObject:@"<font face='HelveticaNeue-CondensedBold' size=20 stroke=1>Text with strokes</font> " forKey:@"text"];
[self.dataArray addObject:row5];
NSMutableDictionary *row6 = [NSMutableDictionary dictionary];
[row6 setObject:@"<font face='HelveticaNeue-CondensedBold' size=20 kern=35>KERN</font> " forKey:@"text"];
[self.dataArray addObject:row6];
NSMutableDictionary *row7 = [NSMutableDictionary dictionary];
[row7 setObject:@"<font face='HelveticaNeue-CondensedBold' size=14><p align=justify><font color=red>JUSTIFY</font> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.Ut enim ad minim </p> <p align=left><font color=red>LEFT ALIGNED</font> veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p><br><p align=right><font color=red>RIGHT ALIGNED</font> Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p><br><p align=center><font color=red>CENTER ALIGNED</font> Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p></font> " forKey:@"text"];
[self.dataArray addObject:row7];
NSMutableDictionary *row20 = [NSMutableDictionary dictionary];
[row20 setObject:@"<p indent=20>Indented bla bla bla bla bla bla bla bla bla bla bla bla bla</p>" forKey:@"text"];
[self.dataArray addObject:row20];
}
return self;
}
本文详细解析了RTLabel组件的布局设计和数据初始化过程,包括cell的初始化、布局方法、以及如何在tableView中展示不同功能的RTLabel。通过具体的代码解读,展示了如何设置字体样式、颜色、文本格式化、超链接、下划线等功能。

被折叠的 条评论
为什么被折叠?



