ios - UITableViewCell 圆圈项目符号点

标签 ios objective-c uitableview geometry cgrect

我一直在尝试像在 iOS 日历应用程序中那样实现彩色圆圈项目符号点。

selection view

我想要像上图一样在 textLabel 前面有带圆圈的单元格。

selected view

而且我还想像上图一样在 detailTextLabel 前面有相应的圆圈。

我尝试用框架内的图像来执行此操作,但它总是太大。另外,日历应用程序中的圆圈似乎不在单元格的 imageView 中。

我还尝试用从另一个问题中找到的一些代码画一个圆圈。虽然这段代码对我不起作用。这是我的 XYZCircleView.m 文件中的代码:

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    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));
    CGContextStrokeEllipseInRect(context, CGRectMake(1, 1, self.frame.size.width - 2,    self.frame.size.height - 2));
}

在我的 cellForRow... 中:

CGRect positionFrame = CGRectMake(10,10,10,10);
XYZCircleView *circleView = [[XYZCircleView alloc] initWithFrame:positionFrame];
[cell.contentView addSubview:circleView];

如何做到这一点?

谢谢

最佳答案

使用项目符号字符并使用属性字符串,以便您可以设置项目符号的颜色。

像这样:

NSString *text = @"• Calendar";
NSDictionary *attributes = @{ NSFontAttributeName : [UIFont systemFontOfSize:15] };
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
[attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 1)]; // color the bullet
[attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(1, attrStr.length - 1)]; // color the rest

cell.text.attributedText = attrStr;

您可能需要根据需要使用不同的字体或颜色。

关于ios - UITableViewCell 圆圈项目符号点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25004897/

相关文章:

ios - 如何在 NSMutableArray 的 TableView 中添加搜索

ios - 加载股票 tableView 时出现 fatal error (发现为零)

android - Delphi Firemonkey iOS后台处理

iphone - 如何在 XCode/iOS 中包含 HTML 文件?

iOS:如何在按下按钮时以编程方式在导航栏上添加搜索栏?

ios - 如何将手势添加到 map 叠加层

ios - 为什么代码没有被执行?

objective-c - 如何在 OS X 中以编程方式创建多输出设备?

objective-c - 你什么时候想在辅助线程上运行 NSURLConnection ?

ios - 旋转时自动调整 UITableView 标题的大小(iPad 上的 MoSTLy)