I am using following function for converting an NSString to an image.
-(UIImage *)imageFromText:(NSString *)text FontName:(UIFont *)font
{
// set the font type and size
//UIFont *font = [UIFont systemFontOfSize:20.0];
CGSize size = [text sizeWithFont:font];
UIGraphicsBeginImageContext(size);
[text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font];
// transfer image
CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
It works well. The problem is that when the string contains long text, then it creates an image for which the width is too much. I want to apply a word wrap functionality if the text beyond the range.
So how can I create a image with word wrap of NSString?
You need to call drawInRect:withAttributes: method on NSString.
Related
In my Apple Watch app, I'm trying to place text over the top of an image. I've got a group with a background image and label positioned correctly. But depending on the image, my white text can sometimes be unreadable. Dark text would have the same problem or be even worse.
Is there a way to have black text just under my white text and slightly offset?
Unfortunately, the current version of WatchKit doesn't provide a way to "stack" or layer WKInterfaceLabel controls. So, the idea of including black text under the white text using a second label won't work.
You might consider rendering your drop-shadowed text as a UIImage in your Watch extension and using the image with a WKInterfaceImage control. Something like:
- (UIImage *)imageWithText:(NSString *)text shadowBlurRadius:(CGFloat)shadowBlurRadius {
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowOffset = CGSizeZero;
shadow.shadowBlurRadius = shadowBlurRadius;
shadow.shadowColor = [UIColor blackColor];
NSDictionary *attributes = #{ NSForegroundColorAttributeName : [UIColor whiteColor],
NSFontAttributeName : [UIFont boldSystemFontOfSize:36.0f],
NSShadowAttributeName : shadow };
CGSize size = [text sizeWithAttributes:attributes];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width + (2.0f * shadowBlurRadius),
size.height + (2.0f * shadowBlurRadius)),
NO,
2.0f);
[text drawAtPoint:CGPointMake(shadowBlurRadius, shadowBlurRadius)
withAttributes:attributes];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
I am adding text to image using the code mentioned below. But the problem am facing is that the color and font size not changing. I am trying to change the font size, not happening and don't know how to change color.
-(UIImage *)imageFromText:(NSString *)text
{
// set the font type and size
//UIFont *font = [UIFont systemFontOfSize:txtView.font];
//CGSize size = [text sizeWithFont:[UIFont fontWithName:#"Arial" size:13.0f]]; // label or textview
NSDictionary *attributes = #{
NSFontAttributeName: [UIFont fontWithName:#"Arial" size:14]
};
CGSize size = [text sizeWithAttributes:#{
NSFontAttributeName: [UIFont fontWithName:#"Helvetica" size:14]
}];
// check if UIGraphicsBeginImageContextWithOptions is available (iOS is 4.0+)
if (UIGraphicsBeginImageContextWithOptions != NULL)
UIGraphicsBeginImageContextWithOptions(size,NO,0.0);
else
// iOS is < 4.0
UIGraphicsBeginImageContext(size);
//[text drawInRect:CGRectMake(0,0,size.width,size.height) withFont:[UIFont fontWithName:#"Arial" size:15.0f]];
CGRect drawRect = CGRectMake(0.0, 0.0, size.width,size.height);
NSStringDrawingContext *drawingContext = [[NSStringDrawingContext alloc] init];
drawingContext.minimumScaleFactor = 1.5;
[text drawWithRect:drawRect options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:drawingContext];
UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return testImg;
}
Please guide, why font size not changing, what is wrong in the code nad how to change color of this text also.
Finally i got myself.
[theText drawAtPoint:CGPointMake(x, y)
withAttributes:#{NSFontAttributeName:[UIFont fontWithName:#"Helvetica" size:8], NSForegroundColorAttributeName:[UIColor whiteColor] }];
Is there a way to create an UIImage out of a UITableView?
I know about this piece of code that will draw a UIImage out of a given UIView:
-(UIImage*) makeImageOutOfView:(UIView*)view {
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
}
but it would only create an image with the size of the table's frame. I would like to create an image that will display the whole table's content. Another problem is that not only that the created image was limited to the table's frame, but when creating the image after scrolling the table had very weird outcomes (only the visible cells out of the first 6 where shown in the image and that's all, the other visible cells were not drawn..)
EDIT - i want to create an image that was drawn out of the content of a tableView, not setting the tableView's background to display an image..
i just Create a DEMO for you and hope its helps you you can capture table image like this way:-
-(IBAction)savebutn:(id)sender
{
[tbl reloadData];
CGRect frame = tbl.frame;
frame.size.height = tbl.contentSize.height;
tbl.frame = frame;
UIGraphicsBeginImageContext(tbl.bounds.size);
[tbl.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *saveImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(saveImage);
NSFileManager *fileMan = [NSFileManager defaultManager];
NSString *fileName = [NSString stringWithFormat:#"%d.png",1];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
[fileMan createFileAtPath:pdfFileName contents:imageData attributes:nil];
}
This is a Screen Shot of capture image:-
capture image contain full of table cell top to bottom please download the demo of it:-
http://www.sendspace.com/file/w48sod
One thing you can do is, create an UIView, add all cells one below another you want in the image and finally convert that UIView into an image using the code you have.
I think that should work.
- (UIImage *)imageWithTableView:(UITableView *)tableView{
UIImage* image = nil;
UIGraphicsBeginImageContextWithOptions(tableView.contentSize, NO, 0.0);
CGPoint savedContentOffset = tableView.contentOffset;
CGRect savedFrame = tableView.frame;
tableView.contentOffset = CGPointZero;
tableView.frame = CGRectMake(0, 0, tableView.contentSize.width, tableView.contentSize.height);
[tableView.layer renderInContext: UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
tableView.contentOffset = savedContentOffset;
tableView.frame = savedFrame;
UIGraphicsEndImageContext();
return image;
}
I want to apply shadow effect to text of type NSString.
Though I understand how to apply shadow effect to UILabel and other view elements,
I can't figure out a way of adding shadow effect to text.
I am currently drawing text as follows:
NSString *text = #"Hello";
[text drawAtPoint:point width withFont:font minFontSize:22.0f actualFontSize:&actualFontSize lineBreakMode:UILineBreakModeTailTruncation baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
I would really appreciate any help. Thanks!
Try CGContextSetShadow() for adding shadow to text
- (void)drawRect:(CGRect)rect
{
NSString *string = #"Hello World!";
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetShadow(context, CGSizeMake(20.0f, 20.0f), 10.0f);
[string drawAtPoint:CGPointMake(100.0f, 100.0f) withFont:[UIFont boldSystemFontOfSize:36.0f]];
}
I want to make an iphone application similar to some greeting cards application, where I could write text over some pre prepared background images(cards).
How can I write this text?
How to save the background image+the text on one image file ?
Thanks.
Here is a method that burns a string into an image. You can tweak the font size and other parameters to configure it to your liking.
/* Creates an image with a home-grown graphics context, burns the supplied string into it. */
- (UIImage *)burnTextIntoImage:(NSString *)text :(UIImage *)img {
UIGraphicsBeginImageContext(img.size);
CGRect aRectangle = CGRectMake(0,0, img.size.width, img.size.height);
[img drawInRect:aRectangle];
[[UIColor redColor] set]; // set text color
NSInteger fontSize = 14;
if ( [text length] > 200 ) {
fontSize = 10;
}
UIFont *font = [UIFont boldSystemFontOfSize: fontSize]; // set text font
[ text drawInRect : aRectangle // render the text
withFont : font
lineBreakMode : UILineBreakModeTailTruncation // clip overflow from end of last line
alignment : UITextAlignmentCenter ];
UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext(); // extract the image
UIGraphicsEndImageContext(); // clean up the context.
return theImage;
}
Thanks Rayfleck! It worked.
To add optional compatibility with retina displays (fixes choppy letters during '#2x' version of image scale up):
replace:
UIGraphicsBeginImageContext(img.size);
with conditional:
if (UIGraphicsBeginImageContextWithOptions != NULL)
UIGraphicsBeginImageContextWithOptions(img.size,NO,0.0);
else
UIGraphicsBeginImageContext(img.size);
Update for ios7...
/* Creates an image with a home-grown graphics context, burns the supplied string into it. */
- (UIImage *)burnTextIntoImage:(NSString *)text :(UIImage *)img {
UIGraphicsBeginImageContext(img.size);
CGRect aRectangle = CGRectMake(0,0, img.size.width, img.size.height);
[img drawInRect:aRectangle];
[[UIColor redColor] set]; // set text color
NSInteger fontSize = 14;
if ( [text length] > 200 ) {
fontSize = 10;
}
UIFont *font = [UIFont fontWithName:#"Courier" size:fontSize];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
paragraphStyle.alignment = NSTextAlignmentRight;
NSDictionary *attributes = #{ NSFontAttributeName: font,
NSParagraphStyleAttributeName: paragraphStyle,
NSForegroundColorAttributeName: [UIColor whiteColor]};
[text drawInRect:aRectangle withAttributes:attributes];
UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext(); // extract the image
UIGraphicsEndImageContext(); // clean up the context.
return theImage;
}
This approach takes in account the screen's scale and it's super intuitive
UIImage * img = ...
UIImageView * iV = [[UIImageView alloc] initWithImage:img];
UILabel * l = [[UILabel alloc] initWithFrame:iV.bounds];
l.textAlignment = ...;
l.adjustsFontSizeToFitWidth = YES;
l.textColor = ...;
l.font = ...;
l.text = ...;
[iV addSubview:l];
UIGraphicsBeginImageContextWithOptions(iV.bounds.size, NO, 0);
[iV.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return finalImage