views:

58

answers:

3

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?

A: 

add a UILabel to your View. then override the View's layoutSubviews method.

gga80
A: 

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.

Manny
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
Steve, 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
+1  A: 

Hi there, 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,heigth are float values.

x defines the spacing between the left hand border of the superview and the begining of the subview your about to add, same applies to y but relating to the spacing between top-boarder of your superview. then width and height are self-explanatory i think.

hope this gets you on the track.
cheers
sam

samsam
I looked up both UILabel and UITextField in the documentation and see that they inherit from...UITextField : UIControl : UIView : UIResponder : NSObjectUILabel : UIView : UIResponder : NSObjectSo 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
(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
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