繁体   English   中英

AsyncDisplayKit ASButtonNode setTitle不起作用

[英]AsyncDisplayKit ASButtonNode setTitle not work

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

谢谢。

@leo,看来您可能只需要测量ASButtonNode才能将其添加到视图中。

[button measure:...];

请在此参考本主题。 使用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
}

您可以使用此代码

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM