假设您要为UIView添加一些文本。要做到这一点,首先单击一个显示文本字段的按钮。此文本字段可在屏幕上移动,并可放置在任何位置。用户将在该字段中输入文本并单击保存按钮。文本字段消失,并创建UILabel,并在其中输入文本。如果用户希望稍后编辑该文本,他们只需触摸它,UILabel将被删除,文本字段将显示文本内容。我还没有实现标签的触摸方法。
我从哪里开始这样的野兽?
我创建了一个TextView,它将UILabel与其正确的属性一起保存在Array中。它会在数组中运行,而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 :(得分: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