2013-12-20 39 views
0

我想色My字符串,並以矩形繪製它,我的代碼是IOS 7的NSString drawInRect使用CoreGraphics中

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

但是相應的字符串是沒有得到所期望的顏色[其爲紅色。是否缺少任何屬性?

回答

1

你在這裏工作在兩個不同的層次。如果您打算使用CGContext函數繪製文本,那麼設置CGContext 的填充和描邊顏色可能是

但是,由於您使用NSAttributedString繪製文本,因此需要將文本的填充和筆觸顏色設置爲屬性。

您正在尋找的屬性是NSForegroundColorAttributeName(文字填充顏色)和NSStrokeColorAttributeName


您也不需要設置文本繪圖模式。這也僅在您使用CGContext函數繪製文本時纔有意義。

爲了得到你的NSAttributedString,你需要同時設置NSStrokeColorAttributeNameNSStrokeWidthAttributeName

相關問題