2

假设您想向 UIView 添加一些文本。为此,您首先单击一个显示文本字段的按钮。此文本字段在屏幕上是可移动的,并且可以放置在任何地方。用户将在字段中输入文本并单击保存按钮。文本字段消失,并创建了一个 UILabel 并在其位置与输入的文本一起使用。如果用户希望稍后编辑该文本,他们只需触摸它,UILabel 将被删除,并且将出现一个包含文本内容的文本字段。我还没有实现标签的触摸方法。

我会从哪里开始这样的野兽?

我创建了一个 TextView,它将 UILabel 及其正确的属性保存在一个数组中。它将通过数组运行,并且 drawRect 将准确地弹出每个标签。

我只是担心我正在泄漏内存,或者我正在完全错误地解决这个问题。是否有任何人都知道的教程或其他东西可以帮助我指出正确的方向?TextView 应该是 UILabel 的子类吗?

这是我的 TextView.h

@interface TextManager : UIView 

- (void) addTextToView : (NSString *) s : (int)rx :(int) ry;

文本视图.m

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
          self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
       NSLog(@"Draw Rect Text Manager");
    // Drawing code
    Settings *mySettings = [Settings sharedSettings];

    if ([[mySettings returnTextArray] count] > 0) {
        [self.superview addSubview:[[mySettings returnTextArray] lastObject]];
    }

}

- (void) addTextToView : (NSString *) s : (int)rx :(int) ry {
    NSLog(@"Add Text to View");

    UIColor *tempColor = [UIColor blueColor];
    CGRect  tempRect = CGRectMake(rx, ry, 100, 100);

    UILabel *thisLabel = [[UILabel alloc] initWithFrame:tempRect];
    thisLabel.text = s;
    thisLabel.font = [UIFont fontWithName:@"Helvetica" size:16];
    thisLabel.textColor = tempColor;
    thisLabel.backgroundColor = [UIColor clearColor];
    thisLabel.transform = CGAffineTransformMakeRotation (0);
    thisLabel.userInteractionEnabled = NO;
    thisLabel.tag = 1;

    [textArray addObject:thisLabel];

    [self setNeedsDisplay];
}
4

1 回答 1

2

您无需交换标签和文本字段即可获得所需的结果。UITextField在 a 中使用一个UIView

@interface CSTextView : UIView

@property (nonatomic, strong) UITextField *textField;

@end

@implementation CSTextView

- (id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame])) {
        UITextField *textField = [[UITextField alloc] initWithFrame:[self bounds]];

        textField.placeholder = @"Your text here";
        textField.font = [UIFont fontWithName:@"Helvetica" size:16];
        textField.textColor = [UIColor redColor];
        textField.backgroundColor = [UIColor clearColor];
        textField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        textField.textAlignment = NSTextAlignmentCenter;
        textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

        [self setTextField:textField];
        [self addSubview:textField];
    }
    return self;
}

@end

然后,在您的视图控制器中,实现UITextFieldDelegate协议并向文本视图添加手势识别器以处理拖动,如下所示:

@interface CSViewController : UIViewController <UITextFieldDelegate>

@property (nonatomic, strong) UIView *textView;

@end

@implementation CSViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    CSTextView *textView = [[CSTextView alloc] initWithFrame:CGRectMake(20, 100, 280, 60)];

    [textView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.1]];
    [[textView textField] setDelegate:self];

    [textView addGestureRecognizer:[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]];

    [self setTextView:textView];
    [[self view] addSubview:textView];
}

#pragma mark -
#pragma mark Gesture recognizer

- (void)handlePan:(UIPanGestureRecognizer *)panRecognizer
{
    CGPoint translation = [panRecognizer translationInView:[self view]];

    if ([panRecognizer state] == UIGestureRecognizerStateChanged) {
        [[self textView] setFrame:CGRectOffset([[self textView] frame], translation.x, translation.y)];
    }

    [panRecognizer setTranslation:CGPointZero inView:[self view]];
}

#pragma mark -
#pragma mark Text field methods

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];

    return YES;
}

@end
于 2013-02-16T04:01:52.043 回答