Here's how I do it for a UIButton which is centered in the view/window and has some padding added around the text. You'll most likely have to set a maximum size for your label width as well and change the font size to suit your needs.

#import <UIKit/UIStringDrawing.h>

NSString* buttonText;

CGSize fontSize = [buttonText sizeWithFont:[UIFont systemFontOfSize: 12.0]];
CGRect currentFrame = button.frame;
CGRect windowFrame = self.view.frame;
CGRect buttonFrame = CGRectMake(windowFrame.size.width/2.0 - (fontSize.width + 15.0)/2.0, currentFrame.origin.y, fontSize.width + 15.0, fontSize.height + 15.0);
[button setFrame:buttonFrame];

Devon

On 29-Dec-08, at 11:20 PM, Tharindu Madushanka wrote:

Hi
I am new to iPhone application development and please help me on this.
I need to add the contents in a text field to a UILabel. But I can't guess how long the string in the text field be. There fore, can I make a UILabel from the text ( I mean to fit the text to UILabel size). I have searched but
could not find a solution. Please help me on this with some code.
I think if I get a CGRect object with some string it could set as UILabel's
frame. can you suggest some code segment to do this.
Thank you
Tharindu
_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/dferns%40devonferns.com

This email sent to [email protected]

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to