2016-07-27 61 views
0

我需要創建一些圓形UIView並在其下方顯示文本。我已經成功創建了圈子。但是有什麼方法可以在該圓圈下方添加標籤。我需要用文字顯示很多圈子。以下是我的代碼圈和附加是我需要創建的屏幕截圖。 enter image description here如何在UIView子視圖中插入UILabel在iOS中

- (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; 
} 
+0

請訪問此鏈接,回答你的問題已經存在: http://stackoverflow.com/questions/10051514/how-can-i-insert-a-subview其他子視圖 – Aamir

回答

0

不喜歡

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

- (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 = labelText; labelCon.numberOfLines = 0; labelCon.backgroundColor = [UIColor clearColor]; labelCon.font = [UIFont fontWithName:@「Roboto-Regular」size:12]; labelCon.textColor = [UIColor darkGrayColor]; labelCon.textAlignment = NSTextAlignmentCenter; return labelCon; } – Kashif

0

你可以做這樣的事情,

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

您可以修改請根據需要大小和來源。這是演示如何你可以實現這一點。

希望這會有所幫助!