Is it possible to allow user input text on a UIImageView, just like the text tool in painter?
I cannot find any resource on this topic?
Is it possible to allow user input text on a UIImageView, just like the text tool in painter?
I cannot find any resource on this topic?
This is not possible with the current iPhone API (3.1). You will need to create your own custom uitextfields and your own image rendering methods to combine the layers into a captioned photo.
UIImageView
is not designed to hold any text, but you could add a UILabel
or UITextField
either within it or on top / below it, depending on what you want to do.
For example, suppose you want to allow the user to edit a piece of text inside an image. You could do something like this:
UIImage* image = [UIImage imageNamed:@"my_image.png"];
UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
imageView.userInteractionEnabled = YES;
UITextField* textField = [[UITextField alloc]
initWithFrame:CGRectMake(10, 10, 50, 20)];
textField.placeholder = @"type here";
[imageView addSubview:textField];
// You might also want to set the imageView's frame.
[self.view addSubview:imageView];
If you add a UITextField
as a subview of a UIImageView
, it's important to set the userInteractionEnabled
to YES
, since it defaults to NO
for that superview (it's usually YES
by default in most UIView
s).
Addendum
If you want the user to be able to click anywhere in the image to edit the text, here is one way to do it: subclass UIControl
and add a UIImageView
and a UITextField
as subviews of it, and connect the clicking action of the UIControl
to the UITextField
. Something like this (WARNING: not tested code, but it conveys the general idea):
@interface ImageAndTextView : UIControl {
UIImageView* imageView;
UITextField* textField;
}
@property (nonatomic, retain) UIImageView* imageView;
@property (nonatomic, retain) UITextField* textField;
- (void) click;
@end
@implementation ImageAndTextView
@synthesize imageView, textField;
- (id) initWithFrame: (CGRect) frame_ {
if (self = [super initWithFrame:frame_]) {
UIImage* image = [UIImage imageNamed:@"my_image.png"];
self.imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
imageView.userInteractionEnabled = YES;
[self addSubview:imageView];
CGRect textFrame = CGRectMake(10, 10, 50, 20); // whatever frame you want
self.textField = [[[UITextField alloc]
initWithFrame:textFrame] autorelease];
[self addSubview:textField];
// Now register an event to happen if the user clicks anywhere.
[self addTarget:self action:@selector(click)
forEvent:UIControlEventTouchUpInside];
}
return self;
}
- (void) click {
[textField becomeFirstResponder];
}
@end