如何在iOS中的UIView子视图下方插入UILabel

Posted

技术标签:

【中文标题】如何在iOS中的UIView子视图下方插入UILabel【英文标题】:How to insert UILabel below UIView subview in iOS 【发布时间】:2016-07-27 06:52:00 【问题描述】:

我需要创建一些圆形 UIView 并在其下方显示文本。我已经成功创建了圈子。但是有什么方法可以在该圆圈下方添加标签。我需要用文字显示很多圆圈。下面是我的圈子代码,附上我需要创建的屏幕截图。

- (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;

【问题讨论】:

请访问此链接,您的问题的答案已经存在:***.com/questions/10051514/… 【参考方案1】:

喜欢

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];

【讨论】:

-(UILabel *)createViewLabel:(UIView *)view text:(NSString *)labelText UILabel *labelCon = [UILabel new]; labelCon.frame = CGRectMake(view.frame.origin.x-15, view.frame.origin.y + view.frame.size.height, view.frame.size.width+30, 35); labelCon.text = 标签文本; labelCon.numberOfLines = 0; labelCon.backgroundColor = [UIColor clearColor]; labelCon.font = [UIFont fontWithName:@"Roboto-Regular" size:12]; labelCon.textColor = [UIColor darkGrayColor]; labelCon.textAlignment = NSTextAlignmentCenter;返回标签Con; 【参考方案2】:

你可以这样做,

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];

您可以根据需要修改大小和来源。这是演示如何实现这一点。

希望这会有所帮助!

【讨论】:

以上是关于如何在iOS中的UIView子视图下方插入UILabel的主要内容,如果未能解决你的问题,请参考以下文章

iOs - 将uiview插入代码中的另一个视图,保持约束

如何在iOS中查找UIView子视图的值变化

UIView子视图中的IOS UIImagePicker

使用 IOS 中的约束将 UIView 中的子视图居中

模糊包含键盘及其下方内容的 UIView (iOS 7)

从 iOS 中的 UIView 子视图重新加载时 UITableView 未重新加载