I have a UIView
that I'd like to add several bits of text to. I have used a UITextView
but I think that's overkill as it doesn't need to be editable. I thought about using a UILabel
or a UITextField
, but I don't see how you tell the superview where to position the UILabel
or UITextField
within itself. I want the lowest footprint object that will let me put text of a font/color/size of my choosing in my UIView
where I want it. Not too much to ask, eh?

- 8,130
- 15
- 59
- 93

- 6,332
- 11
- 41
- 53
7 Answers
The simplest approach for you would be:
UILabel *yourLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 20)];
[yourLabel setTextColor:[UIColor blackColor]];
[yourLabel setBackgroundColor:[UIColor clearColor]];
[yourLabel setFont:[UIFont fontWithName: @"Trebuchet MS" size: 14.0f]];
[yourSuperView addSubview:yourLabel];
Building or populating Views in your code will probably require you to use CGRectMake
a lot.
As its name says, it creates a rectangle that you can use to set the relative position (relative to the borders of your superview) and size of your UIView-Subclass
(in this case a UILabel
).
It works like this:
yourLabel.Frame = CGRectMake(x, y, width, height); //x,y,width,height are float values.
x
defines the spacing between the left hand border of the superview and the beginning of the subview your about to add, same applies to y
but relating to the spacing between top-border of your superview.
then width and height are self-explanatory i think.
Hope this gets you on the track.
-
1I looked up both UILabel and UITextField in the documentation and see that they inherit from... UITextField : UIControl : UIView : UIResponder : NSObject UILabel : UIView : UIResponder : NSObject So I'm guessing that since they both inherit from UIView, that they have all the properties that UIView has? ...plus the extras they pick up along the way? This makes sense, but in the documentation for UILabel, Apple doesn't list any methods other than - (void)drawTextInRect:(CGRect)rect and - (CGRect)textRectForBounds:(CGRect)bounds – Steve Jul 09 '10 at 15:05
-
(Comment boxes are too small for wordy people like me...) Am I safe to assume that I have access to all properties of every superclass up the chain to NSObject for any class I use? Thanks for the help, it's appreciated! – Steve Jul 09 '10 at 15:05
-
yep you're correct, this is exactly how inheritance works. I still don't get what you want to achieve with drawTextInRect etc. A UILabel (Or a UITextview for that matter) are Views that provide you with functionality that allows you to set text in a very easy way. Maybe I just don't see the problem here :). – samsam Jul 14 '10 at 09:19
Instead of finding a way to tell the view where to position the UILabel, you can tell the UILabel where to position itself in the view by using "center".
E.g.
myLabel.center = CGPointMake(0.0, 0.0);
Hope you'll be able to use UILabel, for me it's the basic form of a flexible non editable text.

- 6,277
- 3
- 31
- 45
-
Thanks - a nice, succinct way to put it. Am I correct in thinking that things like UILabel carry less overhead than a full UIView? Now that I (think I) realize that it inherits from UIView, is this a faulty assumption? – Steve Jul 09 '10 at 14:49
-
1Steve, I think you're a perfectionist and that's good. But our line of thinking is quite different, UILabel is already there for you as a convenience, fully tested. UIView is doing it in a lower-level way. Yes it may mean less overhead, faster, less memory usage, but with the effort and bugs that can occur, is it worth it? I suggest use UILabel if it satisfies your needs, unless you'll be displaying thousands of them then you can rethink your strategy of using a more efficient way of doing things. – Manny Jul 10 '10 at 02:11
For Swift:
let yourLabel = UILabel(frame: CGRectMake(100, 100, 100, 100))
yourLabel.textColor = UIColor.whiteColor()
yourLabel.backgroundColor = UIColor.blackColor()
yourLabel.text = "mylabel text"
yoursuperview.addSubview(yourLabel)

- 38,543
- 21
- 161
- 168
-
for swift 3 only change UILabel syntax to UILabel(frame: CGRect(x:100, y:100, width:100, height:100)) – Jirson Tavera Mar 17 '17 at 21:03
This question is old, but for a pure UIView text option without using UILabel or UITextField (as all the other answers describe, but the question is how to do it without them), drawRect in a subclassed UIView works for me. Like so:
- (void)drawRect:(CGRect)rect{
NSString *string = @"Hello World!";
[string drawAtPoint:CGPointMake(100, 100) withFont:[UIFont boldSystemFontOfSize:16.0]];
}

- 332
- 1
- 6
-
1I take this back now -- Actually this is discouraged from iOS 7, Apple says "Many drawing additions to the NSString class are deprecated in favor of newer variants." meaning drawAtPoint:WithFont is not the best way to draw a nsstring into Uiview. – july Jun 12 '13 at 00:35
-
You can use - (void)drawAtPoint:(CGPoint)point withAttributes:(NSDictionary *)attrs NS_AVAILABLE_IOS(7_0); for iOs7 and later – goelectric Sep 24 '13 at 13:11
This routine displays a text at a X-Y position
-(void)placeText:(NSString *)theText:(int)theX:(int)theY {
UILabel *textLabel;
// Set font and calculate used space
UIFont *textFont = [UIFont fontWithName:@"Helvetica" size:14];
CGSize textStringSize = [theText sizeWithFont:textFont constrainedToSize:CGSizeMake(300,50) lineBreakMode:NSLineBreakByTruncatingTail];
// Position of the text
textLabel = [[UILabel alloc] initWithFrame:CGRectMake(theX+OFFSETIMAGEX-(textStringSize.width/2), theY+OFFSETIMAGEY-(textStringSize.height/2), textStringSize.width,textStringSize.height)];
// Set text attributes
textLabel.textColor = [UIColor blackColor];
textLabel.backgroundColor = [UIColor orangeColor];
textLabel.font = textFont;
textLabel.text = theText;
// Display text
[self.view addSubview:textLabel];
}

- 4,342
- 1
- 38
- 37
It might be late but here is what I use:-
CGRect labelFrame = CGRectMake(120,300, 530, 100);
UILabel *myLabel = [[UILabel alloc] initWithFrame:labelFrame];
//If you need to change the color
[myLabel setTextColor:[UIColor whiteColor]];
//If you need to change the system font
[myLabel setFont:[UIFont fontWithName:NULL size:23]];
//If you need alignment
[myLabel setTextAlignment:NSTextAlignmentCenter];
// The label will use an unlimited number of lines
[myLabel setNumberOfLines:0];
//Add label view to current view
[self.view addSubview:myLabel];
NSString *someString = @"Sample String, Yarp!";
myLabel.text = someString;

- 19
- 2