iOS:用 iPhone 日历列表的背景颜色制作一个圆圈

Posted

技术标签:

【中文标题】iOS:用 iPhone 日历列表的背景颜色制作一个圆圈【英文标题】:iOS: Make a circle with background color like the iPhone calendar list 【发布时间】:2012-02-07 20:30:08 【问题描述】:

我正在尝试在表格单元格的左侧添加一个圆圈,但无法找到最佳方法。我尝试添加

CGContextRef context= UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetAlpha(context, 0.5);
CGContextFillEllipseInRect(context, CGRectMake(10.0, 10.0, 10.0, 10.0));

CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextStrokeEllipseInRect(context, CGRectMake(10.0, 10.0, 10.0, 10.0));

到我的 cellForRowAtIndexPath 但不断收到无效的上下文错误。这是我的 cellForRowAtIndexPath。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    static NSString *CellIdentifier = @"AppointmentCell";
    NSDictionary *appointment = [[self.appointments objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    int minutes = (int)[[NSString stringWithFormat:@"%@", [appointment objectForKey:@"start_time"]] integerValue];

    if (cell == nil) 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"h:mma"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
    NSDate *midnight = [NSDate dateWithTimeIntervalSince1970:0];
    NSDate *newDate = [midnight dateByAddingTimeInterval:minutes*60];

    NSString *newTime = [dateFormatter stringFromDate:newDate];
    dateFormatter = nil;

    cell.textLabel.text = newTime;
    cell.detailTextLabel.textAlignment = UITextAlignmentCenter;
    cell.detailTextLabel.text = [appointment objectForKey:@"reason"];

    return cell;

如何添加颜色与 iPhone 的日历列表视图一样的圆圈?

【问题讨论】:

这不能回答您的问题,但您绝对应该将该 dateFormatter 作为该类的属性。每次显示单元格时,您都会创建一个新的 NSDateFormatter。这没有必要,而且对性能非常不利。 【参考方案1】:

更新

您用于渲染圆圈的代码很好,您只需将其放在 UIView 子类中即可正常工作。

@interface CircleView : UIView

@implementation CircleView

- (void)drawRect:(CGRect)rect
    CGContextRef context= UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
    CGContextSetAlpha(context, 0.5);
    CGContextFillEllipseInRect(context, CGRectMake(0,0,self.frame.size.width,self.frame.size.height));

    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    CGContextStrokeEllipseInRect(context, CGRectMake(0,0,self.frame.size.width,self.frame.size.height));



然后您可以将圆形视图添加到表格单元格中,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    //...
    //Your existing code
    CGRect positionFrame = CGRectMake(10,10,10,10);
    CircleView * circleView = [[CircleView alloc] initWithFrame:positionFrame];
    [cell.contentView addSubview:circleView];
    [circleView release];

    return cell;

使用位置框架,直到它与您需要的匹配。

【讨论】:

尝试将圆形视图的背景颜色设置为清除并将draw rect中的self.frame替换为CGRectMake(0,0,self.frame.size.width,self.frame.size.height ) 两个改进使这个圆匹配 ios 日历圆: - 将填充和描边操作的大小减少 2 点(1 pt 半径)以防止边界剪切 - 之前将上下文 alpha 设置为 1.0画笔画 上面的代码产生了一个边是扁平的圆,但这修复了它(对我来说,无论如何):CGContextStrokeEllipseInRect(context, CGRectMake(1, 1, self.frame.size.width - 2, self. frame.size.height - 2)); 这很好用,但如果我可以借用这个问题问你如何清除圆圈或改变它的颜色,当我想要的时候?假设我有一个方法“changeColor”,如何实现它?我只是用另一种颜色画在那个圆圈上吗?【参考方案2】:

tableView:cellForRowAtIndexPath: 是创建单元格的地方,但这不是绘图发生的地方。如果您想在 tableViewCell 中进行自定义绘图,您需要继承 UITableViewCell,覆盖 drawRect:,并将绘图代码放入其中。

或者,您可以将 tableViewCell 的 imageView 的 UIImage 设置为圆形图片。

【讨论】:

以上是关于iOS:用 iPhone 日历列表的背景颜色制作一个圆圈的主要内容,如果未能解决你的问题,请参考以下文章

设置全日历单元格背景颜色

xcode 4.2 和 ios 5 中的标签栏背景颜色

iPhone 中常用好用的提醒类软件(闹钟提醒事项日历滴答清单)

闪亮的 Toastui 日历:更改特定日期的日历背景颜色

用Word制作表格,如何更改表格背景色

iphone日历不显示农历日期怎么办