Adrime Adrime - 1 year ago 102
iOS Question

Wrap a label with another view IOS

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?

Answer Source

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


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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download