Adding the Labels to the Screen

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.

-​ ​(​v​o​i​d​)​d​r​a​w​H​y​p​n​o​t​i​c​M​e​s​s​a​g​e​:​(​N​S​S​t​r​i​n​g​ ​*​)​m​e​s​s​a​g​e​
{​
 ​ ​ ​ ​f​o​r​ ​(​i​n​t​ ​i​ ​=​ ​0​;​ ​i​ ​<​ ​2​0​;​ ​i​+​+​)​ ​{​

 ​ ​ ​ ​ ​ ​ ​ ​U​I​L​a​b​e​l​ ​*​m​e​s​s​a​g​e​L​a​b​e​l​ ​=​ ​[​[​U​I​L​a​b​e​l​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​C​o​n​f​i​g​u​r​e​ ​t​h​e​ ​l​a​b​e​l​'​s​ ​c​o​l​o​r​s​ ​a​n​d​ ​t​e​x​t​
 ​ ​ ​ ​ ​ ​ ​ ​m​e​s​s​a​g​e​L​a​b​e​l​.​b​a​c​k​g​r​o​u​n​d​C​o​l​o​r​ ​=​ ​[​U​I​C​o​l​o​r​ ​c​l​e​a​r​C​o​l​o​r​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​m​e​s​s​a​g​e​L​a​b​e​l​.​t​e​x​t​C​o​l​o​r​ ​=​ ​[​U​I​C​o​l​o​r​ ​w​h​i​t​e​C​o​l​o​r​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​m​e​s​s​a​g​e​L​a​b​e​l​.​t​e​x​t​ ​=​ ​m​e​s​s​a​g​e​;​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​T​h​i​s​ ​m​e​t​h​o​d​ ​r​e​s​i​z​e​s​ ​t​h​e​ ​l​a​b​e​l​,​ ​w​h​i​c​h​ ​w​i​l​l​ ​b​e​ ​r​e​l​a​t​i​v​e​
 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​t​o​ ​t​h​e​ ​t​e​x​t​ ​t​h​a​t​ ​i​t​ ​i​s​ ​d​i​s​p​l​a​y​i​n​g​
 ​ ​ ​ ​ ​ ​ ​ ​[​m​e​s​s​a​g​e​L​a​b​e​l​ ​s​i​z​e​T​o​F​i​t​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​G​e​t​ ​a​ ​r​a​n​d​o​m​ ​x​ ​v​a​l​u​e​ ​t​h​a​t​ ​f​i​t​s​ ​w​i​t​h​i​n​ ​t​h​e​ ​h​y​p​n​o​s​i​s​ ​v​i​e​w​'​s​ ​w​i​d​t​h​
 ​ ​ ​ ​ ​ ​ ​ ​i​n​t​ ​w​i​d​t​h​ ​=​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​(​i​n​t​)​(​s​e​l​f​.​v​i​e​w​.​b​o​u​n​d​s​.​s​i​z​e​.​w​i​d​t​h​ ​-​ ​m​e​s​s​a​g​e​L​a​b​e​l​.​b​o​u​n​d​s​.​s​i​z​e​.​w​i​d​t​h​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​i​n​t​ ​x​ ​=​ ​a​r​c​4​r​a​n​d​o​m​(​)​ ​%​ ​w​i​d​t​h​;​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​G​e​t​ ​a​ ​r​a​n​d​o​m​ ​y​ ​v​a​l​u​e​ ​t​h​a​t​ ​f​i​t​s​ ​w​i​t​h​i​n​ ​t​h​e​ ​h​y​p​n​o​s​i​s​ ​v​i​e​w​'​s​ ​h​e​i​g​h​t​
 ​ ​ ​ ​ ​ ​ ​ ​i​n​t​ ​h​e​i​g​h​t​ ​=​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​(​i​n​t​)​(​s​e​l​f​.​v​i​e​w​.​b​o​u​n​d​s​.​s​i​z​e​.​h​e​i​g​h​t​ ​-​ ​m​e​s​s​a​g​e​L​a​b​e​l​.​b​o​u​n​d​s​.​s​i​z​e​.​h​e​i​g​h​t​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​i​n​t​ ​y​ ​=​ ​a​r​c​4​r​a​n​d​o​m​(​)​ ​%​ ​h​e​i​g​h​t​;​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​U​p​d​a​t​e​ ​t​h​e​ ​l​a​b​e​l​'​s​ ​f​r​a​m​e​
 ​ ​ ​ ​ ​ ​ ​ ​C​G​R​e​c​t​ ​f​r​a​m​e​ ​=​ ​m​e​s​s​a​g​e​L​a​b​e​l​.​f​r​a​m​e​;​
 ​ ​ ​ ​ ​ ​ ​ ​f​r​a​m​e​.​o​r​i​g​i​n​ ​=​ ​C​G​P​o​i​n​t​M​a​k​e​(​x​,​ ​y​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​m​e​s​s​a​g​e​L​a​b​e​l​.​f​r​a​m​e​ ​=​ ​f​r​a​m​e​;​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​A​d​d​ ​t​h​e​ ​l​a​b​e​l​ ​t​o​ ​t​h​e​ ​h​i​e​r​a​r​c​h​y​
 ​ ​ ​ ​ ​ ​ ​ ​[​s​e​l​f​.​v​i​e​w​ ​a​d​d​S​u​b​v​i​e​w​:​m​e​s​s​a​g​e​L​a​b​e​l​]​;​
 ​ ​ ​ ​}​
}​

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.

-​ ​(​B​O​O​L​)​t​e​x​t​F​i​e​l​d​S​h​o​u​l​d​R​e​t​u​r​n​:​(​U​I​T​e​x​t​F​i​e​l​d​ ​*​)​t​e​x​t​F​i​e​l​d​
{​
 ​ ​ ​ ​N​S​L​o​g​(​@​"​%​@​"​,​ ​t​e​x​t​F​i​e​l​d​.​t​e​x​t​)​;​
 ​ ​ ​ ​[​s​e​l​f​ ​d​r​a​w​H​y​p​n​o​t​i​c​M​e​s​s​a​g​e​:​t​e​x​t​F​i​e​l​d​.​t​e​x​t​]​;​

 ​ ​ ​ ​t​e​x​t​F​i​e​l​d​.​t​e​x​t​ ​=​ ​@​"​"​;​
 ​ ​ ​ ​[​t​e​x​t​F​i​e​l​d​ ​r​e​s​i​g​n​F​i​r​s​t​R​e​s​p​o​n​d​e​r​]​;​

 ​ ​ ​ ​r​e​t​u​r​n​ ​Y​E​S​;​
}​

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset