-1

I want to create a label, over a black rectangle with rounded corners and some padding between the label's borders and the rectangle's borders. Its also necessary to wrap correctly the label if the text length gets changed in runtime.

The appearance should be something like this view:

enter image description here

But built in inside my view instead of floating.

I have no experience with the iOS interface builder, how can I achieve this?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Addev
  • 31,819
  • 51
  • 183
  • 302

3 Answers3

1

Easiest way would be

Creating a simple UILabel with black background color and set its corner radius.You can position this label wherever you want .

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
  • How do I handle the padding? – Addev Jun 08 '16 at 15:58
  • what padding ? As you are creating a label but not wrapping it in a view, you need not to worry about the padding. The label will be expaned as per its content and so does the black background. – Teja Nandamuri Jun 08 '16 at 15:59
  • When I create a label and change its background the borders are too close to the text – Addev Jun 08 '16 at 16:01
  • you can give padding to the label text.Those are called insets. You can give (10,10,10,10) so that you will have 10pts gap between text and border. – Teja Nandamuri Jun 08 '16 at 16:02
1

As far as I can understand you want to display a pop-up to show some text which will be dynamic and can be shown wherever you want just by passing the text.

What you need to do is create a custom view containing a label with top, bottom, leading and trailing constraints whose size would increase based on text. Create a class method with arguments descriptionText:NSString and onViewController:UIViewController . Inside it set the frame of your customView according to the text.

You can use the below code if you want to make size dynamic :-

-(CGSize)getLabelSizeFortext:(NSString *)text forWidth:(float)width WithFont:(UIFont *)font
{
    CGSize constraint = CGSizeMake(width, MAXFLOAT);
    CGRect titleRect = [text boundingRectWithSize:constraint options:(NSStringDrawingUsesLineFragmentOrigin| NSStringDrawingTruncatesLastVisibleLine) attributes:@{NSFontAttributeName:font} context:nil];
    return titleRect.size;
}

Inside your class method add your customView to your currentViewController as

onViewController.view.addSubView(self)

You can animate it if you want and let it disappear after few seconds so that the user is able to read the text. As far as appearance is concerned set backgroundColor to [UIColor blackColor] and alpha to 8.0.

0

You can probably get away with just one label.

If you don't assign a specific width to the label, it will resize itself based on the length of the text.

You can change the background color and opacity of the label in Interface Builder under the View portion of the properties tab.

The rounded corners will need to be done in code, by changing the corner radius of the labels CALayer (label.layer.cornerRadius). You may be able to change the corners by adding User Defined Runtime Attributes in Interface Builder, but I haven't tried that so I can't say for certain off the top of my head.

Wyatt
  • 309
  • 2
  • 11