How to display an array of UILabels?

I have a list of items that I would like to show in a UITableViewCell. Right now, I'm just using cell.textLabel with commas separating each value, but I would like to do something more dynamic.

How could I achieve something like this? alt text

Will it be an array of UILabels with borders and a radius at these borders?

Thanks for your ideas.

+3
source share
1 answer

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; // or whatever works for you
 layer.borderWidth = 1.0;
 layer.borderColor = [[UIColor redColor].CGColor;
 layer.backgroundColor = [UIColor blueColor].CGColor;

 // Add the layer into its superview
 [yourSuperview addSubview:label];

, .

+1

Source: https://habr.com/ru/post/1766876/


All Articles