Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a circle in UILabel? [closed]

I want to do some thing like this, option image

How to draw a circle in UILabel which looks similar to this image, i just want to avoid using an image instead.

like image 255
Smith Avatar asked Oct 18 '13 06:10

Smith


2 Answers

I'd suggest adding a shape layer to the UILabel. Something like this:

// Create the shape layer

CAShapeLayer *circleLayer = [CAShapeLayer layer];
circleLayer.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 50, 50)].CGPath;
circleLayer.fillColor = [UIColor clearColor].CGColor;
circleLayer.strokeColor = [UIColor whiteColor].CGColor;
circleLayer.lineWidth = 1;

// Add it do your label's layer hierarchy

[label.layer addSublayer:circleLayer];

That'll give you the greatest control over its appearance and that of the font for the A.

UPDATE:

It's just occurred to me that you'll struggle to get the "A" vertically offset in order to put a circle around it. There are hacks to get vertical centering in a UILabel, but you'd be much better off using a UITextField instead, since it supports the contentVerticalAlignment property. You just need to set its userInteractionEnabled to NO to prevent user from typing text on it!

Other than that, the principle of adding the CAShapeLayer is the same.

like image 138
tarmes Avatar answered Nov 17 '22 22:11

tarmes


You can achieve this by the way @maddy suggested or, create UILabel with square rect, put text alignment to the middle and use label's layer's cornerRadius with borderWidth. Just set cornerRadiusto the half width (or height) of the label. Good Luck!

like image 38
Fahri Azimov Avatar answered Nov 17 '22 22:11

Fahri Azimov