【问题标题】:UITableViewCell Circle Bullet PointUITableViewCell 圆形项目符号点
【发布时间】:2014-07-28 21:47:11
【问题描述】:

我一直在尝试像在 iOS 日历应用中那样实现彩色圆点。

我想要在 textLabel 前面有圆圈的单元格,如上图所示。

我还希望在 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];

如何做到这一点?

谢谢

【问题讨论】:

    标签: ios objective-c uitableview geometry cgrect


    【解决方案1】:

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

    类似这样的:

    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;
    

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

    【讨论】:

    • 尽管我在很多地方都使用了 rounak 的解决方案(带有一个看起来不可见的附加边框),但我更喜欢这个解决方案。在我看来,更优雅。和我最先想到的一模一样。
    • 谢谢!这是一个非常简单的解决方案。 NSFontAttributeName 部分是否决定了子弹的大小?或者我怎样才能让它更大?
    • 字体适用于整个字符串。尝试不同的项目符号字符,例如 ∙ 或 • 或 ・ 或 ● 或 ⬤ 或 ⚫︎。
    • 我想我应该声明您可以将两种不同的字体应用于字符串的不同部分,就像颜色一样。但是,如果适合您的需要,使用更大的子弹会更容易。
    【解决方案2】:

    只需尝试将方形视图作为子视图添加到单元格,并将其图层的cornerRadius 属性设置为圆形。

    【讨论】:

    • 第二个单元格的颜色和日历名称需要出现在单元格的detailTextLabel 中,这将如何工作?
    • 也许您需要一个表格视图单元子类,您可以在其中根据需要布置自己的视图
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-07
    相关资源
    最近更新 更多