IOS(UILabel)

来源:互联网 发布:java手游傲世 编辑:程序博客网 时间:2024/05/16 13:39

前段时间已经把 Object-C 过了一遍了,现在要开始 IOS 开发的实战学习了。因为之前是做 .Net 开发的,所以,转过来的时间,还是有好多不适应和困惑的,特别是 C# -> Object-C 和  VS -> XCode,其中更有很多思想和操作都是不同的,没办法,只有一步步来了。

 
今天在调一些IOS中简单的控件,其中使用到了 Label(UILabel),其实 Label(UILabel) 还是蛮简单的,但是也是最常用的,所以就想先通过 深入了解 Label(UILabel) ,来开始接触 IOS 中的控件,毕竟控件有好多的属性和方法都是相同的~
 
官方文档:Apple  Label(UILabel) 
 
官方文档中,对 Label(UILabel) 描述比较重要的部分如下:
 
The UILabel class implements a read-only text view. You can use this class to draw one or multiple lines of static text, such as those you might use to identify other parts of your user interface. The base UILabel class provides control over the appearance of your text, including whether it uses a shadow or draws with a highlight. If needed, you can customize the appearance of your text further by subclassing. 
 
New label objects are configured to disregard user events by default. If you want to handle events in a custom subclass of UILabel, you must explicitly change the value of the userInteractionEnabled property to YES after initializing the object. 
 
通过这两段话,我们能明白 Label(UILabel) 主要有以下特性:
 
1. 是用来显示文本内容的,且是 只读 的
 
2. 可以单行显示,也可以多行显示,但是要通过设置实现
 
3. 可以给内容增加一些 shadow、draws、highlight 等特效
 
4. 默认是不支持事件的,但是可以通过自定义使其支持
 
 
 
下面,来逐条分析下 Label(UILabel)  的属性(Property):
 
 
 1. text : Label(UILabel) 显示的文本, 可读写的属性,值类型为 NSString,默认值为 Nil
    
 

 2. font : text 的 font-family 和 font-size,可读写,值类型为 UIFont,默认值为 system font at a size of 17 

 

 3. textColor :  文本的颜色, 可读写的属性,值类型为 UIColor,默认值为 Black 
 
 
 4. textAlignment : 文本的对齐方式, 可读写的属性,值类型为 UITextAlignment,默认值为 UITextAlignmentLeft 

 
 5. lineBreakMode :  文本的换行与截断方式, 可读写的属性,值类型为 UILineBreakMode,默认值为 UILineBreakModeTailTruncation 
 

 6. enabled :   控件是否可用,感觉对于 Label 来说比较鸡肋,为 false 后,只是把颜色变灰而已,如果想要隐藏UILabel(Label),请用hidden 属性
 
//隐藏Label(UILabel):lblUserName[lblUserName setHidden:true];
 
 
 
 7. adjustsFontSizeToFitWidth :  是否根据 Label 宽度来自动调整 text 的大小,默认值为 Yes,bool 类型
 
 
 8. baselineAdjustment: text的基线位置,不是太长用,没搞懂~
 
 
 9. minimumFontSize :  text的最小大小,可以用这个 Property 来阻止 adjustsFontSizeToFitWidth 过于缩放
 
 
10. numberOfLines : 设置 text 的最大行,可以防止过高;设为 0 ,即行数不受限制
 
 
11. highlighted : 是否高亮(对于label 来说,就是改变text 的颜色),适合在交互的时间使用这个属性,如 button 被按下,然后改变 button text 的 颜色等,也是说,只有再Label 被按下的时间,比如,一个 tabelcell 里面又1 个label,当这个 tabelcell 被按下时,背景色改为蓝色,这时间,把label 的 highlighted 变成白色会体验更好
 
12. highlightedTextColor : 用何种颜色高亮,参考 highlighted 
 
 
13. shadowColor & shadowOffset : 设置文本的阴影颜色和阴影大小
 
 
14. userInteractionEnabled : 是否可以和用户进行交互,即是否可以响应时间,默认为 NO
 
 
 
UILabel 控制高度,有两个纬度,一个是 numberOfLines ,另外一个是通过 frame 来设置款高来实现;但是经过使用,发现numberOfLines 只有  0 和 1 比较有意义,即 自动行数 和 1 行显示,如若是 0,则要配合 设置 frame 来调整高度,即 numberOfLines 可以极端的说,就是用来控制 UILabel 是否为多行显示,其实用 IsMulitRows 来代替,感觉更好
 
 
示例代码:
 
复制代码
 NSString *txt = @"The UILabel class implements a read-only text view. You can use this class to draw one or multiple lines of static text, such as those you might use to identify other parts of your user interface. \n The base UILabel class provides support for both simple and complex styling of the label text. You can also control over aspects of appearance, such as whether the label uses a shadow or draws with a highlight. \n If needed, you can customize the appearance of your text further by subclassing.The default content mode of the UILabel class is UIViewContentModeRedraw. \n This mode causes the view to redraw its contents every time its bounding rectangle changes. \n You can change this mode by modifying the inherited contentMode property of the class.New label objects are configured to disregard user events by default. \n If you want to handle events in a custom subclass of UILabel, you must explicitly change the value of the userInteractionEnabled property to YES after initializing the object.";       learnLabel.text = txt;        UIFont *font = [UIFont systemFontOfSize:16];    learnLabel.font = font;        /*Test 1 设置 UIlabel 高度*/    learnLabel.numberOfLines =0;        CGSize txtSize = [txt sizeWithFont:font                       constrainedToSize:CGSizeMake(400, 200)                           lineBreakMode:UILineBreakModeCharacterWrap];        learnLabel.frame = CGRectMake(learnLabel.frame.origin.x, learnLabel.frame.origin.y, txtSize.width, txtSize.height);            /*Test 3 测试 Highlightcolor & highlight    learnLabel.highlighted = true;    learnLabel.textColor = [UIColor greenColor];    learnLabel.highlightedTextColor = [UIColor blueColor];    */        /*Test 4 测试 shadowColor & shadowOffset*/    learnLabel.font =[UIFont systemFontOfSize:34];    learnLabel.shadowColor = [UIColor greenColor];    learnLabel.shadowOffset = CGSizeMake(0.0,0.000001);    learnLabel.backgroundColor = [UIColor grayColor];        /*Test 2 设置自动换行、大小自适应    learnLabel.lineBreakMode = UILineBreakModeWordWrap;    learnLabel.numberOfLines = 0;    CGSize size = [learnLabel sizeThatFits:CGSizeMake(500, 0)];    CGRect rect = learnLabel.frame;    rect.size  =size;    learnLabel.frame = rect;    */
复制代码

0 0
原创粉丝点击