2013-12-20 3 views
0
내 문자열 색상과 구형 영역에 그릴 싶습니다

, 내 코드는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을 모두 설정해야합니다.

관련 문제