2013-02-16 54 views
2

假設你想添加一些文本到UIView。要做到這一點,你首先點擊一個可以調出文本框的按鈕。此文本框可在屏幕上移動,並可放置在任何地方。用戶將輸入文本到字段中並單擊保存按鈕。文本字段消失並創建一個UILabel,並在輸入文本的位置。如果用戶希望稍後編輯該文本,則只需觸摸該文本即可,UILabel將被刪除並且文本字段將與文本內容一起顯示。我還沒有實現標籤的觸摸方法。動態添加UILabel到UIView然後編輯

我從哪裏開始這樣一個野獸?

我已經創建了一個TextView,它保存了一個Array中的UILabel及其正確的屬性。它會運行,儘管數組和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]; 
} 
+0

檢查使用儀器泄漏 – Jeremy 2013-02-16 03:55:25

回答

2

你不應該需要更換標籤和文本字段,以達到預期的效果。使用一個UITextField一個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 
+0

哦哇!你只是給了我一個金塊! – JasonBourne 2013-02-16 21:58:34

+0

如果我添加這些來查看,我添加的最後一個是我可以移動或返回並編輯的唯一一個@ cameron-spickert – JasonBourne 2013-02-17 16:53:26

相關問題