UILabel专题
来源:互联网 发布:淘宝卖活体 编辑:程序博客网 时间:2024/06/05 09:39
UILabel是iOS开发中最常用最基础的控件之一,其父类为UIView。
1、常用属性:
// 初始化 UILabel 对象,并设置其在父视图上的位置 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 20)]; // 设置 UILabel 中的展示文字 label.text = @"我是一个 UILabel "; // 设置 UILabel 的字体大小为 15 label.font = [UIFont systemFontOfSize:15]; // 设置 UILabel 的字体颜色,系统默认为黑色 label.textColor = [UIColor redColor]; // 设置 UILabel 的背景色 label.backgroundColor = [UIColor yellowColor]; // 设置 UILabel 的 tag 值 label.tag = 3; // 设置 UILabel 中的文字对其方式,系统默认为左对齐,此处设置为居中 label.textAlignment = NSTextAlignmentCenter; // 设置 UILabel 中可显示的文字行数,0为不限行数,1为一行,2为两行 label.numberOfLines = 2; // 设置 UILabel 的折行方式 label.lineBreakMode = NSLineBreakByWordWrapping; // 设置 UILabel 的用户交互,系统默认为 NO label.userInteractionEnabled = YES; // 将label加载到父视图上 [self.view addSubview:label];
// 待设置字体 NSString *text = @"本应两相忘,何苦损梵行。"; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text]; // 添加字体和该字体的范围,将 text 前半句的字体大小设置为25 [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:25] range:NSMakeRange(0, 6)]; // 给文字添加颜色,将前半句的颜色设置为红色 [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 6)]; // 给前半句添加下划线 [attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, 6)]; // 给后半句添加颜色 [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(6, 6)]; // 段落样式 NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init]; //行间距 paragraph.lineSpacing = 10; //段落间距 paragraph.paragraphSpacing = 20; //对齐方式 paragraph.alignment = NSTextAlignmentLeft; //指定段落开始的缩进像素 paragraph.firstLineHeadIndent = 30; //调整全部文字的缩进像素 paragraph.headIndent = 10; // 添加段落设置 [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, text.length)]; // label 设置富文本 label.attributedText = attributedString;
3、超链接显示,注:但无法点击
// 设置 URL NSURL *url = [NSURL URLWithString:@"http://blog.csdn.net/buyu03/article/details/50807940"]; [attributedString addAttribute:NSLinkAttributeName value:url range:NSMakeRange(6, 6)]; label.attributedText = attributedString;
4、切圆角,设置边线
// 切圆角 label.layer.cornerRadius = 10; label.layer.masksToBounds = YES; // 设置边界线颜色 label.layer.borderColor = [UIColor blackColor].CGColor; // 设置边界线宽度 label.layer.borderWidth = 1.0f;
5、文字过长时显示方式
label.lineBreakMode = UILineBreakModeMiddleTruncation;//截去中间 // typedef enum { // UILineBreakModeWordWrap = 0, // UILineBreakModeCharacterWrap, // UILineBreakModeClip,//截去多余部分 // UILineBreakModeHeadTruncation,//截去头部 // UILineBreakModeTailTruncation,//截去尾部 // UILineBreakModeMiddleTruncation,//截去中间 // } UILineBreakMode;
1 0
- UILabel专题
- UILabel
- UILabel
- UILabel
- UILabel
- UILabel
- UILabel
- UILabel
- UILabel
- UILabel
- UILabel
- UILabel
- UILabel
- UILabel
- UIlabel
- UILabel
- UILabel
- UILabel
- 安卓开发之编辑框属性
- java访问C#webservice接口
- jenkins搭建自动化部署平台(svn+tomcat重启)
- sql优化大全
- 线程池相关
- UILabel专题
- Xcode调试总结
- C#中DataTable排序和检索
- 安卓开发遇到Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
- 什么是“无头系统”(Headless System)
- 如何禁用 VS2010 的 vcpkgsrv.exe 运行
- React-Native:删除线样式
- hdu 1325&&poj1308 Is It A Tree? 基础并查集★
- 数据库之第一张数据表