Ios7 ios 7 NSString drawInRect使用CoreGraphics

Ios7 ios 7 NSString drawInRect使用CoreGraphics,ios7,nsstring,core-graphics,cgcontext,cgcontextref,Ios7,Nsstring,Core Graphics,Cgcontext,Cgcontextref,我想给我的字符串上色,并把它画成一个矩形,我的代码是 CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetTextDrawingMode(context, kCGTextFillStroke); CGContextSetRGBStrokeColor(context, 210.0/255.0f, 0.0f, 0.0f, 1.0f); CGContextSetRGBFillColor(context, 192.0/25

我想给我的字符串上色,并把它画成一个矩形,我的代码是

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetTextDrawingMode(context, kCGTextFillStroke);
CGContextSetRGBStrokeColor(context,  210.0/255.0f, 0.0f, 0.0f, 1.0f);
CGContextSetRGBFillColor(context, 192.0/255.0, 0.0/255.0 , 13.0/255.0, 1.0);    

UIFont *font = [UIFont @"MyriadPro-Regular" size:20.0];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
/// Set line break mode
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
/// Set text alignment
paragraphStyle.alignment = NSTextAlignmentCenter;
NSDictionary *attributes = @{NSFontAttributeName:font,
                                 NSParagraphStyleAttributeName:paragraphStyle};

NSString *string = @"My code";

[string drawInRect:(isPad)?CGRectMake(1.0, 400.0, 270.0, 120.0):CGRectMake(1.0, 150.0, 125, 120) withAttributes:attributes];

但是相应的字符串没有得到所需的颜色[红色]。是否缺少任何属性?

您在两个不同的级别上工作。如果要使用CGContext函数绘制文本,则设置CGContext的填充和笔划颜色可能是合适的

但是,由于使用NSAttributedString绘制文本,因此需要将文本的填充颜色和笔划颜色设置为属性

您要查找的属性是
NSForegroundColorAttributeName
(文本填充颜色)和
NSStrokeColorAttributeName


您也不需要设置文本绘制模式。这也只有在使用CGContext函数绘制文本时才相关

要对NSAttributeString进行笔划,需要同时设置
NSStrokeColorAttributeName
NSStrokeWidthAttributeName