There is a quick and easy way to do this. It is based on the code you can get here .
Please note that you need to add the QuartzCore framework to your project and include it in the file where you write this code!
UIView CALayer. UIView CALayer .layer. UILabel UIView, . , backgroundColor, cornerRadius, borderColor borderWidth. .
, UILabel textAlignment UITextAlignmentCenter. UILabel sizeThatFits , , sizeWithFont , .
, , .
, - UIFont ( , ).
labelFont = [UIFont systemFontOfSize:14];
. , , "". X_PADDING Y_PADDING - . xLoc yLoc - , x y, . , xLoc textSize + X_PADDING + LABEL_SPACING - ( LABEL_SPACING):
CGSize textSize = [text sizeWithFont:labelFont];
CGRect frame = CGRectMake( xLoc, yLoc,
textSize.width + X_PADDING,
textSize.height + Y_PADDING);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.text = text;
label.textAlignment = UITextAlignmentCenter;
CALayer *layer = label.layer;
layer.masksToBounds = YES;
layer.cornerRadius = 7.0;
layer.borderWidth = 1.0;
layer.borderColor = [[UIColor redColor].CGColor;
layer.backgroundColor = [UIColor blueColor].CGColor;
[yourSuperview addSubview:label];
, .