AttributedString sometimes not successfully drawn using drawInRect - ios

I am having a UILabel with attributed string that user edits and eventually draws on a image. Here's how I update the UILabel every time user changes text in a UITextview and confirm.
- (void)displayPicture {
[self.view endEditing:YES];
self.attributedStr = [[NSAttributedString alloc] initWithString:self.textInputField.text attributes:self.attributes];
self.displayField.attributedText = [[NSAttributedString alloc] initWithString:self.textInputField.text attributes:self.attributes];
self.displayField.lineBreakMode = NSLineBreakByCharWrapping;
[self.displayField sizeToFit];
[self.displayField setNeedsDisplay];
self.textInputField.hidden = YES;
self.tapRecog.enabled = YES;
self.displayField.hidden = NO;
}
For now the UILabel displays the way desired on the screen.
When user wants the Pic+Text combined to an UIImage and posted, the following code is implemented:
- (UIImage *)generatePicture {
UIGraphicsBeginImageContextWithOptions(CGSizeMake(Picture_Standard_Width, Picutre_Standard_Height), YES, 0.0f);
[self.bgImageView.image drawInRect:CGRectMake(0, 0, Picture_Standard_Width, Picutre_Standard_Height)];
CGRect originalFrame = self.displayField.frame;
CGRect adjustedFrame = CGRectMake(originalFrame.origin.x, originalFrame.origin.y - self.bgImageView.frame.origin.y, originalFrame.size.width, originalFrame.size.height);
[self.displayField.attributedText drawInRect:adjustedFrame];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
self.imageToUpload = newImage;
UIGraphicsEndImageContext();
return self.imageToUpload;
}
Most of the times the attributed only shows one line or two in the generated image. It may very likely have something to do with the frame. I am simply confused by the fact that the attributed string displays nicely in the label and differently when I draw it.
Any help with having the attributedString drawn correctly will be greatly appreciated.

when you start a picture, you start a new context in which you draw.
the Context is the image and 0,0 is the images corner.
so you have to adjust your originalFrame to take into account that you're now no longer drawing into view's context but into the image's context

Related

Is it possible to display multiple info windows for multiple markers without tapping it?

I want to display multiple info window for multiple markers in google map. The info window should display without tapping the marker itself. Is it possible? After researching, I learned that setting the marker as mapview selected marker can make the info window appear without tapping it. However, multiple markers cannot be selected as the selected marker of the mapview at a time. Is there anything that can be done?
Here is the code to create custom markers as shown in the above image:
Create a subclass of UIView and add the below method to the class.
-(UIImage*)createCustomMarkerImageWithMarker:(GMSMarker *)marker
{
CGRect priceLabelRect = [marker.title boundingRectWithSize:CGSizeMake(500, 50)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:#{NSFontAttributeName:[UIFont systemFontOfSize:10]}
context:nil];
UILabel *priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, priceLabelRect.size.width+25, priceLabelRect.size.height+12)];
priceLabel.text = [NSString stringWithFormat:#" ₹ %# ",marker.title];
priceLabel.textAlignment = NSTextAlignmentCenter;
priceLabel.textColor = [UIColor blackColor];
priceLabel.backgroundColor = [UIColor clearColor];
priceLabel.font = [UIFont systemFontOfSize:11];
CGRect numberOfPropertiesLabelRect = [marker.snippet boundingRectWithSize:CGSizeMake(300, 50)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:#{NSFontAttributeName:[UIFont systemFontOfSize:10]}
context:nil];
UILabel *numberOfPropertiesLabel = [[UILabel alloc]initWithFrame:CGRectMake(priceLabel.frame.size.width, 0, numberOfPropertiesLabelRect.size.width+10, numberOfPropertiesLabelRect.size.height+12)];
numberOfPropertiesLabel.text = marker.snippet;
numberOfPropertiesLabel.textAlignment = NSTextAlignmentCenter;
numberOfPropertiesLabel.textColor = [UIColor whiteColor];
numberOfPropertiesLabel.backgroundColor = [UIColor clearColor];
numberOfPropertiesLabel.font = [UIFont systemFontOfSize:11];
self.frame = CGRectMake(0, 0, priceLabel.frame.size.width+numberOfPropertiesLabel.frame.size.width, priceLabel.frame.size.height+TriangleHeight);
[self addSubview:priceLabel];
[self addSubview:numberOfPropertiesLabel];
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [[UIScreen mainScreen] scale]);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * icon = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return icon;
}
In the above code, 2 labels are creates priceLabel and numberOfPropertiesLabel. The frame of both the labels are set according to your requirement i.e. the position of the labels in the view. Then the frame of the view is set according to the labels' dimensions.
View is then converted into an image. This image is then set as the GMSMarker image.
You cannot select multiple markers at a time.
You can use an alternative approach instead.
You can create custom markers i.e., the custom marker image can be created such that it contains the information/format you want to display in the info window.
The below image can give you an idea on how to achieve it:

iOS: ImageView filled with a picture

I have a table that contains the path to the picture, saved on user's library. Then, I pass this path to a View that contains just a imageView. I want to fills this ImageView with the picture. Like WhatsApp (when you click on a profile's picture). But, my picture it's always cropped or distorted. I tryed different ways to do this, but I didn't find the best way:
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.translucent = NO;
UIImage *picture = [UIImage imageWithContentsOfFile: self.picturePath]; //I passed this path from the previous View
UIImageView* imageView = [[UIImageView alloc] initWithImage:picture];
CGRect screenRect = [[UIScreen mainScreen] bounds];
imageView.frame = CGRectMake(0, 0, screenRect.size.width, screenRect.size.height);
imageView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:imageView];
}
Original (I want something like it)
My app
If your image is cut , the reason is you are using a higher resolution image in a smaller container and instructed your imageview to fill , using this line imageView.contentMode = UIViewContentModeScaleAspectFill;
Change it to imageView.contentMode = UIViewContentModeScaleAspectFit;
I think #whitewolf09 is right and his method will solve your problem. But i suggest you to look into MGImageUtilities that will be very useful for different cases where you can crop images maintaining aspect ratio to fit inside your image view's frame.
Just #import "UIImage+ProportionalFill.h"
UIImage *image = [[UIImage imageWithContentsOfFile:filePath] imageScaledToFitSize:imageView.frame.size];
That's the very useful category for image resizing and may help you in future.
I think the reason it is being clipped is that you are creating the image view with a picture THEN setting the frame and probably the picture is bigger than the frame so it is clipping it. Also try changing the aspect to: 'UIViewContentModeScaleAspectFit' Try this:
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.translucent = NO;
UIImage *picture = [UIImage imageWithContentsOfFile:self.picturePath]; //I passed this path from the previous View
UIImageView* imageView = [[UIImageView alloc] initWithRect:CGRectMake(0, 0, screenRect.size.width, screenRect.size.height);];
CGRect screenRect = [[UIScreen mainScreen] bounds];
imageView.image = picture;
imageView.contentMode = UIViewContentModeCenter;
[self.view addSubview:imageView];
}

iOS - Text displayed on animation

I am animating a set of frames in iOS and would like to have a text displayed at the end of it. At this point I'm only able to animate the images. The text I'm trying to display on it using CATextLayer does not show up. I simply don't know how to make the layer a subview of the view. Also the initWithFrame method I was trying to use does not seem to work with UIViewController. Could anyone help out?
1) Here's the animation code that works perfectly:
//setup animation
NSArray *theFrames = [NSArray array];
theFrames = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"frame1.png"],
[UIImage imageNamed:#"frame2.png"],
[UIImage imageNamed:#"frame3.png"],
[UIImage imageNamed:#"frame4.png"],
nil];
sceneFrames.animationImages = theFrames;
sceneFrames.animationDuration = 1;
sceneFrames.animationRepeatCount = 1;
sceneFrames.image = [theFrames objectAtIndex:theFrames.count - 1];
[sceneFrames startAnimating];
2) Here's how I'm trying to display a layer containing text in it. As you can see I'm not even at the point to display it at the end. Still trying to have it there in the first place and already running into issues:
// Create the new layer object
boxLayer = [[CATextLayer alloc] init];
// Give it a size
[boxLayer setBounds:CGRectMake(0.0, 0.0, 300.0, 85.0)];
// Give it a location
[boxLayer setPosition:CGPointMake(160.0, 350.0)];
// Make half-transparent red the background color for the layer
UIColor *reddish = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.75];
// Get CGColor object with the same color values
CGColorRef cgReddish = [reddish CGColor];
[boxLayer setBackgroundColor:cgReddish];
// Make it a sublayer on the view's layer
// THIS LINE DOES NOT WORK AND GIVES ME HEADACHES. It does not work without initWithFrame, which does not seem to work with UIViewController...
[[self layer] addSublayer:boxLayer];
// Create string
NSString *text2 = #"You are me.";
// Set font
[boxLayer setFont:#"Times New Roman"];
// Set font size
[boxLayer setFontSize:20.0];
[boxLayer setAlignmentMode:kCAAlignmentCenter];
// Assign string to layer
[boxLayer setString:text2];
replace
[[self layer] addSublayer:boxLayer];
with
[self.view.layer addSublayer:boxLayer];
or
[[self.view layer] addSublayer:boxLayer];
they're both the same
Edit
after testing, it worked perfect for me

move didFinishLaunchingWithOptions: method to viewcontroller method with button trigger

I had the following code working in AppDelegate.h/.m but cannot change it to work in ViewController with a button triggering it.
#implementation BSViewController
#synthesize imageView;
#synthesize workingImage;
- (IBAction) chooseImage:(id) sender {
UIImage* testCard = [UIImage imageNamed:#"ipad 7D.JPG"];
CGImageRef num = CGImageCreateWithImageInRect([testCard CGImage],CGRectMake(532, 0, 104, 104));
UIGraphicsBeginImageContext(CGSizeMake( 250,650));
CGContextRef con = UIGraphicsGetCurrentContext();
UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
CGContextDrawImage(con, CGRectMake(0, 0, 13, 13) ,num);
UIGraphicsEndImageContext();
CGImageRelease(num);
UIImageView* iv = [[UIImageView alloc] initWithImage:im];
[self.imageView addSubview: iv];
iv.center = self.imageView.center;
[iv release];
I see the button named "Choose image" in the simulator, but I do not see the image there.
You haven't added self.imageView to any views.
You need
[self.view addSubview:self.imageView];
To show your imageView.
I was able to make it work, but let me ask you some dummy questions before. You can skip these and jump directly to the end for the answer:
Did it worked before?
Why aren't you using ARC?
Have you linked the chooseImage: to the button with Interface Builder, or programmatically? I sometimes forget this basic step, and nothing works of course :)
Why are you adding iv as a subview of self.imageView instead of just changing the image property of self.imageView. Knowing this could be useful to answer properly.
Supposing you have a good reason for adding iv as a subview, beware iv.center = self.imageView.center the centers are in two different coordinates system.
Form the Apple Documentation:
The center is specified within the coordinate system of its superview
and is measured in points. Setting this property changes the values of
the frame properties accordingly.
Edit: Other things to check
Does self.imageView exist? I mean, have you added it with IB and linked it to the IBOutlet, or defined it programmatically?
Add a break point in chooseImage: and go through it step by step, making sure that the objects you generate there are not nil
Test: try self.imageView setImage:im instead of self.imageView addSubview:iv, even if it's not what you want to do, if everything else is ok you should see the image.
It works if you switch the order of
UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
and
CGContextDrawImage(con, CGRectMake(0, 0, 13, 13) ,num);
Otherwise you would be asking to create im with the content of the context without nothing in drawn in it.
The code would then become:
UIImage* testCard = [UIImage imageNamed:#"ipad 7D.JPG"];
CGImageRef num = CGImageCreateWithImageInRect([testCard CGImage],CGRectMake(532, 0, 104, 104));
UIGraphicsBeginImageContext(CGSizeMake( 250,650));
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextDrawImage(con, CGRectMake(0, 0, 13, 13) ,num);
UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRelease(num);
Hope it helps. :D

Add image to the left of a UILabel

I have this relatively simple question, I think.
Imagine I have a UILabel with some text in it. Then, I want on the left, or right side of
the text also an image to be displayed (added).
Something like this:
http://www.zazzle.com/blue_arrow_button_left_business_card_templates-240863912615266256
Is there a way to do it, using, say UILabel methods? I didn't find such.
Just in case someone else look this up in the future. I would subclass the UIlabel class and add an image property.
Then you can override the setter of the text and image property.
- (void)setImage:(UIImage *)image {
_image = image;
[self repositionTextAndImage];
}
- (void)setText:(NSString *)text {
[super setText:text];
[self repositionTextAndImage];
}
In repositionTextAndImage, you can do your positioning calculation. The code I pasted, just insert an image on the left.
- (void)repositionTextAndImage {
if (!self.imageView) {
self.imageView = [[UIImageView alloc] init];
[self addSubview:self.imageView];
}
self.imageView.image = self.image;
CGFloat y = (self.frame.size.height - self.image.size.height) / 2;
self.imageView.frame = CGRectMake(0, y, self.image.size.width, self.image.size.height);
}
Lastly, override drawTextInRect: and make sure you leave space on the left of your label so that it does not overlap with the image.
- (void)drawTextInRect:(CGRect)rect {
// Leave some space to draw the image.
UIEdgeInsets insets = {0, self.image.size.width + kImageTextSpacer, 0, 0};
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
I just implemented similar thing in my Live Project, hope it will be helpful.
-(void)setImageIcon:(UIImage*)image WithText:(NSString*)strText{
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = image;
float offsetY = -4.5; //This can be dynamic with respect to size of image and UILabel
attachment.bounds = CGRectIntegral( CGRectMake(0, offsetY, attachment.image.size.width, attachment.image.size.height));
NSMutableAttributedString *attachmentString = [[NSMutableAttributedString alloc] initWithAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];
NSMutableAttributedString *myString= [[NSMutableAttributedString alloc] initWithString:strText];
[attachmentString appendAttributedString:myString];
_lblMail.attributedText = attachmentString;
}
Create a custom UIView that contains a UIImageView and UILabel subview. You'll have to do some geometry logic within to size the label to fit the image on the left or right, but it shouldn't be too much.
Create a UIImageView with your image and add the UILabel on top like
[imageview addSubView:label];
Set the frame of the label accordingly to your required position.

Resources