[英]How to create these kind of badges?
我绘制了这个原型的草图: http : //dl.dropbox.com/u/3630641/3-%20Todolist%20Main.jpg
左边的数字是项目优先级,列表是可重新排序的,因此值将更改。 如何创建这些左侧的徽章? 如果可能的话,我更喜欢使用代码方法,而不是PNG图片。
请您看看:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
CGRect priorityLabelRect = CGRectMake(10, 5, 20, 30);
UILabel *priorityLabel = [[UILabel alloc] initWithFrame:priorityLabelRect];
priorityLabel.layer.cornerRadius = 5.0f;
priorityLabel.layer.backgroundColor = [[UIColor grayColor] CGColor];
priorityLabel.tag = kPriorityValueTag;
[cell.contentView addSubview:priorityLabel];
[priorityLabel release];
CGRect meatLabelRect = CGRectMake(80, 5, 300, 30);
UILabel *meatLabel = [[UILabel alloc] initWithFrame:meatLabelRect];
meatLabel.tag = kMeatValueTag;
[cell.contentView addSubview:meatLabel];
[meatLabel release];
cell.showsReorderControl = YES;
}
// Configure the cell.
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
但是优先级周围没有显示灰色区域。
如果我添加一个新的,优先级周围的灰色区域会出现少于一秒钟的时间,但是会立即消失。 更不用说优先级值不在灰色区域的中心。
知道我在做什么错吗?
将UILabel
添加到单元格, UILabel
将其格式化为自己喜欢的格式。 对于圆角,设置label.layer.cornerRadius
。
看这里,它将说明如何绘制您想要的徽章外观。 并绘制数字: http : //www.techotopia.com/index.php/An_iPhone_Graphics_Drawing_Tutorial_using_Quartz_2D
创建一个将接收单元格编号并将其绘制在单元格中的变量。
它应该看起来像这样-
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGRect rectangle = CGRectMake(10,10,40,40);
CGContextAddRect(context, rectangle);
CGContextStrokePath(context);
CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);
CGContextFillRect(context, rectangle);
CGPoint point =CGPointMake(20,20);
[self.cellNumber drawAtPoint:point withFont:font];
}
4将BadgeView添加到您的单元格中,然后在CellForRawAtIndexPath中将数字传递到徽章视图。
祝好运
您可以进行组合控制。 这是一个UIImageView,具有一个UIImage和一个UILabel作为子视图。
一个常规对象,它的UIImageView.image带有灰色矩形,然后是带有白色数字的UILabel。 然后只需更改标签文本的值即可。
如果从UIImageView开始并为其子类化,则可以将其直接粘贴到常规UITableViewCells中,因为它们已经具有用于UIImageView的空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.