Click here to Skip to main content
Click here to Skip to main content

A UITextView with borders

, 6 Jan 2014
Rate this:
Please Sign up or sign in to vote.
Ok I very rarely ever work with the UITextView (more like ever) and this will be a quick post to go over what I am sure is probably a simple and widely known fix for some developers. So in my current project I have a need for the user to be able to enter multiple […]

Ok I very rarely ever work with the UITextView (more like ever) and this will be a quick post to go over what I am sure is probably a simple and widely known fix for some developers.

So in my current project I have a need for the user to be able to enter multiple lines of text as a way of storing notes:

screen shot

The screen looks simple and as such I assumed it would be easily laid out in the storyboard editor. Maybe that was my first mistake? The following picture gives you an idea of what it looks like in the storyboard and, without some code quick additions, what it also will look when run:

iosstoryboard

Are you thinking the same thing I was?  Where is the border that intuitively tells the users they can click and enter some text on the screen?

I don’t know the reason behind this (maybe someone can explain it) but apparently the UITextView does not come with borders that you can control or define with the storyboard editor. There is also no way to enter Placeholder text but I will leave that as a subject for a future post.

For now lets cover the solution of getting a border to surround the text area.

Make sure that you first include the QuartzCore.framwork and then for your view add the following include statement:

#include <QuartzCore/QuartzCore.h>

Then in your views viewDidLoad method add the following lines of code (make sure that you change the name to match):

- (void)viewDidLoad
{
   [super viewDidLoad];
   // Do any additional setup after loading the view.

   [[self.textViewNotes layer] setBorderColor:[[UIColor lightGrayColor] CGColor]];
   [[self.textViewNotes layer] setBorderWidth:.4];
   [[self.textViewNotes layer] setCornerRadius:8.0f];    
}

Run it and there you go.

I am not going to go into details about what each line of code is doing because in this situation I believe the code is doing a fairly good job at documenting itself.

Where did I get the numbers from you ask?? I hate to say this but I applied no science in those numbers and it was just guess work and trying to find something that matched the UITextField already on the screen. I tried using the getters on my title field (it’s a UITextField) but that didn’t work. Probably a subject for another post at some other time.

For now enjoy and as always Happy Coding!!

License

This article, along with any associated source code and files, is licensed under The MIT License

About the Author

Dennis E White
Software Developer
United States United States
I am software developer with over 20 years of professional experience. I have been employed as a software developer since the early 90′s back when Microsoft’s Windows 3.1x was gaining popularity and IBM’s OS/2 was the predominant leader in 32-bit PC based Operating Systems.
 
Prior to choosing this as my profession I had studied architecture and then later Electrical and Mechanical engineering in college. As a young kid growing up I always played with computers, my first computer was a TRS-80 that I would spend countless hours writing programs for, I never really thought of programming as a profession. The story goes that in my final year of college I took a C/C++ programming class and had so much fun working on the various projects that my professor told me something that changed everything.
 
“You know they pay people to do stuff like this for a living?” – Professor Bolman
 
Check out my blog here.
 
My current and ever evolving projects:
 
jqAlert javascript alerts done right for those using jQueryUI.
DooScrib Doodle and scribble pad written in javascript for use with HTML5 Canvas.
Follow on   Twitter   Google+

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 6 Jan 2014
Article Copyright 2014 by Dennis E White
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid