1

是否可以允许用户在 UIImageView 上输入文本,就像 Painter 中的文本工具一样?

我找不到有关此主题的任何资源?

4

2 回答 2

5

UIImageView不是为保存任何文本而设计的,但您可以在其中或在其顶部/下方添加一个UILabelUITextField,具体取决于您要执行的操作。

例如,假设您希望允许用户编辑图像中的一段文本。你可以这样做:

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];

如果将 a 添加UITextField为 a 的子视图,则将其设置为UIImageView很重要,因为它默认为该父视图(通常在大多数s 中默认为)。userInteractionEnabledYESNOYESUIView

附录

如果您希望用户能够单击图像中的任意位置来编辑文本,这是一种方法:子类化UIControl并添加 aUIImageView和 aUITextField作为它的子视图,并将单击操作连接UIControlUITextField. 像这样的东西(警告:未经测试的代码,但它传达了总体思路):

@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
于 2009-09-17T06:51:40.623 回答
0

这对于当前的 iPhone API (3.1) 是不可能的。您将需要创建自己的自定义 uitextfields 和自己的图像渲染方法,以将图层组合成带标题的照片。

于 2009-09-17T05:47:33.663 回答