欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

UILabel详解

发布时间:2025/3/21 编程问答 44 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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];

转载于:https://www.cnblogs.com/3G-iphone/archive/2012/12/03/2800455.html

总结

以上是生活随笔为你收集整理的UILabel详解的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。