UILabel详解
UILable
//UILable,繼承于UIView;初始化設置坐標,width,height
UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 80, 80)];
//CGRectMake();?返回一個結構體,
/* struct CGRect {CGPoint origin;CGSize size;}; struct CGPoint {CGFloat x;CGFloat y;}; struct CGSize {CGFloat width;CGFloat height;}; */
//為label添加內容
lab.text = @"Label";
//設置背景色
lab.backgroundColor = [UIColor blackColor];
//設置字體
lab.font = [UIFont systemFontOfSize:30.0];
lab.font = [UIFont FontWithName:@"Arial" size:30.0]; /*font三種表現形式UIFont *font = [UIFont systemFontOfSize:20.0];//正常體font = [UIFont boldSystemFontOfSize:40.0];//粗體font = [UIFont italicSystemFontOfSize:40.0];// 斜體*/ //設置字體顏色 lab.textColor = [UIColor whiteColor]; /*+ (UIColor *)blackColor; // 0.0 white + (UIColor *)darkGrayColor; // 0.333 white + (UIColor *)lightGrayColor; // 0.667 white + (UIColor *)whiteColor; // 1.0 white + (UIColor *)grayColor; // 0.5 white + (UIColor *)redColor; // 1.0, 0.0, 0.0 RGB + (UIColor *)greenColor; // 0.0, 1.0, 0.0 RGB + (UIColor *)blueColor; // 0.0, 0.0, 1.0 RGB + (UIColor *)cyanColor; // 0.0, 1.0, 1.0 RGB + (UIColor *)yellowColor; // 1.0, 1.0, 0.0 RGB + (UIColor *)magentaColor; // 1.0, 0.0, 1.0 RGB + (UIColor *)orangeColor; // 1.0, 0.5, 0.0 RGB + (UIColor *)purpleColor; // 0.5, 0.0, 0.5 RGB + (UIColor *)brownColor; // 0.6, 0.4, 0.2 RGB + (UIColor *)clearColor; // 0.0 white, 0.0 alpha */ //設置對齊方式 lab.textAlignment = UITextAlignmentLeft; /* typedef enum {UITextAlignmentLeft = 0,UITextAlignmentCenter,UITextAlignmentRight, // could add justified in future} UITextAlignment;*/ //設置行數 lab.numberOfLines = 1;//默認是1,0表示不限行數//設置折行方式 lab.lineBreakMode = UILineBreakModeClip;//與行數配合使用 /* typedef enum { UILineBreakModeWordWrap = 0, // Wrap at word boundariesUILineBreakModeCharacterWrap, // Wrap at character boundariesUILineBreakModeClip, // Simply clip when it hits the end of the rectUILineBreakModeHeadTruncation, // Truncate at head of line: "...wxyz". Will truncate multiline text on first lineUILineBreakModeTailTruncation, // Truncate at tail of line: "abcd...". Will truncate multiline text on last lineUILineBreakModeMiddleTruncation, // Truncate middle of line: "ab...yz". Will truncate multiline text in the middle} UILineBreakMode;*/ /*補充:根據文字多少確定label大小NSString *str = @"abcdefg hijklmn opqrst uvwxyz";CGSize size = [str sizeWithFont:[UIFont systemFontSize:20.0]//告知字體constrainedToSize:CGSizeMake(320, 1000) //一般限定行寬,來找高heightlineBreakMode:UILineBreakModeClip];//告知換行方式UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 320, size.height)];*/ //將lab添加到window // [self.window addSubview:lab]; [lab release];
lab.font = [UIFont FontWithName:@"Arial" size:30.0]; /*font三種表現形式UIFont *font = [UIFont systemFontOfSize:20.0];//正常體font = [UIFont boldSystemFontOfSize:40.0];//粗體font = [UIFont italicSystemFontOfSize:40.0];// 斜體*/ //設置字體顏色 lab.textColor = [UIColor whiteColor]; /*+ (UIColor *)blackColor; // 0.0 white + (UIColor *)darkGrayColor; // 0.333 white + (UIColor *)lightGrayColor; // 0.667 white + (UIColor *)whiteColor; // 1.0 white + (UIColor *)grayColor; // 0.5 white + (UIColor *)redColor; // 1.0, 0.0, 0.0 RGB + (UIColor *)greenColor; // 0.0, 1.0, 0.0 RGB + (UIColor *)blueColor; // 0.0, 0.0, 1.0 RGB + (UIColor *)cyanColor; // 0.0, 1.0, 1.0 RGB + (UIColor *)yellowColor; // 1.0, 1.0, 0.0 RGB + (UIColor *)magentaColor; // 1.0, 0.0, 1.0 RGB + (UIColor *)orangeColor; // 1.0, 0.5, 0.0 RGB + (UIColor *)purpleColor; // 0.5, 0.0, 0.5 RGB + (UIColor *)brownColor; // 0.6, 0.4, 0.2 RGB + (UIColor *)clearColor; // 0.0 white, 0.0 alpha */ //設置對齊方式 lab.textAlignment = UITextAlignmentLeft; /* typedef enum {UITextAlignmentLeft = 0,UITextAlignmentCenter,UITextAlignmentRight, // could add justified in future} UITextAlignment;*/ //設置行數 lab.numberOfLines = 1;//默認是1,0表示不限行數//設置折行方式 lab.lineBreakMode = UILineBreakModeClip;//與行數配合使用 /* typedef enum { UILineBreakModeWordWrap = 0, // Wrap at word boundariesUILineBreakModeCharacterWrap, // Wrap at character boundariesUILineBreakModeClip, // Simply clip when it hits the end of the rectUILineBreakModeHeadTruncation, // Truncate at head of line: "...wxyz". Will truncate multiline text on first lineUILineBreakModeTailTruncation, // Truncate at tail of line: "abcd...". Will truncate multiline text on last lineUILineBreakModeMiddleTruncation, // Truncate middle of line: "ab...yz". Will truncate multiline text in the middle} UILineBreakMode;*/ /*補充:根據文字多少確定label大小NSString *str = @"abcdefg hijklmn opqrst uvwxyz";CGSize size = [str sizeWithFont:[UIFont systemFontSize:20.0]//告知字體constrainedToSize:CGSizeMake(320, 1000) //一般限定行寬,來找高heightlineBreakMode:UILineBreakModeClip];//告知換行方式UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 320, size.height)];*/ //將lab添加到window // [self.window addSubview:lab]; [lab release];
轉載于:https://www.cnblogs.com/3G-iphone/archive/2012/12/03/2800455.html
總結
- 上一篇: php书籍推荐
- 下一篇: [嵌入式]Bootloader的作用