I have a very simple app (frankly because I don't know how to create anything more complicated) in which a UILabel, contained within a scroll view, takes on a new string of text when the user touches any 1 of 8 buttons.
My problem is that these strings of text are all of different lengths. I would like to somehow code the UILabel so that it's size is dependent upon the length of text that inhabits it.
I've googled this like crazy, and have found several threads of the same topic, but can't quite seem to get it to work. Could someone please tell me what I need to enter in my .m and .h files in order for this to work properly?
One thing you could use is the sizeWithFont:constrainedToSize:lineBreakMode: method of NSString. You send it to the string you want to put in the label, give it the font and size you will be using in the label, the constraint rect (the size you do not want to go over, for example 300 width and 150 height), and line break mode (word wrap) and it will return you a size of the rectangle in which that text would fit. Based on that you can set the size of the label and calculate the number of lines you need to set.
Now, I'm not sure if there is any easier way, maybe you can dig around the NSString and UILabel class references in the docs and see if you can find something.
One thing you could use is the sizeWithFont:constrainedToSize:lineBreakMode: method of NSString. You send it to the string you want to put in the label, give it the font and size you will be using in the label, the constraint rect (the size you do not want to go over, for example 300 width and 150 height), and line break mode (word wrap) and it will return you a size of the rectangle in which that text would fit. Based on that you can set the size of the label and calculate the number of lines you need to set.
Now, I'm not sure if there is any easier way, maybe you can dig around the NSString and UILabel class references in the docs and see if you can find something.
I haven't mucked with this in quite a while. (I'm off working on a Mac OS project.) UIView has a -sizeToFit method. Will a label resize itself if you call that method? You'd need to set the numberOfLines property to 0 first, so the label will use as many lines as needed.
If that doesn't work, baja_yu's method will work. I've used that approach to create custom table view cells that size themselves to the text they are displaying.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
So you think I need to manually calculate the size of the frame, based on the string length and font size? There's no good way to create a dynamically sized UILabel?
I haven't mucked with this in quite a while. (I'm off working on a Mac OS project.) UIView has a -sizeToFit method. Will a label resize itself if you call that method? You'd need to set the numberOfLines property to 0 first, so the label will use as many lines as needed.
If that doesn't work, baja_yu's method will work. I've used that approach to create custom table view cells that size themselves to the text they are displaying.
I could be wrong (very likely), but I thought sizetofit resized the text to fit the label, and not the label to fit the text.
So I created a new class for the verticallyalignedlabel, ran it and everything worked well.
Then, I added that last piece of code to my view controller class, in the viewdidload section, and I had a number of errors. First, I had an error with the import, which I corrected by adding "" to your code.
I also have an error saying that x, y, w, and h are undeclared, which makes me think that it isn't importing the verticallyalignedlabel.h properly.
Thanks for this! Will this still work with my app though? I have the label embedded in a scroll view, on a tab bar view controlled window. Like I said earlier, I'm very new to xcode. What is a table view?
Sorry, didn't read the OP properly... I assumed you were using a tableView as well.
Quote:
Originally Posted by ScottishPirate
Thanks for this! Will this still work with my app though? I have the label embedded in a scroll view, on a tab bar view controlled window. Like I said earlier, I'm very new to xcode. What is a table view?