如何使用drawInRect时设置文本颜色



我一直在使用PaintCode在我的应用程序中绘制一些图形,一些绘制的元素具有使用"drawInRect"绘制的文本。无论我如何尝试设置颜色,文本始终显示为黑色。

这是创建文本的代码,我正在尝试设置颜色:

       //// Rounded Rectangle Drawing
    UIBezierPath* roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(0, 0, 66, 66) cornerRadius: 15];
    [[NPSColor NPSPrimaryColor] setFill];
    [roundedRectanglePath fill];
    UIFont *helveticaFont = [UIFont fontWithName:@"AvenirNextCondensed-Bold" size:50];
    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    /// Set line break mode
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    /// Set text alignment
    paragraphStyle.alignment = NSTextAlignmentCenter;
    NSDictionary *attributes = @{NSFontAttributeName:helveticaFont,
                                 NSParagraphStyleAttributeName:paragraphStyle};

    //// Text Drawing
    CGRect textRect = CGRectMake(-4, -2.5, 74, 71.5);
    [[NPSColor whiteColor] set];
    [textContent drawInRect: CGRectInset(textRect, 0, 6) withAttributes:attributes];
}

"文本绘图"中的白色是无法正常工作的。感谢您的帮助。

您需要通过以下方式在" attributes"字典中设置文本的颜色:

[textContent drawInRect: CGRectInset(textRect, 0, 6) withAttributes:attributes];

我的建议是:

NSDictionary *attributes = @{ NSFontAttributeName:helveticaFont,
                              NSParagraphStyleAttributeName:paragraphStyle,
                              NSForegroundColorAttributeName: [NPSColor whiteColor]}

相关内容

  • 没有找到相关文章

最新更新