• iOS开发UI篇--UILabel


    一、简介

    •  UILabel类实现了一个只读文本视图。您可以使用这个类来画一个或多个行静态文本,比如你可能使用确定的其他部分的用户界面。UILabel类支持既简单又复杂的样式标签的文本,还可以控制外观,比如标签是否使用一个影子或吸引了一大亮点。
    •   在iOS程序中,看的见、摸得着的,都是UIView的子类。UILabel是一个用于显示文字信息的标签视图类,即UIView的子类。

      

    以下是关于UILabel的官方网址:https://developer.apple.com/reference/uikit/uilabel

     二、关于UILabel的苹果官方API的解释 

         

    一、访问文本属性

    @property(nullable, nonatomic,copy)   NSString           *text; // 设置显示文字, 默认是空的          

    @property(null_resettable, nonatomic,strong) UIFont      *font;            //  设置字体(系统字体默认17号字体)

    @property(null_resettable, nonatomic,strong) UIColor     *textColor;       //  字体的颜色(默认是黑色)

    @property(nullable, nonatomic,strong) UIColor            *shadowColor;     //   阴影的颜色

    @property(nonatomic)        CGSize             shadowOffset;    // 阴影的偏移量,默认是 CGSizeMake(0, -1) -- a top shadow  

    @property(nonatomic)        NSTextAlignment    textAlignment;   //  对齐方式,默认是左对齐

    @property(nonatomic)        NSLineBreakMode    lineBreakMode(换行方式);   // 默认是 NSLineBreakByTruncatingTail. 用于单和多行文本  换行方式

    二、富文本

    是带有属性的字符串

     the underlying attributed string drawn by the label, if set, the label ignores the properties above.//底层带属性字符串的标签,如果设置,可以忽略了上面的属性。相当于标签的另一种呈现方法。

    @property(nullable, nonatomic,copy)   NSAttributedString *attributedText NS_AVAILABLE_IOS(6_0);  // default is nil

      

    关于富文本的使用会单独做介绍。

    三、高亮属性

     the 'highlight' property is used by subclasses for such things as pressed states. it's useful to make it part of the base class as a user property

    //设置高亮  

    @property(nullable, nonatomic,strong)               UIColor *highlightedTextColor; // 高亮 状态的字体颜色

    @property(nonatomic,getter=isHighlighted) BOOL     highlighted;          //是否高亮

    @property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled;  // default is NO  设置是否能与用户进行交互,默认没有打开交互 

    @property(nonatomic,getter=isEnabled)                BOOL enabled;                 // default is YES. changes how the label is drawn  设置label中的文字是否可变,默认值是YES

      

    四、换行

     this determines the number of lines to draw and what to do when sizeToFit is called. default value is 1 (single line). A value of 0 means no limit

    •  这决定的行数和调用sizeToFit时该做什么,默认值是1行。0值意味着没有限制

    // if the height of the text reaches the # of lines or the height of the view is less than the # of lines allowed, the text will be

    // truncated using the line break mode.

    @property(nonatomic) NSInteger numberOfLines;//换行

    五、自适应

      these next 3 property allow the label to be autosized to fit a certain width by scaling the font size(s) by a scaling factor >= the minimum scaling factor

    • 下面3个属性,允许标签通过一个比例因子(s)> =最低比例因子,自适应缩放字体大小得到合理的宽度

    @property(nonatomic) BOOL adjustsFontSizeToFitWidth;         // default is NO  设置字体大小适应label宽度     默认NO     

    @property(nonatomic) UIBaselineAdjustment baselineAdjustment; // default is UIBaselineAdjustmentAlignBaselines 

     //如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为

    @property(nonatomic) CGFloat minimumScaleFactor NS_AVAILABLE_IOS(6_0); // default is 0.0

      Tightens inter-character spacing in attempt to fit lines wider than the available space if the line break mode is one of the truncation modes before starting to truncate.

    • 收紧inter-character间距在尝试适应更广泛的比可用空间如果换行模式是一种截断模式之前开始截断。

    // The maximum amount of tightening performed is determined by the system based on contexts such as font, line width, etc.

    @property(nonatomic) BOOL allowsDefaultTighteningForTruncation NS_AVAILABLE_IOS(9_0); // default is NO

      override points. can adjust rect before calling super.

    六、Drawing and Positioning Overrides 图和定位覆盖

    // label has default content mode of UIViewContentModeRedraw

    - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;

    - (void)drawTextInRect:(CGRect)rect;

    七、auto layou相关的代码(做屏幕适配的时候用)

     Support for constraint-based layout (auto layout)

      

    // If nonzero, this is used when determining -intrinsicContentSize for multiline labels

    @property(nonatomic) CGFloat preferredMaxLayoutWidth NS_AVAILABLE_IOS(6_0);

    八、已经废弃不用的

    @property(nonatomic) CGFloat minimumFontSize NS_DEPRECATED_IOS(2_0, 6_0) __TVOS_PROHIBITED; // deprecated - use minimumScaleFactor. default is 0.0

    // Non-functional.  Hand tune by using NSKernAttributeName to affect tracking, or consider using the allowsDefaultTighteningForTruncation property.

    @property(nonatomic) BOOL adjustsLetterSpacingToFitWidth NS_DEPRECATED_IOS(6_0,7_0) __TVOS_PROHIBITED;

    @end

    NS_ASSUME_NONNULL_END

    三、代码例子

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view, typically from a nib.

        

        self.view.backgroundColor = [UIColor whiteColor];

        

        [self createLabel];

    }

    - (void)createLabel{

        

        //1. 创建一个标签 设置大小, 位置

        

        UILabel *label = [[UILabel alloc] init];

        

        label.frame = CGRectMake(0, 0, 200, 400);

        

        // UILabel是继承自UIView的, 所有的view都是矩形

        

        // CGRect 是一个结构体, 里面包含了一个点, 和一个大小  点==用来确定显示的位置,表示一个视图左上角的坐标        大小==确定范围(宽高)

        

        

        

        //2. 设置背景颜色

        

        // 默认是透明的

        

        label.backgroundColor = [UIColor redColor];

        

        //3. 设置显示文字

        

        NSString *text = @"hello world";

        

        label.text = text;

        

        //4. 设置显示内容的颜色

        

        label.textColor = [UIColor blueColor];

             

        //5. 对齐方式

        

        /**

         

         NSTextAlignmentLeft      = 0,  // 居左对齐

         

         NSTextAlignmentCenter    = 1,  // 居中对齐

         

         NSTextAlignmentRight           // 居右对齐

         

         */

        

        label.textAlignment = NSTextAlignmentCenter;

        

        //6.设置字体(UIFont)(label默认的字体大小是17)

        

        label.font = [UIFont systemFontOfSize:30];      // 系统字体

        

        label.font = [UIFont boldSystemFontOfSize:30];  // 系统字体 加粗

        

        label.font = [UIFont italicSystemFontOfSize:30];// 系统字体 斜体(不支持中文...)

         

        // 自定义字体

        

        // 可以获取当前设备支持的所有的字体

        

        NSArray *allfonts = [UIFont familyNames];

        

        NSLog(@"%@", allfonts);

        

        // 使用自定义字体(非系统的字体)(也不支持中文)

        

        label.font = [UIFont fontWithName:@"Futura" size:30];

        

    //    7. 字的阴影(shadow)

        

        label.shadowColor = [UIColor greenColor];   // 设置阴影的颜色

        

        label.shadowOffset = CGSizeMake(2, 2);      // 设置阴影的偏移量

        

        

    //    8. 换行

        

        // 最大显示的行数(默认是1)

        

        // Δ 这里需要去理解一下

        

        //  1. 当label的内容足够多, 而且, label足够高, 最大显示numberOfLines行

        

        //  2. 当label的内容足够多, 但是, label的高度不够高, 最大显示label能容纳多少行

        

        //  3. 当label的内容不够多, 能显示多少行, 显示多少行

        

        // 0 表示不限制最大行数

        

        label.numberOfLines = 0;

             

    //    9. 换行方式

        

        /**

         

         NSLineBreakByWordWrapping = 0, // 以单词换行

         

         NSLineBreakByCharWrapping,      // 以字母换行(iOS 7无效)

         

         NSLineBreakByClipping,          //

         

         NSLineBreakByTruncatingHead,    // 以单词换行, 省略最后一行开头

         

         NSLineBreakByTruncatingTail,    // 以单词换行, 省略最后一行结尾

         

         NSLineBreakByTruncatingMiddle   // 以单词换行, 省略最后一行中间

         

         */

        

        label.lineBreakMode = NSLineBreakByTruncatingTail;

        

    //    10.设置是否能与用户进行交互

        label.userInteractionEnabled = YES;

        //  给label添加一个点击手势

        [label addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(test)]];

        

    //    11.设置label中的文字是否可变,默认值是YES

        label.enabled = YES;

    //    12.设置字体大小适应label宽度     默认NO

        

        label.adjustsFontSizeToFitWidth = YES;

        

    //    13.如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为

         

        label.baselineAdjustment = UIBaselineAdjustmentNone;

        typedef enum {

            UIBaselineAdjustmentAlignBaselines,

            UIBaselineAdjustmentAlignCenters,

            UIBaselineAdjustmentNone,

        } UIBaselineAdjustment;

    //    14.设置高亮

        label.highlighted = YES;

        label.highlightedTextColor = [UIColor orangeColor];

        

        

    //     15.label根据文字改变高度

        

        [label sizeToFit];

         

    //    16. 根据文字的内容, 文字的大小, 换行.. 来获取需要占用的大小

        

        // 1. 文字需要显示的字体

        

        // 2. 最大显示的宽度

        

        CGSize size = [text sizeWithFont:label.font constrainedToSize:CGSizeMake(label.frame.size.width, FLT_MAX) lineBreakMode:NSLineBreakByWordWrapping];

         NSLog(@"%@", NSStringFromCGSize(size));

        

        label.frame = CGRectMake(0, 0, size.width, size.height);

         

    #pragma mark=====UILabel的另一种展现形式--富文本====

         

    //可以设置其中某段内容的显示样式

    // 1. string 表示要显示的内容

    // 2. attributes 表示这个字符串的属性

        // NSAttributeString只能整体设置属性

        // NSMutableAttributeString  可变, 可以单独给某一段设置属性

        NSMutableAttributedString *att = [[NSMutableAttributedString alloc]initWithString:text];

        /*  常用属性key:

         NSFontAttributeName : 设置字体的大小

         

         NSForegroundColorAttributeName : 设置字体的镂空颜色

         

         NSBackgroundColorAttributeName : 设置字体的背景颜色

         

         NSParagraphStyleAttributeName: 段落样式属性

         

         */

         

        

     // 1. 实例化一个段落样式

        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];

        

      // 2. 设置行间距

        style.lineSpacing = 5;

         

    // 3. 给att添加一个属性

     //  给某一个范围的内容添加属性,range:就是 NSAttributeString和NSMutableAttributeString 区别所在

        [att addAttributes:@{

                              

                             NSForegroundColorAttributeName:[UIColor purpleColor],

                             

                             NSFontAttributeName:[UIFont systemFontOfSize:20],

                             

                             NSBackgroundColorAttributeName: [UIColor greenColor],

                             

                             NSParagraphStyleAttributeName:style

                         

                             } range:NSMakeRange(0, text.length)];

             

        label.attributedText = att;

         

        [self.view addSubview:label];

         

    }

    - (void)test{

        NSLog(@" just a test");

    }

    博主的话

    最近又写了个demo,点击标签可以调用相册。地址:https://i.cnblogs.com/Files.aspx。 
     

  • 相关阅读:
    使用dfs求解全排列
    并查集
    Best Cow Line
    Saruman's Army
    Fence Repair
    Laking Counting
    淘宝商品定向爬取
    爬取股票信息
    python中的正则表达式的使用

  • 原文地址:https://www.cnblogs.com/dreamDeveloper/p/5947313.html
Copyright © 2020-2023  润新知