2013-10-25 29 views
1

我正在圖像上繪製文本。文本沒有任何問題,但我的文本是動態的,有時更多的字符或有時更少。帶文本的圖像上的DrawText調整爲圖像的大小

從下面的圖像可以看到「這是超長短信超過320寬度圖像的多,圖像那不來的。

我要顯示在未來的所有消息行,因爲我的信息是動態的,我想在320 * 480大小的圖片來顯示它,我在這裏只是用於測試目的顯示小圖像

這是代碼繪製文字:

-(UIImage *)addText:(UIImage *)img text:(NSString *)text1{ 

    int w = img.size.width; 
    int h = img.size.height; 
    //lon = h - lon; 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst); 
     CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage); 
    CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1); 

    char* text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding]; 

    CGContextSelectFont (context, // 3 
         "Helvetica-Bold", 
         15, 
         kCGEncodingMacRoman); 
    CGContextSetTextDrawingMode(context, kCGTextFill); 
    CGContextSetRGBFillColor(context, 255, 255, 255, 1); 
    CGContextShowTextAtPoint(context, 4, 52, text, strlen(text)); 
    CGImageRef imageMasked = CGBitmapContextCreateImage(context); 
    CGContextRelease(context); 
    CGColorSpaceRelease(colorSpace); 
    return [UIImage imageWithCGImage:imageMasked]; 
} 

enter image description here

+0

是文字必須的嗎?如果沒有,使用UILabel是一種更簡單的方法 – Jing

+0

實際上我並不希望文本顯示在圖像上,我的消息是動態的,我應該將該消息放在圖像上。 – krish

+0

使用標籤有可能嗎? – krish

回答

2

使用UIImageView顯示圖像並添加UILabel作爲UIImageView的子視圖。

1.configure您的UILabel第一:

[yourLabel setNumberOfLines:0]; 
[yourLabel setLineBreakMode:NSLineBreakByWordWrapping]; 

2.註冊與內容長度

- (CGRect)labelFrameWithText:(NSString *)text 
{ 
    CGRect rect; 

    // the font of your text 
    UIFont *font = [UIFont systemFontOfSize:15.0]; 
    NSDictionary *attributes = @{NSFontAttributeName: font}; 

    // the first parametric CGSize is the max size that the rect's size can be 
    CGRect boundingRect = [title boundingRectWithSize:CGSizeMake(youImageWidth, 100.0) 
               options:NSStringDrawingUsesLineFragmentOrigin 
              attributes:attributes 
               context:nil]; 

    //the rect of the UILabel 
    //This method returns fractional sizes (in the size component of the returned CGRect); to use a returned size to size views, you must use raise its value to the nearest higher integer using the ceil function. 
    rect = CGRectMake(yourLabelOriginX, 
         yourLabelOriginY, 
         ceil(boundingRect.size.width), 
         ceil(boundingRect.size.height)); 

    return rect; 
} 

3.change您的標籤的框架和的setText

您的標籤框架--- ----舊版本---------

CGSize contentSize = [content sizeWithFont:font 
          constrainedToSize:CGSizeMake(maxWidth, maxHeight) 
           lineBreakMode: NSLineBreakByWordWrapping]; 
+0

順便說一下,這些方法在iOS7.0及更高版本中可用-boundingRectWithSize:options:attributes:context: – Jing

+0

如果您的應用需要覆蓋較早版本,請告訴我,以便我可以提供舊版本代碼 – Jing

+0

我的應用需要工作舊版本也是如此。請給我提供 – krish

5

試試這個代碼:

lblText.text=[NSString stringWithFormat:@"Put Your Text"]; 
lblText.font = [UIFont fontWithName:@"Helvetica" size:10.0f]; 
CGPoint point=lblText.center; 
imgview.image =[self drawText:lblText.text inImage:imgview.image atPoint:point]; 


    //Save it in Document directory for Checking Purpose... 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"]; 
    UIImage *image1 = imgview.image; // imageView is my image from camera 
    NSData *imageData = UIImagePNGRepresentation(image1); 
    [imageData writeToFile:savedImagePath atomically:NO]; 


-(UIImage *) drawText:(NSString*) text inImage:(UIImage*)image atPoint:(CGPoint)point 
    { 
    UIFont *font =lblText.font;;  
    if ([UIScreen instancesRespondToSelector:@selector(scale)]) { 
      UIGraphicsBeginImageContextWithOptions(imgview.frame.size, NO, 0.0f); 
     } else { 
     UIGraphicsBeginImageContext(imgview.frame.size); 
     } 

     [image drawInRect:CGRectMake(0,0,imgview.frame.size.width,imgview.frame.size.height)]; 
     CGRect rect = CGRectMake(point.x,point.y,lblText.frame.size.width, lblText.frame.size.height);//Set Frame as per your Requirement 
     [lblText.textColor set]; 
     [text drawInRect:CGRectIntegral(rect) withFont:font]; 
     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 

    return newImage; 
} 

它將幫助。

+0

這是什麼條件? ([UIScreen instancesRespondToSelector:@selector(scale)]) – krish

+0

如果scale正在通過它,這裏是scale = 0.0f。 檢查它 - > UIGraphicsBeginImageContextWithOptions(CGSize size,BOOL opaque,CGFloat scale) –

+0

@krish UIScreen.scale在iOS4中引入。 'if'測試系統是否可以自動選擇刻度分辨率,如果是true,我們簡單地設置刻度= 0.對於較老的系統,UIGraphicsBeginImageContext()將刻度設置爲默認值= 1.0。 雖然,我想簡單地使用'UIGraphicsBeginImageContextWithOptions(imgview.frame.size,NO,0.0f);'應該就足夠了,因爲它應該在默認情況下設置scale = 1.0。 – chunkyguy