Add custom Button with AsyncDisplayKit


Answers

App Crashes when i add UIImageView to ASCellNode from AsyncDisplayKit

Finally I was able to add UIKit components in the ASCellNode subclass implementation using -[ASDisplayNode initWithViewBlock:].

Following is the code which wraps an ASDisplayNode over UIImageView and returns the resulting node :

ASDisplayNode *_imageViewNode = [[ASDisplayNode alloc] initWithViewBlock:^{
    UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"testImage.jpg"]];
    return image;
}];
_imageViewNode.frame = CGRectMake(10.0f, 10.0f, 40.0f, 40.0f);
[self addSubnode:_imageViewNode];

Similarly, code for UIWebView component :

ASDisplayNode *webViewNode = [[ASDisplayNode alloc]initWithViewBlock:^{

NSString *strURL = @"http://www.google.com";
NSURL *url = [NSURL URLWithString:strURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
UIWebView *webview = [[UIWebView alloc]init];
[webview loadRequest:urlRequest];
return webview;
}];

[webViewNode setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:webViewNode.view];
Question

I am developing an IOS application. I use Facebook AsyncDisplayKit library. I want to a button in ASNodeCell Bu I got "Variable 'node' is uninitialized when captured by block. How can I add UIButton or UIWebView control in ASNodeCell. Please help me

dispatch_queue_t _backgroundContentFetchingQueue;
    _backgroundContentFetchingQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);

dispatch_async(_backgroundContentFetchingQueue, ^{
    ASDisplayNode *node = [[ASDisplayNode alloc] initWithViewBlock:^UIView *{
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button sizeToFit];
        node.frame = button.frame;
        return button;
    }];

                           // Use `node` as you normally would...
    node.backgroundColor = [UIColor redColor];

    [self.view addSubview:node.view];
});




Please reference into this topic here Add custom Button with AsyncDisplayKit

Basiclly, ASTextNode will be used as button, also below func example aim to set title, color, font-weight and color (in Swift)

func configureTextNode(text: String, size: CGFloat, frame : CGRect = CGRectMake(0,0,0,0), bold: Bool = false, color: UIColor = UIColor.whiteColor(), textAlignment: NSTextAlignment = .Left) -> ASTextNode {
    let textNode = ASTextNode()
    let range = NSMakeRange(0, text.characters.count)

    // Set String
    let mutableString = NSMutableAttributedString(string: text) // Set string

    // Set size and font-weight
    let fontSize = bold ? UIFont.boldSystemFontOfSize(size) :  UIFont.systemFontOfSize(size)
    mutableString.addAttribute(NSFontAttributeName, value: fontSize, range: range)

    // Set text color
    mutableString.addAttribute(NSForegroundColorAttributeName, value: color, range: range)

    // Set alignment
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = textAlignment
    mutableString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: range)

    // Add attributes into node
    textNode.attributedString = mutableString

    // Node Frame
    textNode.frame = frame

    return textNode
}



Related



Tags