【问题标题】:Drawing colored text in UIView's -drawRect: method在 UIView -drawRect: 方法中绘制彩色文本
【发布时间】:2012-12-03 08:53:27
【问题描述】:

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

文本已绘制,但无论我将颜色设置为什么,它始终是黑色的。

- (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] set],但无济于事。

答案:

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

【问题讨论】:

    标签: ios uikit drawrect uicolor nsattributedstring


    【解决方案1】:

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

    【讨论】:

    • 谢谢。我也应该一直在使用:NSFontAttributeName
    • 没错。在这里你可以找到属性字典中所有要使用的键的list
    【解决方案2】:

    您可以尝试以下方法。它将通过以下属性帮助您在右下角的 UIView 中绘制文本。

    • NSFontAttributeName - 带大小的字体名称
    • NSStrokeWidthAttributeName - 描边宽度
    • NSStrokeColorAttributeName - 文本颜色

    Objective-C - 在 UIView 中绘制文本并作为 UIImage 返回。

        -(UIImage *) imageWithView:(UIView *)view text:(NSString *)text {
    
            UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
    
            [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    
            // Setup the font specific variables
            NSDictionary *attributes = @{
                    NSFontAttributeName   : [UIFont fontWithName:@"Helvetica" size:12],
                    NSStrokeWidthAttributeName    : @(0), 
                    NSStrokeColorAttributeName    : [UIColor blackColor]
            };
            // Draw text with CGPoint and attributes
            [text drawAtPoint:CGPointMake(view.frame.origin.x+10 , view.frame.size.height-25) withAttributes:attributes];
    
            UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
    
            return img;
        }`
    

    Swift - 在 UIView 中绘制文本并作为 UIImage 返回。

        func imageWithView(view : UIView, text : NSString) -> UIImage {
    
            UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
            view.layer.renderInContext(UIGraphicsGetCurrentContext()!);
            // Setup the font specific variables
            let attributes :[String:AnyObject] = [
                NSFontAttributeName : UIFont(name: "Helvetica", size: 12)!,
                NSStrokeWidthAttributeName : 0,
                NSForegroundColorAttributeName : UIColor.blackColor()
            ]
            // Draw text with CGPoint and attributes
            text.drawAtPoint(CGPointMake(view.frame.origin.x+10, view.frame.size.height-25), withAttributes: attributes);
            let img:UIImage = UIGraphicsGetImageFromCurrentImageContext();
    
            UIGraphicsEndImageContext();
            return img;
        }`
    

    【讨论】:

      猜你喜欢
      • 2011-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-03
      相关资源
      最近更新 更多