在UIView的-drawRect:方法中绘制彩色文本 [英] Drawing colored text in UIView's -drawRect: method

查看:118
本文介绍了在UIView的-drawRect:方法中绘制彩色文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 UIView 子类中绘制彩色文本。现在我正在使用Single View应用程序模板(用于测试)。除了 drawRect:方法之外没有任何修改。

I am trying to draw colored text in my UIView subclass. Right now I am using the Single View app template (for testing). There are no modifications except the drawRect: method.

文本被绘制但无论什么是黑色都是黑色的我将颜色设置为。

The text is drawn but it is always black no matter what I set the color to.

- (void)drawRect:(CGRect)rect
{
    UIFont* font = [UIFont fontWithName:@"Arial" size:72];
    UIColor* textColor = [UIColor redColor];
    NSDictionary* stringAttrs = @{ UITextAttributeFont : font, UITextAttributeTextColor : textColor };

    NSAttributedString* attrStr = [[NSAttributedString alloc] initWithString:@"Hello" attributes:stringAttrs];

    [attrStr drawAtPoint:CGPointMake(10.f, 10.f)];
}

我也试过 [[UIColor redColor]设置] 无济于事。

答案:

NSDictionary * stringAttrs = @ {NSFontAttributeName:font,
NSForegroundColorAttributeName:textColor};

NSDictionary* stringAttrs = @{ NSFontAttributeName : font, NSForegroundColorAttributeName : textColor };


推荐答案

您应该使用 NSForegroundColorAttributeName 而不是 UITextAttributeTextColor 。希望这有帮助!

Instead of UITextAttributeTextColor you should use NSForegroundColorAttributeName. Hope this helps!

这篇关于在UIView的-drawRect:方法中绘制彩色文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆