Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell Circle Bullet Point

I've been trying to implement colorful circle bullet points like in the iOS Calendar app.

selection view

I'd like to have cells with the circles in front of the textLabel like the image above.

selected view

And I'd also like to have the corresponding circle in front of the detailTextLabel like the image above.

I've tried to do this with an image inside a frame but it is always too big. Plus, the circles in the Calendar app don't appear to be in the imageView of the cell.

I've also tried drawing a circle with some code I found from another question. This code isn't working for me though. Here's code from my XYZCircleView.m file:

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

And inside my cellForRow...:

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

How can this be done?

Thanks

like image 245
Benck Avatar asked Aug 09 '26 01:08

Benck


1 Answers

Use a bullet character and use an attributed string so you can set the color of the bullet.

Something like this:

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;

You may need to use a different font or colors as needed.

like image 66
rmaddy Avatar answered Aug 10 '26 15:08

rmaddy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!