zoukankan      html  css  js  c++  java
  • IOS控件Label(UILabel)

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

     
    今天在调一些IOS中简单的控件,其中使用到了 Label(UILabel),其实 Label(UILabel) 还是蛮简单的,但是也是最常用的,所以就想先通过 深入了解 Label(UILabel) ,来开始接触 IOS 中的控件,毕竟控件有好多的属性和方法都是相同的~
     
     
    官方文档中,对 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. 
    本文禁止任何网站转载,严厉谴责那些蛀虫们。
    本文首发于,博客园,请搜索:博客园 - 寻自己,查看原版文章
    本文首发地址:IOS控件Label(UILabel) - www.cnblogs.com/xunziji/archive/2012/09/20/2695474.html
     
    通过这两段话,我们能明白 Label(UILabel) 主要有以下特性:
     
    1. 是用来显示文本内容的,且是 只读 的
     
    2. 可以单行显示,也可以多行显示,但是要通过设置实现
     
    3. 可以给内容增加一些 shadow、draws、highlight 等特效
     
    4. 默认是不支持事件的,但是可以通过自定义使其支持
     
    5. 在代码中控制换行,需要添加字符 '\n'
     
    6. 在 XCode 编辑模式下换行,需要按住 'Control + Enter' 按键
     
     
    本文禁止任何网站转载,严厉谴责那些蛀虫们。
    本文首发于,博客园,请搜索:博客园 - 寻自己,查看原版文章
    本文首发地址:IOS控件Label(UILabel) - www.cnblogs.com/xunziji/archive/2012/09/20/2695474.html
     
    下面,来逐条分析下 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];
     
    本文禁止任何网站转载,严厉谴责那些蛀虫们。
    本文首发于,博客园,请搜索:博客园 - 寻自己,查看原版文章
    本文首发地址:IOS控件Label(UILabel) - www.cnblogs.com/xunziji/archive/2012/09/20/2695474.html
     
     
     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 来代替,感觉更好
     
    本文禁止任何网站转载,严厉谴责那些蛀虫们。
    本文首发于,博客园,请搜索:博客园 - 寻自己,查看原版文章
    本文首发地址:IOS控件Label(UILabel) - www.cnblogs.com/xunziji/archive/2012/09/20/2695474.html
     
    示例代码:
     
     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;
        */
    本文禁止任何网站转载,严厉谴责那些蛀虫们。
    本文首发于,博客园,请搜索:博客园 - 寻自己,查看原版文章
    本文首发地址:IOS控件Label(UILabel)
  • 相关阅读:
    Java多线程系列 JUC锁03 公平锁(一)
    Java多线程系列 JUC锁02 互斥锁ReentrantLock
    JDBC课程3--通过ResultSet执行查询操作
    JDBC课程2--实现Statement(用于执行SQL语句)--使用自定义的JDBCTools的工具类静态方法,包括insert/update/delete三合一
    JDBC_通过DriverManager获得数据库连接
    JDBC课程1-实现Driver接口连接mysql数据库、通用的数据库连接方法(使用文件jdbc.properties)
    [终章]进阶20-流程控制结构--if/case/while结构
    MySQL进阶19--函数的创建(举例)/设置mysql的创建函数的权限/查看(show)/删除(drop) / 举4个栗子
    MySQL进阶18- 存储过程- 创建语句-参数模式(in/out/inout-对应三个例子) -调用语法-delimiter 结束标记'$'- 删除/查看/修改-三个练习
    SQL进阶17-变量的声明/使用(输出)--全局变量/会话变量--用户变量/局部变量
  • 原文地址:https://www.cnblogs.com/xunziji/p/2695474.html
Copyright © 2011-2022 走看看