Insert UImageVIew into UITextField - cocoa

How can I add an image to a textfield? I saw it in a few apps, but can't get around it. Also based on the location of the curser, the imageView will be initialized (add image to next line). Thanks.

If you really mean insert an image into the text box of the textfield I don't believe this is possible. What are you trying to accomplish? There are probably more appropriate ways of doing it.

Yes We can insert imageview into textfield with the following code.
UIImageView *myView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"text_line.png"]];
myView.center = CGPointMake(5, 27);
[letterField insertSubview:myView atIndex:0];// mgViewTitleBackGround is image
[myView release];
Go through this code.

Related

-[NSTextField intrinsicContentSize] always has undefined width

I'm pulling hairs here. I have an NSTextField created on a xib that has an intrinsicContentSize adjusted to it's stringValue.
If I create a textField programmatically like so:
_textfield = [[NSTextField alloc] initWithFrame:CGRectZero];
_textfield.translatesAutoresizingMaskIntoConstraints = NO;
_textfield.alignment = NSCenterTextAlignment;
_textfield.drawsBackground = NO;
[_textfield setBordered:NO];
_textfiled.stringValue = #"Test"
It's intrinsicContentSize is always (width=-1, height=16)
I've tried calling invalidateIntrinsicContentSize but to no avail…
How does one get NSTextField's intrinsicContentSize to work?? I must be doing something different than the one from the xib, I just can't figure out what.
OK, finally figured it out…
[_textfield setEditable:NO]
That's it. I guess that with an editable textfield one must have an explicit constraint for the textfield width. Which kind of makes sense, imagine editing a textfield and it would constantly grow horizontally with every keystroke... not an ideal UI.
If you do need an editable textfield, make sure the NSTextField has a width constraint set to the minimum width the view should be.

UIImageView not updating image when displayed in a UIScrollView

I am new to Xcode and objective C so the problem I have hit may be a simple one but I haven't been able to find an answer yet so thanks for any help you can give.
I am trying to write a simple app that takes a picture from the camera and displays it in a UIImageView. This all works fine if the UIImageView control is just placed in the UIView control (using the interface builder). The code I use to set the image to the control is
[self.imageViewOSFCorner setImage:image];
However I need to display several images an wanted the user to be able to scroll up and down the page. So I created a UIScrollView in the interface builder that is the size I need and placed all the controls in that. Then in the viewDidLoad method I placed the following code to display the UIScrollView
self.scrollView.contentSize = self.scrollView.frame.size;
self.scrollView.frame = self.view.frame;
[self.view addSubview:self.scrollView];
This works in that you can now scroll the page and see all of the UIImageView controls, and when I press the button it launches the camera, but now after taking the picture it doesn't display the image into the UIImageView. If I take the UIScrollView away it works again (but obviously doesn't scroll) - I am guessing I am doing something silly, is there a function I need to call to update the UIScrollView?
As an aside I originally tried to use the code
self.imageViewOSFCorner.image = image;
to display the image but this wouldn't display the image and had to use
[self.imageViewOSFCorner setImage:image];
can someone tell me why the first method didn't work?
Thanks
Will
May be useful:
self.scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width*num,view.frame.size.hight );
num is your number of all image
Ok it is working now, I must have done something stupid when creating the controls into the UIScrollView since I have started from scratch again in the interface builder and now it all works.

How to animate a tabbarbutton image

I have a toolBarItem which calls a save action on a database. I would really much like to change the image of the item (a cabinet) dynamically so that a drawer opens, a label is animated "inside" and then it closes. Very much like the trash item on the mail app animation.
I know how to make a UIView pop-up by scaling it up and down on an animation on a given times given in an array (together with an array of CT scale), so I'm guessing it could be done more or less the same way.
Does anyone know about an example of who to accomplish that?. Back on xcode 4.1 i was able to highlight the button while the label was moving, but I cannot do that anymore (somehow I did add a normal button on the toolbarItem, which I cannot do anymore).
Thanks in advance!
well, if somebody is having the same issue here is how it can be done:
- (IBAction)barButtonAction:(UIBarButtonItem *)sender {
NSArray *frameArray=[NSArray arrayWithObjects:[UIImage imageNamed:#"01-refresh.png"], [UIImage imageNamed:#"02-redo.png"], [UIImage imageNamed:#"03-loopback.png"], [UIImage imageNamed:#"03-loopback.png"],[UIImage imageNamed:#"03-loopback.png"],[UIImage imageNamed:#"03-loopback.png"],[UIImage imageNamed:#"03-loopback.png"],[UIImage imageNamed:#"03-loopback.png"], nil];
self.button.image=[UIImage animatedImageWithImages:frameArray duration:10.0];
}

UIView: hide drawing and show it again

I create a view at run time and I use it's drawRect: to draw a figure on it. In the next step I add a sublayer with contents of an image to the layer of the view and then I show it. It works. But the figure on the view is still shown below the image. With view.layer.contents = nil; before creating the sublayer, the figure on the view is removed. But I need to show it again. With [view setNeedsDisplay]; (calling drawRect:) I can draw it again.
Is there any (easier) way to hide (or cover) this figure and show it again (without removing and redrawing it)? Thanks.
Edit:
I can do this: layer.backgroundColor = [UIColor blackColor].CGColor; the figure is covered. But the backgroundColor must be transparent. Thanks very much for another ideas.
You can use:
view.layer.hidden = YES;
If you only want to hide and show a sublayer, you can keep hold of a reference to that sublayer, or find it by looking in view.layer.sublayers.

Apple Interface Builder: adding subview to UIImageView

I created UIImageView with the help of Interface Bulder. Now I want to place label inside it (as its subview). In code I can type something like: [myUIImageView addSubview:myUILabel]; But can I do it with the help of IB? I found the solution for UIView, but can't find something similar for UIImageView.
You cannot add a subview to UIImageView in interface builder for reasons only known to Apple! You are right in saying that you can addSubview programmatically, but then, the overhead of setting autoresizing masks and placements of subviews should all be handled in code, which is cumbersome.
So there is an easy workaround. Instead of dragging an instance of UIImageView in the nib, just drag a UIView and change its class to UIImageView from UIView (cmd+4 option of inspector). The only difference you find in the nib for default imageView instance and your new UIImageView subclass instance is: you cannot set image to your new imageView from nib (cmd+1 option). So, in the -viewDidLoad method of its appropriate viewController, set image to this outlet of UIImageView.
By doing so, you are free to add subviews to your "now UIImageView" instances in interface builder, which is much easy.
I would like to add answer.
While it sucks you cannot add subview to UIImageView, you can get the same effect by incorporating UIView with transparent (clear color) background.
Then put the UIImageview BEFORE it.
So the UIView has the subviews and the UIImageview is the background of the UIView.
I think that's apple's intent.
Here is a screenshot:
Here is the result:
Don't forget to set background as clear color
Now if someone could actually point me to a tutorial how to do this it'll be great. I spent hours doing it the checked answered way. The checked answer is a fine answer but very unintuitive because you can't see your background image clearly while working. I think mine is the proper way to do so.
In latest XCode(4.5) there is an option to drag and drop the required controls to the parent.
It is quite easy.
Attached screen shot for the same. I dragged the Label/TextField and Button to UIImageView
Use this code:
UIImage *image = [UIImage imageNamed:#"background.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view insertSubview:imageView atIndex:0];
(replace background.png with image) (replace atIndex:0 with whatever place in the .index root you want to insert the image and your off.

Resources