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的作用