简体   繁体   中英

How to insert UILabel below UIView subview in iOS

I need to create some UIView in circular shape and showing text below that. I have created circle successfully. But is there any way that I should add label below that circle. I need to display many circle with text. Below is my code of circle and attach is the screen shot which I need to create. 在此处输入图片说明

- (UIView*)createCircleViewWithRadius
{
    // circle view
    UIView *circle = [[UIView alloc] initWithFrame:CGRectZero];
    circle.translatesAutoresizingMaskIntoConstraints = NO;
    circle.layer.cornerRadius = 50;
    circle.layer.masksToBounds = YES;

    // border
    circle.layer.borderColor = [UIColor lightGrayColor].CGColor;
    circle.layer.borderWidth = 1;

    // gradient background color
    CAGradientLayer *gradientBg = [CAGradientLayer layer];
    gradientBg.frame = circle.frame;
    gradientBg.colors = [NSArray arrayWithObjects:
                         (id)[UIColor clearColor].CGColor,
                         (id)[UIColor clearColor].CGColor,
                         nil];
    // vertical gradient
    gradientBg.locations = [NSArray arrayWithObjects:
                            [NSNumber numberWithFloat:0.0f],
                            [NSNumber numberWithFloat:1.0f],
                            nil];

    // gradient background
    CALayer *layer = circle.layer;
    layer.masksToBounds = YES;
    [layer insertSublayer:gradientBg atIndex:0];

    return circle;
}

You can do something like,

UIView *containerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 170, 200)];


UIView *circle = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 150, 150)];

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 170, 150, 30)];
label.text = @"Request funds";
label.textAlignment = NSTextAlignmentCenter;

circle.layer.borderColor = [UIColor lightGrayColor].CGColor;
circle.layer.borderWidth = 1.0;
circle.layer.cornerRadius = 75; // width or height / 2  and  width = height (must for proper round shape)
circle.layer.masksToBounds = YES;

[containerView addSubview:circle];
[containerView addSubview:label];

[self.view addSubview: containerView];

You can modify size and origins according your need. this is demonstration that how you can achieve this.

Hope this helps!

do like

UILabel *yourItemlabel = [[UILabel alloc]initWithFrame:CGRectMake(yourView.frame.origin.x + 10,   yourView.frame.origin.y + yourView.frame.size.height + 10 , yourView.frame.size.width, 30)]; // customize the height
yourItemlabel.text = @"Request funds";
yourItemlabel.textAlignment = NSTextAlignmentCenter;
[sel.view addSubview:yourItemlabel];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM