【问题标题】:How to set text color when using drawInRect?使用drawInRect时如何设置文本颜色?
【发布时间】:2014-03-15 06:57:29
【问题描述】:

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

}

“文本绘图”中的白色表示无法正常工作。感谢您的帮助。

【问题讨论】:

  • 更新您的属性以包含颜色。
  • 什么是“NPSColor”?如果您在绘制文本之前尝试“[[UIColor redColor] set];”之类的内容会发生什么?它是红色的吗?
  • NPSColor 是 UIColor 的自定义类别 ...

标签: ios cgrect drawinrect paintcode


【解决方案1】:

你需要在你传入的“attributes”字典中设置文字的颜色:

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

我的建议是:

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

【讨论】:

  • 是的,就是这样,我很难找到正确的“NSAttribute”我尝试了颜色、文本颜色、字体颜色......我从未尝试过“NSFore......”谢谢!
  • 是否可以在 swift 中使用 draw in rect 将渐变颜色应用于具有属性的文本?如果可能的话,你也可以检查问题:stackoverflow.com/questions/67807362/…
猜你喜欢
  • 2012-06-12
  • 2021-01-13
  • 2015-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多