简体   繁体   English

如何制作这类徽章?

[英]How to create these kind of badges?

I sketched this prototype: http://dl.dropbox.com/u/3630641/3-%20Todolist%20Main.jpg 我绘制了这个原型的草图: http : //dl.dropbox.com/u/3630641/3-%20Todolist%20Main.jpg

The number at the left is the item priority and list is re-orderable so the values will change. 左边的数字是项目优先级,列表是可重新排序的,因此值将更改。 How to create these left-located badges? 如何创建这些左侧的徽章? I prefer a code approach if possible, not PNG images. 如果可能的话,我更喜欢使用代码方法,而不是PNG图片。

Update: 更新:

Would you please have a look: 请您看看:

- (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;
}

But No grey area is shown around the priority. 但是优先级周围没有显示灰色区域。
If I add a new one, a grey area appears for less than a second around the priority but it disappears immediately. 如果我添加一个新的,优先级周围的灰色区域会出现少于一秒钟的时间,但是会立即消失。 Not to mention that the priority value isn't located at the center for the grey area. 更不用说优先级值不在灰色区域的中心。

Any idea what I'm doing wrong? 知道我在做什么错吗?

Add a UILabel to the cell and format it to your liking. UILabel添加到单元格, UILabel将其格式化为自己喜欢的格式。 For the rounded corners, set label.layer.cornerRadius . 对于圆角,设置label.layer.cornerRadius

  1. Create a UIView for the badge. 为徽章创建一个UIView。
  2. look here it will explaine how to draw what ever you want the badge to look like. 看这里,它将说明如何绘制您想要的徽章外观。 and draw the number : http://www.techotopia.com/index.php/An_iPhone_Graphics_Drawing_Tutorial_using_Quartz_2D 并绘制数字: http : //www.techotopia.com/index.php/An_iPhone_Graphics_Drawing_Tutorial_using_Quartz_2D

  3. create a variable that will receive the cell number and draw it in the cell. 创建一个将接收单元格编号并将其绘制在单元格中的变量。

It should look something like that - 它应该看起来像这样-

- (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 add the BadgeView to your cell and in the CellForRawAtIndexPath pass the number to the badge view. 4将BadgeView添加到您的单元格中,然后在CellForRawAtIndexPath中将数字传递到徽章视图。

good luck 祝好运

You could make a combination control. 您可以进行组合控制。 It's a UIImageView with a UIImage and a UILabel as subviews. 这是一个UIImageView,具有一个UIImage和一个UILabel作为子视图。

A regular object that has a UIImageView.image with the gray rectangle and then a UILabel for the white number. 一个常规对象,它的UIImageView.image带有灰色矩形,然后是带有白色数字的UILabel。 Then just change the value of the label's text. 然后只需更改标签文本的值即可。

If you start with a UIImageView and subclass that, you will be able to just stick it directly into the regular UITableViewCells since they already have a space for a UIImageView. 如果从UIImageView开始并为其子类化,则可以将其直接粘贴到常规UITableViewCells中,因为它们已经具有用于UIImageView的空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM