Saturday, December 17, 2011

Display the multi-lines in the UILabel

To use NSString method, "sizeWithFont:constrainedToSize: lineBreakMode:"
It figure out the UILabel size to accommodate the long String.

//  init the content of string

NSString *string = @"Cocos2d-x open source project is C++ version of cocos2d-iphone. We focus on making cocos2d framework cross multiple platforms. Mobile games can be written on the top of cocos2d-x in C++ or Lua language, via the COMPLETELY SAME API as cocos2d-iphone, they can easily be built & run on iOS, Android, Samsung Bada and BlackBerry Tablet OS. Cocos2d-x also supports Windows & Linux, therefore we can debug source code easily and write editors on desktop operating systems."; 

// Determine the size of UILabel for the string
CGSize aSize = [string sizeWithFont: [UIFont systemFontOfSize:18] constrainedToSize:CGSizeMake(250.0, 999.0) lineBreakMode:UILineBreakModeWordWrap]; 

 // Declare UILabel,
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(20, 20, aSize.width, aSize.height)]; 

 // init the label
label.backgroundColor = [UIColor grayColor]; 
label.textColor = [UIColor redColor]; 
label.font = UIFont systemFontOfSize:18;
label.lineBreakMode = UILineBreakModeWordWrap; 
label.text = string; 
label.numberOfLines = 0;  
[self.view addSubview:label]; 

No comments:

Post a Comment