Texture布局例子

作者: 1a392df793c3 | 来源:发表于2017-11-07 14:17 被阅读812次

    Check out 例子

    简单的带有左右对齐的标题


    让我们来创建这个布局,我们将会使用到:

    • 一个垂直的 ASStackLayoutSpec
    • 一个水平的 ASStackLayoutSpec
    • ASInsetlayoutSpec 插入整个标题

    Objective-C

    - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
    {
      // when the username / location text is too long, 
      // shrink the stack to fit onscreen rather than push content to the right, offscreen
      ASStackLayoutSpec *nameLocationStack = [ASStackLayoutSpec verticalStackLayoutSpec];
      nameLocationStack.style.flexShrink = 1.0;
      nameLocationStack.style.flexGrow = 1.0;
      
      // if fetching post location data from server, 
      // check if it is available yet and include it if so
      if (_postLocationNode.attributedText) {
        nameLocationStack.children = @[_usernameNode, _postLocationNode];
      } else {
        nameLocationStack.children = @[_usernameNode];
      }
      
      // horizontal stack
      ASStackLayoutSpec *headerStackSpec = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal
                                                                                   spacing:40
                                                                            justifyContent:ASStackLayoutJustifyContentStart
                                                                                alignItems:ASStackLayoutAlignItemsCenter
                                                                                  children:@[nameLocationStack, _postTimeNode]];
      
      // inset the horizontal stack
      return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(0, 10, 0, 10) child:headerStackSpec];
    }
    

    试着将屏幕从竖屏转为横屏看看布局间距是如何拉伸和收缩的。

    插入文本覆盖物的图片


    这个布局我们将会使用到:

    • ASInsetLayoutSpec 来插入文本
    • ASOverlayLayoutSpec 来添加文本覆盖到照片上
      Objective-C
    - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
    {
      _photoNode.style.preferredSize = CGSizeMake(USER_IMAGE_HEIGHT*2, USER_IMAGE_HEIGHT*2);
    
      // INIFINITY is used to make the inset unbounded
      UIEdgeInsets insets = UIEdgeInsetsMake(INFINITY, 12, 12, 12);
      ASInsetLayoutSpec *textInsetSpec = [ASInsetLayoutSpec insetLayoutSpecWithInsets:insets child:_titleNode];
      
      return [ASOverlayLayoutSpec overlayLayoutSpecWithChild:_photoNode overlay:textInsetSpec];
    }
    

    带有边缘图标的照片


    创建这个布局我们将会用到:

    • ASAbsoluteLayoutSpec 来放置图片和图标,它们必须被设置各自的大小和位置通过它们ASLayoutable 属性。
      Objective-C
    - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
    {
      _iconNode.style.preferredSize = CGSizeMake(40, 40);
      _iconNode.style.layoutPosition = CGPointMake(150, 0);
      
      _photoNode.style.preferredSize = CGSizeMake(150, 150);
      _photoNode.style.layoutPosition = CGPointMake(40 / 2.0, 40 / 2.0);
      
      return [ASAbsoluteLayoutSpec absoluteLayoutSpecWithSizing:ASAbsoluteLayoutSpecSizingSizeToFit
                                                       children:@[_photoNode, _iconNode]];
    }
    

    简单的文本Cell


    要创建上面这个在Pinterest上的搜索页面上的单行Cell,我们将会用到:

    • ASInsetLayoutSpec 插入文本
    • ASCenterLayoutSpec 根据指定的属性约束这个文本居中
      Objective-C
    - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
    {
        UIEdgeInsets insets = UIEdgeInsetsMake(0, 12, 4, 4);
        ASInsetLayoutSpec *inset = [ASInsetLayoutSpec insetLayoutSpecWithInsets:insets
                                                                          child:_titleNode];
    
        return [ASCenterLayoutSpec centerLayoutSpecWithCenteringOptions:ASCenterLayoutSpecCenteringY
                                                          sizingOptions:ASCenterLayoutSpecSizingOptionMinimumX
                                                                  child:inset];
    }
    

    顶部和底部的分割线


    要创建上面这个布局,我们将用到:

    • ASInsetLayoutSpec 插入文本
    • 一个垂直的ASStackLayoutSpec来堆叠两个分割线在文本的顶上和底下。
      下面这个图解展示了布局表的组成(layout specs + nodes)



      下面的代码可以在例子 ASLayoutSpecPlayground中找到。
      Objective-C

    - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
    {
      _topSeparator.style.flexGrow = 1.0;
      _bottomSeparator.style.flexGrow = 1.0;
    
      ASInsetLayoutSpec *insetContentSpec = [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(20, 20, 20, 20) child:_textNode];
    
      return [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical
                                                     spacing:0
                                              justifyContent:ASStackLayoutJustifyContentCenter
                                                  alignItems:ASStackLayoutAlignItemsStretch
                                                    children:@[_topSeparator, insetContentSpec, _bottomSeparator]];
    }
    

    相关文章

      网友评论

        本文标题:Texture布局例子

        本文链接:https://www.haomeiwen.com/subject/yiwlmxtx.html