2

UIView にテキストを追加したいとしましょう。これを行うには、まずテキストフィールドを表示するボタンをクリックします。このテキストフィールドは画面上で移動可能で、どこにでも配置できます。ユーザーはフィールドにテキストを入力し、保存ボタンをクリックします。テキストフィールドが消え、UILabel が作成され、その場所に入力されたテキストが表示されます。ユーザーが後でそのテキストを編集したい場合は、タッチするだけで、UILabel が削除され、テキストの内容を含むテキスト フィールドが表示されます。ラベルの touch メソッドはまだ実装していません。

そのような獣からどこから始めますか?

UILabel を正しいプロパティとともに配列に保持する TextView を作成しました。それは配列を介して実行され、drawRect はすべてのラベルを想定されている場所に正確にポップアウトします。

メモリをリークしているか、これについて完全に間違っているのではないかと心配しています。私を正しい方向に導くのに役立つチュートリアルや誰かが知っている何かがありますか? TextView は UILabel のサブクラスであるべきですか?

これが私のTextView.hです

@interface TextManager : UIView 

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

TextView.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

目的の結果を得るために、ラベルとテキスト フィールドを交換する必要はありません。UITextFielda 内で1 つを使用し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 に答える