AsyncDisplayKit ASButtonNode setTitle不起作用

时间:2016-01-14 05:54:48

标签: ios objective-c asyncdisplaykit

我使用AsyncDisplayKit写入按钮,并使用ASButtonNode,但在使用- (void)setAttributedTitle:(NSAttributedString *)title forState:(ASButtonState)state时无法设置标题。它可以响应行动,但没有标题。我担心还是需要设置其他东西?

这是代码:

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];

感谢。

3 个答案:

答案 0 :(得分:1)

@leo,看起来您可能只需要在将ASButtonNode添加到视图之前测量它。

[button measure:...];

答案 1 :(得分:0)

请在此处Add custom Button with AsyncDisplayKit

参考此主题

Basiclly,ASTextNode将用作按钮,也可以在func示例目标下设置标题,颜色,字体粗细和颜色(在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
}

答案 2 :(得分:0)

您可以使用此代码

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())