代码改变世界

SDAutoLayerOut

2016-05-03 15:22  000aaa  阅读(744)  评论(0编辑  收藏  举报
   UIView *vi = [UIView new];
    vi.backgroundColor = [UIColor redColor];
    [self.view addSubview:vi];
    vi.sd_layout
    .leftSpaceToView(self.view,20)
    .rightSpaceToView(self.view,20)
    .topSpaceToView(self.view,30)
    .bottomSpaceToView(self.view,20);

创建一个View  通过SpeaceToView方法 设置他与参数view之间的距离为定值

 

    UILabel *autoWidthlabel = [UILabel new];
    autoWidthlabel.backgroundColor = [[UIColor orangeColor] colorWithAlphaComponent:0.5];
    autoWidthlabel.font = [UIFont systemFontOfSize:12];
    autoWidthlabel.text = @"宽度自适应(距离父view右边距10)";
    [self.view addSubview:autoWidthlabel];
    
    autoWidthlabel.sd_layout
    .widthIs(180)
    .rightSpaceToView(self.view, 10)
    .heightIs(20)
    .bottomSpaceToView(self.view, 50);

通过widthIs来设定 定宽     ----    heightIs来设定 定高

     UILabel *autoHeightlabel = [UILabel new];
    autoHeightlabel.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.5];
    autoHeightlabel.font = [UIFont systemFontOfSize:12];
    autoHeightlabel.text = @"高度自适应(距离父view左边距10,底部和其右侧label相同,宽度为100)";
    [self.view addSubview:autoHeightlabel];
    autoHeightlabel.sd_layout
    .bottomEqualToView(autoWidthlabel)//底边沿和 autoWidthlabel 低边沿 平齐
    .leftSpaceToView(self.view, 10)
    .widthIs(100)
    .autoHeightRatio(0);//高度自动缩放

 

    UIView *vi1 = [UIView new];
    [self.view addSubview:vi1];
    vi1.backgroundColor = [UIColor orangeColor];
    vi1.sd_layout
    .leftSpaceToView(self.view,20)
    .topSpaceToView(self.view,20)
    .widthRatioToView(autoHeightlabel,1)//设置宽度和autoHeightlabel的宽度1:1
    .heightEqualToWidth(2);//设置高宽比例

 

     UIView *v = [UIView new];
    [self.view addSubview:v];
    v.backgroundColor = [UIColor blueColor];
    v.sd_layout
    .widthIs(50)
    .heightEqualToWidth()
    .centerYEqualToView(self.view)//设置 v的center x 设 参数 self.view 的center X 一样
    .centerXEqualToView(self.view);//设置 v的center y 设 参数 self.view 的center y 一样

 

//  --------- attributedString测试:行间距为8 ---------------------------

 

NSString *text = @"attributedString测试:行间距为8。彩虹网络卡福利费绿调查开房;卡法看得出来分开了的出口来反馈率打开了房;快烦死了;了; 调查开房;;v单纯考虑分离开都快来反馈来看发v离开的积分房积分jdhflgfkkvvm.cm。";
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:8];
    UIColor *color = [UIColor blackColor];
    NSAttributedString *string = [[NSAttributedString alloc] initWithString:text attributes:@{NSForegroundColorAttributeName : color, NSParagraphStyleAttributeName: paragraphStyle}];
    
    UILabel *label = [UILabel new];
    [self.view addSubview:label];
    label.attributedText = string;

    label.sd_layout
    .leftSpaceToView(self.view, 10)
    .rightSpaceToView(self.view, 10)
    .topSpaceToView(self.view, 40)
    .autoHeightRatio(0);
    
    // 标注lable的text为attributedString
    label.isAttributedContent = YES;

 

  // 设置圆角

view0.sd_cornerRadiusFromHeightRatio = @(0.55); // 设置view0的圆角半径为自身高度的0.5倍

 

UIScrollView *scrollView = [UIScrollView new];
    [self.view addSubview:scrollView];
    
    [scrollView sd_addSubviews:@[self.view0, self.view1, self.view2, self.view3]];
    
    
    scrollView.sd_layout.spaceToSuperView(UIEdgeInsetsZero);
    
    self.view0.sd_layout
    .leftSpaceToView(scrollView, 20)
    .rightSpaceToView(scrollView, 20)
    .topSpaceToView(scrollView, 20)
    .heightIs(150);
    
    self.view1.sd_layout
    .widthIs(200)
    .heightIs(200)
    .centerXEqualToView(scrollView)
    .topSpaceToView(self.view0, 20);
    
    self.view2.sd_layout
    .leftSpaceToView(scrollView, 50)
    .rightSpaceToView(scrollView, 50)
    .topSpaceToView(self.view1, 20)
    .heightIs(150);
    
    self.view3.sd_layout
    .widthIs(250)
    .heightEqualToWidth()
    .centerXEqualToView(scrollView)
    .topSpaceToView(self.view2, 20);

    // scrollview自动contentsize
    [scrollView setupAutoContentSizeWithBottomView:self.view3 bottomMargin:20];
    
    // 设置圆角
    self.view0.sd_cornerRadiusFromHeightRatio = @(0.5); // 设置view0的圆角半径为自身高度的0.5倍