Draw text and add to a UIImage iOS 5/6

前端 未结 3 825
遇见更好的自我
遇见更好的自我 2020-12-16 05:57

I have a textbox, where i want the written text to be added to a UIImage.

How can i draw NSString to a UIImage?

I´ve searched, and found lots of examples, bu

3条回答
  •  悲哀的现实
    2020-12-16 06:46

    i think solution is here..i have used this method in one of my application here lbltext is the object of my label. this method creates new image with given text & return the object of new image with text.

    -(UIImage *) drawText:(NSString*) text inImage:(UIImage*)image atPoint:(CGPoint)point 
    {
        UIFont *font = lblText.font;
        UIGraphicsBeginImageContext(iv.frame.size);
        [image drawInRect:CGRectMake(0,0,iv.frame.size.width,iv.frame.size.height)];
        CGRect rect = CGRectMake(point.x,point.y,lblText.frame.size.width, lblText.frame.size.height);
        [lblText.textColor set];
        [text drawInRect:CGRectIntegral(rect) withFont:font]; 
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
    }
    

    in your below comment you have passed iv.center as a point.. try manualy like this

    CGPoint point;
    point.x=30; // dynamicaly just for example lblText.frame.origin.x;
    point.y=40; //lblText.frame.origin.y;
    

    you have to call above method like this...

    UIImage *img = [self drawText:@"Test String" inImage:originalImage atPoint:point];
    

    here,img is another instace of UIImage for storing new image with text...after calling this method use img imager for your further use..

    for example...

    [newimageviewObj setImage:img];
    

    i hope this will help you

提交回复
热议问题