2015-09-02 25 views
1

我試圖在上下文上繪製文本,並且它可以工作。無法爲drawInRect設置字體

我想設置一個UIfont到這個文本,但它不起作用。文字大小保持不變。

這裏我的代碼

- (UIImage *)imageByDrawingCircleOnImage:(UIImage *)image 
{ 

UIGraphicsBeginImageContextWithOptions((image.size), NO, [[UIScreen  mainScreen]scale]); 

[image drawAtPoint:CGPointZero]; 
CGContextRef context = UIGraphicsGetCurrentContext(); 

UIFont *font = [UIFont systemFontOfSize:30];;//[UIFont fontWithName: @"Helvetica" size:45.0]; 

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

NSMutableParagraphStyle *textStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; 
textStyle.lineBreakMode = NSLineBreakByWordWrapping; 
textStyle.alignment = NSTextAlignmentRight; 

NSMutableDictionary *attributes = [NSMutableDictionary dictionary]; 
[attributes setObject:textStyle forKey:@"NSParagraphStyleAttributeName"]; 
[attributes setObject:font forKey:@"NSFontAttributeName"]; 

NSString *string = @"Tour de poitrine : 95 cm"; 

CGSize string_len =[string sizeWithAttributes:attributes]; 

[string drawInRect:CGRectMake(245 - string_len.width, 100.0, string_len.width , 100) withAttributes:[NSDictionary dictionaryWithDictionary:attributes]]; 



UIImage *retImage = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 
return retImage; 
} 

感謝 亞歷山大

回答

1

不要使用字符串爲您的屬性字典鍵,使用該框架提供的常量。

@"NSFontAttributeName"替換爲NSFontAttributeName@"NSParagraphStyleAttributeName"通過NSParagraphStyleAttributeName

+0

這就是它!非常感謝。 –

0

試試這個:

//// General Declarations 
CGContextRef context = UIGraphicsGetCurrentContext(); 

//// Text Drawing 
CGRect textRect = CGRectMake(CGRectGetMinX(frame) + 23, CGRectGetMinY(frame) + 28, 101, 27); 
{ 
    NSString* textContent = @"Hello, World!"; 
    NSMutableParagraphStyle* textStyle = NSMutableParagraphStyle.defaultParagraphStyle.mutableCopy; 
    textStyle.alignment = NSTextAlignmentLeft; 

    NSDictionary* textFontAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize: UIFont.labelFontSize], NSForegroundColorAttributeName: UIColor.blackColor, NSParagraphStyleAttributeName: textStyle}; 

    CGFloat textTextHeight = [textContent boundingRectWithSize: CGSizeMake(textRect.size.width, INFINITY) options: NSStringDrawingUsesLineFragmentOrigin attributes: textFontAttributes context: nil].size.height; 
    CGContextSaveGState(context); 
    CGContextClipToRect(context, textRect); 
    [textContent drawInRect: CGRectMake(CGRectGetMinX(textRect), CGRectGetMinY(textRect) + (CGRectGetHeight(textRect) - textTextHeight)/2, CGRectGetWidth(textRect), textTextHeight) withAttributes: textFontAttributes]; 
    CGContextRestoreGState(context); 
} 
1

用此替換您的代碼。它爲我工作。

- (UIImage *)imageByDrawingCircleOnImage:(UIImage *)image 

{

UIGraphicsBeginImageContextWithOptions((image.size), NO, [[UIScreen  mainScreen]scale]); 

[image drawAtPoint:CGPointZero]; 
CGContextRef context = UIGraphicsGetCurrentContext(); 

UIFont *font = [UIFont fontWithName: @"Chalkduster" size:20.0]; 

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

NSMutableParagraphStyle *textStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; 
textStyle.lineBreakMode = NSLineBreakByWordWrapping; 
textStyle.alignment = NSTextAlignmentRight; 


NSString *string = @"Tour de poitrine : 95 cm"; 

NSDictionary* attributes = @{NSFontAttributeName: font, 
          NSForegroundColorAttributeName: [UIColor redColor], 
          NSParagraphStyleAttributeName: textStyle 
          }; 


CGSize string_len =[string sizeWithAttributes:attributes]; 

[string drawInRect:CGRectMake(245 - string_len.width, 100.0, string_len.width , 100) withAttributes:[NSDictionary dictionaryWithDictionary:attributes]]; 



UIImage *retImage = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 
return retImage; 
}