转自:http://blog.csdn.net/zhaopenghhhhhh/article/details/16331041

·UILable是iPhone界面最基本的控件,主要用来显示文本信息。
·常用属性和方法有:
1、创建
CGRect rect = CGRectMake(100, 200, 50, 50);
UILabel *label = [[UILabel alloc] initWithFrame:rect];
2、text //设置和读取文本内容,默认为nil
label.text = @”文本信息”; //设置内容
NSLog(@”%@”, label.text); //读取内容
3、textColor //设置文字颜色,默认为黑色
lable.textColor = [UIColor redColor];
4、font //设置字体大小,默认17
label.font = [UIFont systemFontOfSize:20]; //⼀一般方法
label.font = [UIFont boldSystemFontOfSize:20]; //加粗方法
label.font = [UIFont fontWithName:@"Arial" size:16]; //指定
字体的方法
//还有⼀一种从外部导入字体的方法。
5、textAlignment //设置标签文本对齐方式。
label.textAlignment = NSTextAlignmentCenter; //还有
NSTextAlignmentLeft、 NSTextAlignmentRight.
6、numberOfLines //标签最多显示行数,如果为0则表示多行。
label.numberOfLines = 2;
7、enabled //只是决定了Label的绘制方式,将它设置
为NO将会使文本变暗,表示它没有激活,这时向它设置颜色值是无效的。
label.enable = NO;
8、highlighted //是否高亮显示
label.highlighted = YES;
label.highlightedTextColor = [UIColor orangeColor]; //高亮
显示时的文本颜色
9、ShadowColor //设置阴影颜色 
[label setShadowColor:[UIColor blackColor]];
10、ShadowOffset //设置阴影偏移量
[label setShadowOffset:CGSizeMake(-1, -1)];
11、baselineAdjustment //如果adjustsFontSizeToFitWidth属性设
置为YES,这个属性就来控制文本基线的行为。
label.baselineAdjustment = UIBaselineAdjustmentNone;
UIBaselineAdjustmentAlignBaselines = 0,默认,文本最上端与中线对齐。
UIBaselineAdjustmentAlignCenters,  文本中线与label中线对齐。
UIBaselineAdjustmentNone, 文本最低端与label中线对齐。
12、Autoshrink //是否自动收缩
Fixed Font Size 默认,如果Label宽度小于文字长度时时,文字大小不自动缩放
minimumScaleFactor 设置最小收缩比例,如果Label宽度小于文字长度时,文字
进行收缩,收缩超过比例后,停止收缩。
minimumFontSize 设置最小收缩字号,如果Label宽度小于文字长度时,文字字号
减小,低于设定字号后,不再减小。//6.0以后不再使用了。
label.minimumScaleFactor = 0.5;
13、adjustsLetterSpacingToFitWidth //改变字母之间的间距来适应Label大小
myLabel.adjustsLetterSpacingToFitWidth = NO;
14、 lineBreakMode //设置文字过长时的显示格式             
label.lineBreakMode = NSLineBreakByCharWrapping;以字符为显示单位显
示,后面部分省略不显示。
label.lineBreakMode = NSLineBreakByClipping;剪切与文本宽度相同的内
容长度,后半部分被删除。
label.lineBreakMode = NSLineBreakByTruncatingHead;前面部分文字
以……方式省略,显示尾部文字内容。
label.lineBreakMode = NSLineBreakByTruncatingMiddle;中间的内容
以……方式省略,显示头尾的文字内容。
label.lineBreakMode = NSLineBreakByTruncatingTail;结尾部分的内容
以……方式省略,显示头的文字内容。
label.lineBreakMode = NSLineBreakByWordWrapping;以单词为显示单位显
示,后面部分省略不显示。
15、 adjustsFontSizeToFitWidth //设置字体大小适应label宽度  
label.adjustsFontSizeToFitWidth = YES;
16、attributedText:设置标签属性文本。
NSString *text = @"first";
NSMutableAttributedString *textLabelStr =
[[NSMutableAttributedString alloc]
initWithString:text];
[textLabelStr
setAttributes:@{NSForegroundColorAttributeName :
[UIColor lightGrayColor], NSFontAttributeName :
[UIFont systemFontOfSize:17]} range:NSMakeRange(11,
10)];
label.attributedText = textLabelStr;
17、竖排文字显示每个文字加一个换行符,这是最方便和简单的实现方式。
label.text = @"请\n竖\n直\n方\n向\n排\n列";
label.numberOfLines = [label.text length];

18、计算UIlabel 随字体多行后的高度
CGRect bounds = CGRectMake(0, 0, 200, 300);
heightLabel = [myLabel textRectForBounds:bounds
limitedToNumberOfLines:20]; //计算20行后的Label的Frame
NSLog(@"%f",heightLabel.size.height);
19、UILabel根据字数多少自动实现适应高度
UILabel *msgLabel = [[UILabel alloc]
initWithFrame:CGRectMake(15, 45, 0, 0)];
msgLabel.backgroundColor = [UIColor lightTextColor];
[msgLabel setNumberOfLines:0];
msgLabel.lineBreakMode = UILineBreakModeWordWrap;
msgLabel.font = [UIFont fontWithName:@"Arial" size:12];
CGSize size = CGSizeMake(290, 1000);
msgLabel.text = @"获取到的deviceToken,我们可以通过webservice服务提
交给.net应用程序,这里我简单处理,直接打印出来,拷贝到.net应用环境中使
用。";
CGSize msgSie = [msgLabel.text sizeWithFont:fonts
constrainedToSize:size];
[msgLabel setFrame:CGRectMake(15, 45, 290, msgSie.height)];

20、渐变字体Label
UIColor *titleColor = [UIColor colorWithPatternImage:[UIImage
imageNamed:@"btn.png"]];
NSString *title = @"Setting";
UILabel *titleLabel = [[UILabel alloc]
initWithFrame:CGRectMake(0, 0, 80, 44)];
titleLabel.textColor = titleColor;
titleLabel.text = title;
titleLabel.font = [UIFont boldSystemFontOfSize:20];
titleLabel.backgroundColor = [UIColor clearColor];
[self.view addSubview:titleLabel];
[titleLabel release];
21、Label添加边框
titleLabel.layer.borderColor = [[UIColor grayColor] CGColor];
titleLabel.layer.borderWidth = 2;

UILabel 详解的更多相关文章

  1. UILabel详解

    // ----------------------UILabel--------------------------- UILabel *label = [[UILabel alloc] initWi ...

  2. iOS UILabel详解

    1.创建 CGRect rect = CGRectMake(100, 200, 50, 50); UILabel *label = [[UILabel alloc] initWithFrame:rec ...

  3. IOS开发学习笔记(1)-----UILabel 详解

    1. [代码][C/C++]代码     //创建uilabelUILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, ...

  4. 详解UILabel的adjustsFontSizeToFitWidth值

    详解UILabel的adjustsFontSizeToFitWidth值 UILabel有一个属性值,叫adjustsFontSizeToFitWidth,看着名字就知道,他是用来让文字自动适应UIL ...

  5. iPhone应用开发 UITableView学习点滴详解

    iPhone应用开发 UITableView学习点滴详解是本文要介绍的内容,内容不多,主要是以代码实现UITableView的学习点滴,我们来看内容. -.建立 UITableView DataTab ...

  6. UI第六节——UINavigationController 详解

    1. UINavigationController 是一个容器类.里面盛放的是UIViewController. 容器的意思是,如果你不放入UIViewController,里面就是空的,什么也没有. ...

  7. 自动布局之autoresizingMask使用详解(Storyboard&Code)

    自动布局之autoresizingMask使用详解(Storyboard&Code) http://www.cocoachina.com/ios/20141216/10652.html 必须禁 ...

  8. UITableView 详解 ()

    (原本取至D了个L微信公众号) UITableView 详解 一.建立 UITableView DataTable = [[UITableView alloc] initWithFrame:CGRec ...

  9. UINavigationController使用详解

    UINavigationController使用详解 有一阵子没有写随笔,感觉有点儿手生.一个多月以后终于又一次坐下来静下心写随笔,记录自己的学习笔记,也希望能够帮到大家. 废话少说回到正题,UINa ...

随机推荐

  1. 已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭

    已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭 引用:   http://www.cnblogs.com/maxao/archive/2011/03/18/19881 ...

  2. 关于malloc和free函数的用法

    原文:http://blog.pfan.cn/vfdff/33507.html 个人总结 在C语言的学习中,对内存管理这部分的知识掌 握尤其重要!之前对C中的malloc()和free()两个函数的了 ...

  3. H5的新应用-指定视频的播放进度

    <!DOCTYPE html> <html> <head> <title>指定视频的播放进度</title> <meta http-e ...

  4. hdu_1181_变形课(dfs)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1181 题意:中文题,不解释 题解:直接DFS #include<cstdio> #incl ...

  5. cmstop中实例化controller_admin_content类传递$this,其构造方法中接收到的是--名为cmstop的参数--包含cmstop中所有属性

    主程序cmstop类,实例化controller_admin_content类(接收请求后拼接的).传递cmstop所有属性过去.controller_admin_content.构造方法中接收到名称 ...

  6. ECSTORE1.2系统更改后台密码

    <?php set_time_limit(0); error_reporting(E_ALL^E_NOTICE); ?> <META HTTP-EQUIV="content ...

  7. 剑指offer 旋转数组

    class Solution { public: int minNumberInRotateArray(vector<int> rotateArray) { //常规的遍历方法时间是O(N ...

  8. 上传下载文件, 同时部署在webapps下, 而不是项目下,防止重新部署tomcat, 上传文件消失

    前端上传 <a href='javascript:upload("+data[i].id+")' title='Upload Report'> <img src= ...

  9. JSP基本语法--Page指令 <%@page 属性=”内容“%>

    page指令语法:<%@page 属性=”内容“%> 常用:contentType,import,pageEncoding 例子,设置MIME属性,如果使用一些高版本的tomcat,可能自 ...

  10. Inno Setup入门(十九)——Inno Setup类参考(5)

    : Install Setup 2013-02-02 11:29 377人阅读 评论(0) 收藏 举报 单选按钮 单选按钮在安装中也很常见,例如同一个程序可以选择安装不同的性质的功能,例如选择32位或 ...