【问题标题】:ios 7 NSString drawInRect using CoreGraphicsios 7 NSString drawInRect 使用 CoreGraphics
【发布时间】:2013-12-20 06:09:07
【问题描述】:

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

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

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

【问题讨论】:

    标签: ios7 nsstring core-graphics cgcontext cgcontextref


    【解决方案1】:

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

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

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


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

    要让您的 NSAttributedString 被描边,您需要同时设置 NSStrokeColorAttributeNameNSStrokeWidthAttributeName

    【讨论】:

      猜你喜欢
      • 2014-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-17
      • 2012-05-22
      • 2014-08-09
      • 1970-01-01
      • 2016-10-15
      相关资源
      最近更新 更多