To make things a little interesting, you are going to add instances of UILabel to the screen at random positions. In BNRHypnosisViewController.m, implement a new method that will draw a given string on the screen twenty times at random positions.
- (void)drawHypnoticMessage:(NSString *)message { for (int i = 0; i < 20; i++) { UILabel *messageLabel = [[UILabel alloc] init]; // Configure the label's colors and text messageLabel.backgroundColor = [UIColor clearColor]; messageLabel.textColor = [UIColor whiteColor]; messageLabel.text = message; // This method resizes the label, which will be relative // to the text that it is displaying [messageLabel sizeToFit]; // Get a random x value that fits within the hypnosis view's width int width = (int)(self.view.bounds.size.width - messageLabel.bounds.size.width); int x = arc4random() % width; // Get a random y value that fits within the hypnosis view's height int height = (int)(self.view.bounds.size.height - messageLabel.bounds.size.height); int y = arc4random() % height; // Update the label's frame CGRect frame = messageLabel.frame; frame.origin = CGPointMake(x, y); messageLabel.frame = frame; // Add the label to the hierarchy [self.view addSubview:messageLabel]; } }
In BNRHypnosisViewController.m, update the textFieldShouldReturn: method to call this new method, passing in the text field’s text, clear the text that the user typed, and then dismiss the keyboard by calling resignFirstResponder.
- (BOOL)textFieldShouldReturn:(UITextField *)textField { NSLog(@"%@", textField.text); [self drawHypnoticMessage:textField.text]; textField.text = @""; [textField resignFirstResponder]; return YES; }
Build and run the application, and enter some text into the text field. After tapping the return key, the text should be displayed on instances of UILabel across the view.