0

我想给我的字符串上色并将其绘制成一个矩形,我的代码是

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];

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

4

1 回答 1

1

你在这里工作在两个不同的层面。如果您打算使用 CGContext 函数来绘制文本,那么设置 CGContext 的填充和描边颜色可能是合适的。

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

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


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

要让你的 NSAttributedString 被抚摸,你需要同时设置NSStrokeColorAttributeNameNSStrokeWidthAttributeName

于 2013-12-20T21:56:14.793 回答