### ASRatioLayoutSpec Objective-C Example Source: https://texturegroup.org/docs/layout2-layoutspec-types.html Lays out a component at a fixed 0.5 aspect ratio. ```Objective-C - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize { // Half Ratio ASStaticSizeDisplayNode *subnode = ASDisplayNodeWithBackgroundColor([UIColor greenColor], CGSizeMake(100, 100)); return [ASRatioLayoutSpec ratioLayoutSpecWithRatio:0.5 child:subnode]; } ``` -------------------------------- ### ASRatioLayoutSpec Swift Example Source: https://texturegroup.org/docs/layout2-layoutspec-types.html Lays out a component at a fixed 0.5 aspect ratio in Swift. ```Swift override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec { // Half Ratio let subnode = ASDisplayNodeWithBackgroundColor(UIColor.green, CGSize(width: 100, height: 100.0)) let ratioSpec = ASRatioLayoutSpec(ratio: 0.5, child: subnode) return ratioSpec } ``` -------------------------------- ### Run pod install Source: https://texturegroup.org/docs/installation.html Run 'pod install' in the project directory after quitting Xcode completely. ```bash > pod install ``` -------------------------------- ### Create ASDimension instances Source: https://texturegroup.org/docs/layout2-conversion-guide.html Examples of using handy functions to create ASDimension objects in Objective-C. ```Objective-C // 2.0: // Handy functions to create ASDimensions ASDimension dimensionInPoints; dimensionInPoints = ASDimensionMake(ASDimensionTypePoints, 5.0) dimensionInPoints = ASDimensionMake(5.0) dimensionInPoints = ASDimensionMakeWithPoints(5.0) dimensionInPoints = ASDimensionMake("5.0pt"); ASDimension dimensionInFractions; dimensionInFractions = ASDimensionMake(ASDimensionTypeFraction, 0.5) dimensionInFractions = ASDimensionMakeWithFraction(0.5) dimensionInFractions = ASDimensionMake("50%"); ``` -------------------------------- ### Creating ASDimension values Source: https://texturegroup.org/docs/layout2-api-sizing.html Examples of creating ASDimension instances representing relative percentages or fixed point values. ```Objective-C **// dimension returned is relative (%)** ASDimensionMake(@"50%"); ASDimensionMakeWithFraction(0.5); **// dimension returned in points** ASDimensionMake(@"70pt"); ASDimensionMake(70); ASDimensionMakeWithPoints(70); ``` -------------------------------- ### Creating ASDimension values in Swift Source: https://texturegroup.org/docs/layout2-api-sizing.html Examples of creating ASDimension instances representing relative percentages or fixed point values in Swift. ```Swift **// dimension returned is relative (%)** ASDimensionMake("50%") ASDimensionMakeWithFraction(0.5) **// dimension returned in points** ASDimensionMake("70pt") ASDimensionMake(70) ASDimensionMakeWithPoints(70) ``` -------------------------------- ### Initialize ASScrollNode with layoutSpecBlock Source: https://texturegroup.org/docs/scroll-node.html Example showing how to initialize an ASScrollNode, enable automatic subnode and content size management, and define a layoutSpecBlock. ```objective-c // NOTE: If you are using a horizontal stack, set scrollNode.scrollableDirections. ASScrollNode *scrollNode = [[ASScrollNode alloc] init]; scrollNode.automaticallyManagesSubnodes = YES; scrollNode.automaticallyManagesContentSize = YES; scrollNode.layoutSpecBlock = ^(ASDisplayNode *node, ASSizeRange constrainedSize){ ASStackLayoutSpec *stack = [ASStackLayoutSpec verticalStackLayoutSpec]; // Add children to the stack. return stack; }; ``` -------------------------------- ### ASRelativeDimension Example (Objective-C) Source: https://texturegroup.org/docs/layout-api-sizing.html Uses flexBasis with ASRelativeDimensionMakeWithPercent to set two child nodes of a horizontal stack to share width 40/60. ```objectivec leftSideStack.flexBasis = ASRelativeDimensionMakeWithPercent(0.4f); self.detailLabel.flexBasis = ASRelativeDimensionMakeWithPercent(0.6f); [horizontalStack setChildren:@[leftSideStack, self.detailLabel]]; ``` -------------------------------- ### ASRelativeDimension Example (Swift) Source: https://texturegroup.org/docs/layout-api-sizing.html Uses flexBasis with ASDimensionMake to set two child nodes of a horizontal stack to share width 40/60. ```swift leftSideStack.style.flexBasis = ASDimensionMake("40%") detailsLabel.style.flexBasis = ASDimensionMake("60%") horizontalStack.children = [leftSideStack, detailsLabel] ``` -------------------------------- ### Configure ASTextNode with line spacing in Swift Source: https://texturegroup.org/docs/text-node.html Example of setting up an ASTextNode with NSParagraphStyle line spacing, which may cause layout issues. ```Swift let someLongString = "..." let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = 10.0 let font = UIFont(name: "SomeFontName", size: 15.0) let attributes = [ NSFontAttributeName: font, NSParagraphStyleAttributeName: paragraphStyle ] let textNode = ASTextNode() textNode.maximumNumberOfLines = 4 textNode.attributedText = NSAttributedString(string: someLongString, attributes: attributes) ``` -------------------------------- ### Configure ASTextNode with line spacing in Objective-C Source: https://texturegroup.org/docs/text-node.html Example of setting up an ASTextNode with NSParagraphStyle line spacing, which may cause layout issues. ```Objective-C // ... NSString *someLongString = @"..."; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineSpacing = 10.0; UIFont *font = [UIFont fontWithName:@"SomeFontName" size:15]; NSDictionary *attributes = @{ NSFontAttributeName : font, NSParagraphStyleAttributeName: paragraphStyle }; ASTextNode *textNode = [[ASTextNode alloc] init]; textNode.maximumNumberOfLines = 4; textNode.attributedText = [[NSAttributedString alloc] initWithString:someLongString attributes:attributes]; // ... ``` -------------------------------- ### ASCornerLayoutSpec Objective-C Example Source: https://texturegroup.org/docs/layout2-layoutspec-types.html Demonstrates using ASCornerLayoutSpec to position a badge node at the top right corner of an avatar node in Objective-C. ```objective-c - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize { ... // Layout the center of badge to the top right corner of avatar. ASCornerLayoutSpec *cornerSpec = [ASCornerLayoutSpec cornerLayoutSpecWithChild:self.avatarNode corner:self.badgeNode location:ASCornerLayoutLocationTopRight]; // Slightly shift center of badge inside of avatar. cornerSpec.offset = CGPointMake(-3, 3); ... } ``` -------------------------------- ### ASCornerLayoutSpec Swift Example Source: https://texturegroup.org/docs/layout2-layoutspec-types.html Demonstrates using ASCornerLayoutSpec to position a badge node at the top right corner of an avatar node in Swift. ```swift override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec { ... // Layout the center of badge to the top right corner of avatar. let cornerSpec = ASCornerLayoutSpec(child: avatarNode, corner: badgeNode, location: .topRight) // Slightly shift center of badge inside of avatar. cornerSpec.offset = CGPoint(x: -3, y: 3) ... } ``` -------------------------------- ### Configure autoplay, autorepeat, and muting (Objective-C) Source: https://texturegroup.org/docs/video-node.html Set shouldAutoplay, shouldAutorepeat, and muted properties to control video behavior. This example configures a node to play once silently. ```Objective-C videoNode.shouldAutoplay = YES; videoNode.shouldAutorepeat = NO; videoNode.muted = YES; ``` -------------------------------- ### Configure autoplay, autorepeat, and muting (Swift) Source: https://texturegroup.org/docs/video-node.html Set shouldAutoplay, shouldAutorepeat, and muted properties to control video behavior. This example configures a node to play once silently. ```Swift videoNode.shouldAutoplay = true videoNode.shouldAutorepeat = false videoNode.muted = true ``` -------------------------------- ### Thread-safe node block example (Objective-C) Source: https://texturegroup.org/docs/containers-ascollectionnode.html Shows how to access the data model outside the node block to ensure thread safety. The index is used to fetch the model before creating the block. ```objectivec - (ASCellNodeBlock)collectionNode:(ASCollectionNode *)collectionNode nodeBlockForItemAtIndexPath:(NSIndexPath *)indexPath { PhotoModel *photoModel = [_photoFeed objectAtIndex:indexPath.row]; // this may be executed on a background thread - it is important to make sure it is thread safe ASCellNode *(^cellNodeBlock)() = ^ASCellNode *() { PhotoCellNode *cellNode = [[PhotoCellNode alloc] initWithPhoto:photoModel]; cellNode.delegate = self; return cellNode; }; return cellNodeBlock; } ``` -------------------------------- ### ASDisplayNode keep alive reference example Source: https://texturegroup.org/docs/faq.html Shows creating an ASTextNode, setting its attributed string, and adding it as a subnode. The retain cycle output is shown, which is intentionally created when the node's view is in a live view hierarchy. This behavior should not be relied upon in good application design. ```objective-c ASTextNode *title=[[ASTextNode alloc]init]; title.attributedString=Text; [self addSubnode:title]; retain cycles ( "-> _keepalive_node -> ASTextNode ", "-> _view -> _ASDisplayView " ) ``` -------------------------------- ### ASDKViewController init with ASCollectionNode (Objective-C) Source: https://texturegroup.org/docs/containers-ascollectionnode.html Shows how to create an ASCollectionNode and assign it to an ASDKViewController using initWithNode:. ```objectivec - (instancetype)init { _flowLayout = [[UICollectionViewFlowLayout alloc] init]; _collectionNode = [[ASCollectionNode alloc] initWithCollectionViewLayout:_flowLayout]; self = [super initWithNode:_collectionNode]; if (self) { _flowLayout.minimumInteritemSpacing = 1; _flowLayout.minimumLineSpacing = 1; } return self; } ``` -------------------------------- ### Initialize ASDisplayNode in Swift Source: https://texturegroup.org/docs/display-node.html Basic initialization of an ASDisplayNode and setting its properties in Swift. ```swift let node = ASDisplayNode() node.backgroundColor = .orange node.bounds = CGRect(x: 0, y: 0, width: 100, height: 100) print("Underlying view: \(node.view)") ``` -------------------------------- ### ASInsetLayoutSpec implementation Source: https://texturegroup.org/docs/layout2-layoutspec-types.html Examples of using ASInsetLayoutSpec to apply insets to a child node in Objective-C and Swift. ```objective-c - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize { ... UIEdgeInsets *insets = UIEdgeInsetsMake(10, 10, 10, 10); ASInsetLayoutSpec *headerWithInset = [ASInsetLayoutSpec insetLayoutSpecWithInsets:insets child:textNode]; ... } ``` ```swift override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec { ... let insets = UIEdgeInsets(top: 10.0, left: 10.0, bottom: 10.0, right: 10.0) let headerWithInset = ASInsetLayoutSpec(insets: insets, child: textNode) ... } ``` -------------------------------- ### ASStackLayoutSpec Swift Implementation Source: https://texturegroup.org/docs/layout2-layoutspec-types.html Example of creating an ASStackLayoutSpec in Swift with horizontal direction and constrained dimensions. ```swift override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec { let mainStack = ASStackLayoutSpec(direction: .horizontal, spacing: 6.0, justifyContent: .start, alignItems: .center, children: [titleNode, subtitleNode]) // Set some constrained size to the stack mainStack.style.minWidth = ASDimensionMakeWithPoints(60.0) mainStack.style.maxHeight = ASDimensionMakeWithPoints(40.0) return mainStack } ``` -------------------------------- ### ASStackLayoutSpec Objective-C Implementation Source: https://texturegroup.org/docs/layout2-layoutspec-types.html Example of creating an ASStackLayoutSpec in Objective-C with horizontal direction and constrained dimensions. ```objective-c - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize { ASStackLayoutSpec *mainStack = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:6.0 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsCenter children:@[_iconNode, _countNode]]; // Set some constrained size to the stack mainStack.style.minWidth = ASDimensionMakeWithPoints(60.0); mainStack.style.maxHeight = ASDimensionMakeWithPoints(40.0); return mainStack; } ``` -------------------------------- ### ASDKViewController init with ASCollectionNode (Swift) Source: https://texturegroup.org/docs/containers-ascollectionnode.html Shows how to create an ASCollectionNode and assign it to an ASDKViewController using init(node:). ```swift init() { flowLayout = UICollectionViewFlowLayout() collectionNode = ASCollectionNode(collectionViewLayout: flowLayout) super.init(node: collectionNode) flowLayout.minimumInteritemSpacing = 1 flowLayout.minimumLineSpacing = 1 } ``` -------------------------------- ### Enable Subtree Rasterization Source: https://texturegroup.org/docs/subtree-rasterization.html Enables precompositing for the entire node hierarchy starting from the root node. ```swift rootNode.enableSubtreeRasterization() ``` -------------------------------- ### Basic ASVideoNode usage with AVAsset (Swift) Source: https://texturegroup.org/docs/video-node.html Initialize an ASVideoNode and assign an AVAsset from a URL. Requires linking AVFoundation. This is the Swift version. ```Swift let videoNode = ASVideoNode() let asset = AVAsset(url: URL(string: "http://www.w3schools.com/html/mov_bbb.mp4")!) videoNode.asset = asset ``` -------------------------------- ### Enable Subtree Rasterization Source: https://texturegroup.org/docs/subtree-rasterization.html Enables precompositing for the entire node hierarchy starting from the root node. ```objective-c [rootNode enableSubtreeRasterization]; ``` -------------------------------- ### Wrap a UIView with initWithViewBlock in Swift Source: https://texturegroup.org/docs/display-node.html Initializes a node with a block that returns a view in Swift. ```swift let node = ASDisplayNode { () -> UIView in let view = SomeView() return view } ``` -------------------------------- ### ASAbsoluteLayoutSpec implementation in Objective-C Source: https://texturegroup.org/docs/layout2-layoutspec-types.html Example of defining an absolute layout by setting layoutPosition and preferredSize for child nodes. ```Objective-C - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize { CGSize maxConstrainedSize = constrainedSize.max; // Layout all nodes absolute in a static layout spec guitarVideoNode.style.layoutPosition = CGPointMake(0, 0); guitarVideoNode.style.preferredSize = CGSizeMake(maxConstrainedSize.width, maxConstrainedSize.height / 3.0); nicCageVideoNode.style.layoutPosition = CGPointMake(maxConstrainedSize.width / 2.0, maxConstrainedSize.height / 3.0); nicCageVideoNode.style.preferredSize = CGSizeMake(maxConstrainedSize.width / 2.0, maxConstrainedSize.height / 3.0); simonVideoNode.style.layoutPosition = CGPointMake(0.0, maxConstrainedSize.height - (maxConstrainedSize.height / 3.0)); simonVideoNode.style.preferredSize = CGSizeMake(maxConstrainedSize.width/2, maxConstrainedSize.height / 3.0); hlsVideoNode.style.layoutPosition = CGPointMake(0.0, maxConstrainedSize.height / 3.0); hlsVideoNode.style.preferredSize = CGSizeMake(maxConstrainedSize.width / 2.0, maxConstrainedSize.height / 3.0); return [ASAbsoluteLayoutSpec absoluteLayoutSpecWithChildren:@[guitarVideoNode, nicCageVideoNode, simonVideoNode, hlsVideoNode]]; } ``` -------------------------------- ### ASCenterLayoutSpec Objective-C Example Source: https://texturegroup.org/docs/layout2-layoutspec-types.html Centers a subnode within the layout spec using XY centering options. ```Objective-C - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize { ASStaticSizeDisplayNode *subnode = ASDisplayNodeWithBackgroundColor([UIColor greenColor], CGSizeMake(70, 100)); return [ASCenterLayoutSpec centerLayoutSpecWithCenteringOptions:ASCenterLayoutSpecCenteringXY sizingOptions:ASCenterLayoutSpecSizingOptionDefault child:subnode] } ``` -------------------------------- ### Initialize ASDisplayNode Source: https://texturegroup.org/docs/display-node.html Basic initialization of an ASDisplayNode and setting its properties. ```objective-c ASDisplayNode *node = [[ASDisplayNode alloc] init]; node.backgroundColor = [UIColor orangeColor]; node.bounds = CGRectMake(0, 0, 100, 100); NSLog(@"Underlying view: %@", node.view); ``` -------------------------------- ### ASOverlayLayoutSpec implementation Source: https://texturegroup.org/docs/layout2-layoutspec-types.html Examples of using ASOverlayLayoutSpec to layer a foreground node over a background node in Objective-C and Swift. ```objective-c - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize { ASDisplayNode *backgroundNode = ASDisplayNodeWithBackgroundColor([UIColor blueColor]); ASDisplayNode *foregroundNode = ASDisplayNodeWithBackgroundColor([UIColor redColor]); return [ASOverlayLayoutSpec overlayLayoutSpecWithChild:backgroundNode overlay:foregroundNode]; } ``` ```swift override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec { let backgroundNode = ASDisplayNodeWithBackgroundColor(UIColor.blue) let foregroundNode = ASDisplayNodeWithBackgroundColor(UIColor.red) return ASOverlayLayoutSpec(child: backgroundNode, overlay: foregroundNode) } ``` -------------------------------- ### Basic ASImageNode Usage (Swift) Source: https://texturegroup.org/docs/image-node.html Initialize an ASImageNode and set its image and content mode using Swift. ```swift let imageNode = ASImageNode() imageNode.image = UIImage(named: "someImage") imageNode.contentMode = .scaleAspectFill ``` -------------------------------- ### Thread-safe nodeBlockForRowAt implementation Source: https://texturegroup.org/docs/containers-astablenode.html Example of accessing the data model outside the block to ensure thread safety. ```Swift func tableNode(_ tableNode: ASTableNode, nodeBlockForRowAt indexPath: IndexPath) -> ASCellNodeBlock { guard photoFeed.count > indexPath.row else { return { ASCellNode() } } let photoModel = photoFeed[indexPath.row] // this may be executed on a background thread - it is important to make sure it is thread safe let cellNodeBlock = { () -> ASCellNode in let cellNode = PhotoCellNode(photo: photoModel) cellNode.delegate = self return cellNode } return cellNodeBlock } ``` -------------------------------- ### Thread-safe nodeBlockForRowAtIndexPath implementation Source: https://texturegroup.org/docs/containers-astablenode.html Example of accessing the data model outside the block to ensure thread safety. ```Objective-C - (ASCellNodeBlock)tableNode:(ASTableNode *)tableNode nodeBlockForRowAtIndexPath:(NSIndexPath *)indexPath { PhotoModel *photoModel = [_photoFeed objectAtIndex:indexPath.row]; // this may be executed on a background thread - it is important to make sure it is thread safe ASCellNode *(^cellNodeBlock)() = ^ASCellNode *() { PhotoCellNode *cellNode = [[PhotoCellNode alloc] initWithPhoto:photoModel]; cellNode.delegate = self; return cellNode; }; return cellNodeBlock; } ``` -------------------------------- ### Placeholder Text - Swift Source: https://texturegroup.org/docs/editable-text-node.html Set the attributedPlaceholderText property to display a placeholder that disappears when the user starts typing. ```swift editableTextNode.attributedPlaceholderText = NSAttributedString(string: "Type something here...") ``` -------------------------------- ### Placeholder Text - Objective-C Source: https://texturegroup.org/docs/editable-text-node.html Set the attributedPlaceholderText property to display a placeholder that disappears when the user starts typing. ```objectivec editableTextNode.attributedPlaceholderText = [[NSAttributedString alloc] initWithString:@"Type something here..."]; ``` -------------------------------- ### Basic ASVideoNode usage with AVAsset (Objective-C) Source: https://texturegroup.org/docs/video-node.html Initialize an ASVideoNode and assign an AVAsset from a URL. Requires linking AVFoundation. This is the Objective-C version. ```Objective-C ASVideoNode *videoNode = [[ASVideoNode alloc] init]; AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:@"http://www.w3schools.com/html/mov_bbb.mp4"]]; videoNode.asset = asset; ``` -------------------------------- ### ASAbsoluteLayoutSpec implementation in Swift Source: https://texturegroup.org/docs/layout2-layoutspec-types.html Example of defining an absolute layout by setting layoutPosition and preferredSize for child nodes in Swift. ```Swift override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec { let maxConstrainedSize = constrainedSize.max // Layout all nodes absolute in a static layout spec guitarVideoNode.style.layoutPosition = CGPoint.zero guitarVideoNode.style.preferredSize = CGSize(width: maxConstrainedSize.width, height: maxConstrainedSize.height / 3.0) nicCageVideoNode.style.layoutPosition = CGPoint(x: maxConstrainedSize.width / 2.0, y: maxConstrainedSize.height / 3.0) nicCageVideoNode.style.preferredSize = CGSize(width: maxConstrainedSize.width / 2.0, height: maxConstrainedSize.height / 3.0) simonVideoNode.style.layoutPosition = CGPoint(x: 0.0, y: maxConstrainedSize.height - (maxConstrainedSize.height / 3.0)) simonVideoNode.style.preferredSize = CGSize(width: maxConstrainedSize.width / 2.0, height: maxConstrainedSize.height / 3.0) hlsVideoNode.style.layoutPosition = CGPoint(x: 0.0, y: maxConstrainedSize.height / 3.0) hlsVideoNode.style.preferredSize = CGSize(width: maxConstrainedSize.width / 2.0, height: maxConstrainedSize.height / 3.0) return ASAbsoluteLayoutSpec(children: [guitarVideoNode, nicCageVideoNode, simonVideoNode, hlsVideoNode]) } ``` -------------------------------- ### Configure ASLayoutElement sizing with ASDimensionUnitAuto Source: https://texturegroup.org/docs/layout2-conversion-guide.html Demonstrates how to use ASDimensionUnitAuto for intrinsic sizing versus setting explicit sizes for nodes without intrinsic content. ```Objective-C // 2.0: // No specific size needs to be set as the imageNode's size // will be calculated from the content (the image in this case) ASImageNode *imageNode = [ASImageNode new]; imageNode.image = ...; // Specific size must be set for ASLayoutElement objects that // do not have an intrinsic content size (ASVideoNode does not // have a size until it's video downloads) ASVideoNode *videoNode = [ASVideoNode new]; videoNode.style.preferredSize = CGSizeMake(200, 100); ``` -------------------------------- ### Setting ASDisplayNode and ASLayoutSpec size Source: https://texturegroup.org/docs/layout2-conversion-guide.html Demonstrates setting width and preferred size using the 2.0 style API compared to 1.x limitations. ```Objective-C // 1.x: // no good way to set an intrinsic size // 2.0: ASDisplayNode *ASDisplayNode = ...; // width 100 points, height: auto displayNode.style.width = ASDimensionMakeWithPoints(100); // width 50%, height: auto displayNode.style.width = ASDimensionMakeWithFraction(0.5); ASLayoutSpec *layoutSpec = ...; // width 100 points, height 100 points layoutSpec.style.preferredSize = CGSizeMake(100, 100); ``` -------------------------------- ### ASRelativeLayoutSpec Swift Implementation Source: https://texturegroup.org/docs/layout2-layoutspec-types.html Example of using ASRelativeLayoutSpec in Swift to position a foreground node within a background node. ```swift override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec { ... let backgroundNode = ASDisplayNodeWithBackgroundColor(UIColor.blue) let foregroundNode = ASDisplayNodeWithBackgroundColor(UIColor.red, CGSize(width: 70.0, height: 100.0)) let relativeSpec = ASRelativeLayoutSpec(horizontalPosition: .start, verticalPosition: .start, sizingOption: [], child: foregroundNode) let backgroundSpec = ASBackgroundLayoutSpec(child: relativeSpec, background: backgroundNode) ... } ``` -------------------------------- ### ASRelativeLayoutSpec Objective-C Implementation Source: https://texturegroup.org/docs/layout2-layoutspec-types.html Example of using ASRelativeLayoutSpec in Objective-C to position a foreground node within a background node. ```objective-c - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize { ... ASDisplayNode *backgroundNode = ASDisplayNodeWithBackgroundColor([UIColor redColor]); ASStaticSizeDisplayNode *foregroundNode = ASDisplayNodeWithBackgroundColor([UIColor greenColor], CGSizeMake(70, 100)); ASRelativeLayoutSpec *relativeSpec = [ASRelativeLayoutSpec relativePositionLayoutSpecWithHorizontalPosition:ASRelativeLayoutSpecPositionStart verticalPosition:ASRelativeLayoutSpecPositionStart sizingOption:ASRelativeLayoutSpecSizingOptionDefault child:foregroundNode] ASBackgroundLayoutSpec *backgroundSpec = [ASBackgroundLayoutSpec backgroundLayoutSpecWithChild:relativeSpec background:backgroundNode]; ... } ``` -------------------------------- ### Initialize and Set URL for ASNetworkImageNode (Swift) Source: https://texturegroup.org/docs/network-image-node.html Initialize an ASNetworkImageNode and set its url property to load a remote image. ```swift let imageNode = ASNetworkImageNode() imageNode.url = URL(string: "https://someurl.com/image_uri") ``` -------------------------------- ### Constructing exact size range (ASRelativeSizeRangeMakeWithExactCGSize and ASSizeRangeMake) Source: https://texturegroup.org/docs/layout-api-sizing.html Shows the Objective-C and Swift convenience functions for constructing an exact size range (min = max = no range) when relative values are not needed. ```objective-c ASRelativeSizeRangeMakeWithExactCGSize(CGSize exact); ``` ```swift public func ASSizeRangeMake(_ exactSize: CGSize) -> ASSizeRange ``` -------------------------------- ### ASCenterLayoutSpec Swift Example Source: https://texturegroup.org/docs/layout2-layoutspec-types.html Centers a subnode within the layout spec using XY centering options in Swift. ```Swift override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec { let subnode = ASDisplayNodeWithBackgroundColor(UIColor.green, CGSize(width: 60.0, height: 100.0)) let centerSpec = ASCenterLayoutSpec(centeringOptions: .XY, sizingOptions: [], child: subnode) return centerSpec } ``` -------------------------------- ### Run carthage update Source: https://texturegroup.org/docs/installation.html Run 'carthage update' in Terminal to fetch dependencies and build frameworks. ```bash > carthage update ``` -------------------------------- ### Add Target-Action Pair (Swift) Source: https://texturegroup.org/docs/button-node.html Adds a target-action pair to respond to touch-up-inside events. This is the Swift version of the target-action example. ```swift buttonNode.addTarget(self, action: #selector(buttonPressed), forControlEvents: .touchUpInside) ``` -------------------------------- ### Initialize ASMultiplexImageNode in ASCellNode Source: https://texturegroup.org/docs/multiplex-image-node.html Initialize the node, set downloadsIntermediateImages to true, and define the imageIdentifiers array in descending order of quality. ```Objective-C - (instancetype)initWithURLs:(NSDictionary *)urls { ... _imageURLs = urls; // something like @{@"thumb": "/smallImageUrl", @"medium": ...} _multiplexImageNode = [[ASMultiplexImageNode alloc] initWithCache:nil downloader:[ASBasicImageDownloader sharedImageDownloader]]; _multiplexImageNode.downloadsIntermediateImages = YES; _multiplexImageNode.imageIdentifiers = @[ @"original", @"medium", @"thumb" ]; _multiplexImageNode.dataSource = self; _multiplexImageNode.delegate = self; ... } ``` ```Swift init(urls: [String: NSURL]) { imageURLs = urls multiplexImageNode = ASMultiplexImageNode(cache: nil, downloader: ASBasicImageDownloader.shared()) multiplexImageNode.downloadsIntermediateImages = true multiplexImageNode.imageIdentifiers = ["original", "medium", "thumb" ] multiplexImageNode.dataSource = self multiplexImageNode.delegate = self ... } ``` -------------------------------- ### Add Target-Action Pair (Objective-C) Source: https://texturegroup.org/docs/button-node.html Adds a target-action pair to respond to touch-up-inside events. This is the Objective-C version of the target-action example. ```objectivec [buttonNode addTarget:self action:@selector(buttonPressed:) forControlEvents:ASControlNodeEventTouchUpInside]; ``` -------------------------------- ### Create a Simple Header Layout in Swift Source: https://texturegroup.org/docs/automatic-layout-examples-2.html Uses a vertical ASStackLayoutSpec for text, a horizontal ASStackLayoutSpec for the header, and an ASInsetLayoutSpec for padding. ```Swift override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec { let nameLocationStack = ASStackLayoutSpec.vertical() nameLocationStack.style.flexShrink = 1.0 nameLocationStack.style.flexGrow = 1.0 if postLocationNode.attributedText != nil { nameLocationStack.children = [userNameNode, postLocationNode] } else { nameLocationStack.children = [userNameNode] } let headerStackSpec = ASStackLayoutSpec(direction: .horizontal, spacing: 40, justifyContent: .start, alignItems: .center, children: [nameLocationStack, postTimeNode]) return ASInsetLayoutSpec(insets: UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10), child: headerStackSpec) } ``` -------------------------------- ### Set Attributed Title (Swift) Source: https://texturegroup.org/docs/button-node.html Sets an attributed title for a control state. This is the Swift version of the attributed string example. ```swift buttonNode.setAttributedTitle(attributedTitle, for: .normal) ``` -------------------------------- ### Create a Simple Header Layout in Objective-C Source: https://texturegroup.org/docs/automatic-layout-examples-2.html Uses a vertical ASStackLayoutSpec for text, a horizontal ASStackLayoutSpec for the header, and an ASInsetLayoutSpec for padding. ```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]; } ``` -------------------------------- ### Set Attributed Title (Objective-C) Source: https://texturegroup.org/docs/button-node.html Sets an attributed title for a control state. This is the Objective-C version of the attributed string example. ```objectivec [self.buttonNode setAttributedTitle:attributedTitle forState:ASControlStateNormal]; ```