1

我使用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];

谢谢。

4

3 回答 3

1

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

[button measure:...];

于 2016-04-18T17:35:28.430 回答
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())
于 2017-10-13T08:07:51.553 回答
0

请在此处参考此主题使用 AsyncDisplayKit 添加自定义按钮

基本上,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
}
于 2016-04-14T07:51:10.360 回答