1

I use AsyncDisplayKit write button , and use ASButtonNode , but I can't set title when I use - (void)setAttributedTitle:(NSAttributedString *)title forState:(ASButtonState)state . It can respond action, but without title. Am I worry or something else need set ?

Here is code :

NSDictionary *placeholderAttrs = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-LightItalic" size:14.0f] , NSForegroundColorAttributeName: [UIColor greenColor] };

ASButtonNode *button = [[ASButtonNode alloc] init];
[button setAttributedTitle:[[NSAttributedString alloc] initWithString:@"button" attributes:placeholderAttrs] forState:ASButtonStateNormal];
[button addTarget:self action:@selector(action:) forControlEvents:ASControlNodeEventTouchUpInside];
[button setFrame:CGRectMake(20, 20, 100, 44)];
button.backgroundColor = [UIColor redColor];
button.titleNode.displaysAsynchronously = NO;
[self.view addSubnode:button];

Thanks.

Bista
  • 7,869
  • 3
  • 27
  • 55
leo
  • 91
  • 4

3 Answers3

1

@leo, It looks like you may just need to measure your ASButtonNode before adding it to your view.

[button measure:...];

VTSoup
  • 36
  • 4
0

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
}
Community
  • 1
  • 1
Hieu Nguyen
  • 474
  • 1
  • 5
  • 21
0

you can use this code

fileprivate var noteButtonNode : ASButtonNode = ASButtonNode()
addSubnode(noteButtonNode)
let attributedTitle : NSAttributedString = NSAttributedString(
            string: "Your_Text",
            attributes: [
                NSFontAttributeName: UIFont.init(name: Fonts.Your_Font, size: 16)!,
                NSForegroundColorAttributeName: Colors.Your_Color
            ])
        noteButtonNode.setAttributedTitle(attributedTitle, for: ASControlState())
swetansh kumar
  • 475
  • 7
  • 17