UIKit回顾总结----UILabel

本章内容:

  1. 使用代码创建Label。
  2. 常用的Label属性。
  3. 特殊要求的使用。

一、 使用代码创建Label

使用xib或者storeboard拖拽的就不介绍了,毕竟在项目中已经是几乎没用过了,而且在接触的项目开发中,都是用的纯代码开发,下面代码就是简单的创建一个Label展示:

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UILabel *textLabel;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _textLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 90, 40)];
    _textLabel.font = [UIFont systemFontOfSize:17];
    _textLabel.textColor = UIColor.blueColor;
    _textLabel.text = @"hello Label";
    [self.view addSubview:_textLabel];
}
@end

以上代码运行显示的结果如下:


UIKit回顾总结----UILabel_第1张图片
Simulator Screen Shot - iPhone 12 - 2021-05-18 at 09.46.05.png

二、常用的Label属性。

1.设置字体使用 font,font的类型是UIFont,下面这段代码设置的字体是系统默认字体,字体大小是17号。

_textLabel.font = [UIFont systemFontOfSize:17];

2.设置字体颜色用textColor,textColor的类型是UIColor。

_textLabel.textColor = UIColor.blueColor;

3.设置需要显示的文字。

 _textLabel.text = @"hello Label";

4.设置Label显示或隐藏,默认是显示。

    _textLabel.hidden = NO;

5.设置Label显示的行数,如果没有设置,默认是1行。单高度足够显示多行时也不会换行,设置-1表示无限行。

    _textLabel.numberOfLines = -1;

通常设置完以上属性,就可以正常显示出Label内容了。

三、 特殊要求的使用。

  1. 高亮状态显示highlightedTextColor,这个属性是设置文字在高亮状态下的颜色,通常配合UItableCell一起使用。
    _textLabel.highlightedTextColor = UIColor.orangeColor;
  1. 文字超出显示模式。
    在实际开发中,因为屏幕限制原因,要显示的文字过长,这就可能显示不了,在Label中,有几种设置超出设置宽度的显示模式,通过属性lineBreakMode设置。
    _textLabel.lineBreakMode = NSLineBreakByTruncatingTail;// Truncate at tail of line: "abcd..."

lineBreakMode是一个NSLineBreakMode枚举类型,里面有如下几种类型:

typedef NS_ENUM(NSInteger, NSLineBreakMode) {
    NSLineBreakByWordWrapping = 0,         // Wrap at word boundaries, default
    NSLineBreakByCharWrapping,        // Wrap at character boundaries
    NSLineBreakByClipping,        // Simply clip
    NSLineBreakByTruncatingHead,    // Truncate at head of line: "...wxyz"
    NSLineBreakByTruncatingTail,    // Truncate at tail of line: "abcd..."
    NSLineBreakByTruncatingMiddle    // Truncate middle of line:  "ab...yz"
} API_AVAILABLE(macos(10.0), ios(6.0), watchos(2.0), tvos(9.0));

可根据具体需求进行设置。

  1. 字体自动适配大小。
    有这么一个需求,Label需要适配字体的宽高,其中一种方式是指定字体,通过计算字体的宽高再设置Label的宽高,还有一种,就是Label自带的一种计算方式,使用sizeToFit方法。
    下面就进行一项对比,首先是没有使用sizeToFit的方法。
#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UILabel *textLabel;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _textLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 10, 40)];
    _textLabel.font = [UIFont systemFontOfSize:17];
    _textLabel.textColor = UIColor.blueColor;
    _textLabel.text = @"hello Label";
    _textLabel.textAlignment = NSTextAlignmentCenter;
    _textLabel.highlightedTextColor = UIColor.orangeColor;
//    [_textLabel sizeToFit];
    [self.view addSubview:_textLabel];

}
@end
UIKit回顾总结----UILabel_第2张图片
Simulator Screen Shot - iPhone 12 - 2021-05-18 at 10.33.32.png

可以看到,设置固定的宽度之后,因为要显示的内容过长,导致无法正常显示,这是设置了如果过长就以...进行代替。
下面看使用sizeToFit方法之后的效果:

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UILabel *textLabel;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _textLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 15, 40)];
    _textLabel.font = [UIFont systemFontOfSize:17];
    _textLabel.textColor = UIColor.blueColor;
    _textLabel.text = @"hello Label";
    _textLabel.textAlignment = NSTextAlignmentCenter;
    _textLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    _textLabel.highlightedTextColor = UIColor.orangeColor;
    [_textLabel sizeToFit];///调用sizeTofit方法
    [self.view addSubview:_textLabel];

}
@end
UIKit回顾总结----UILabel_第3张图片
Simulator Screen Shot - iPhone 12 - 2021-05-18 at 10.35.45.png

结果显示,可以完整的显示出内容,而不会受Label本身的size影响。

  1. 富文本显示。
    在上面我们直接调用属性,设置了Label的字体、颜色、对齐方式等,在Label中还有一个属性,即attributedText这个属性,是NSAttributedString富文本类型,通过key-value键值方式,也可以设置上面这些属性,只是设置的方式有所不同,而且它的功能非常强大,通常在使用的时候,使用的是它的子类NSMutableAttributedString进行设置。
#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UILabel *textLabel;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _textLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 150, 90)];
    _textLabel.attributedText = [self returnAttributedText:@"UILabel"];
    [self.view addSubview:_textLabel];

}
- (NSMutableAttributedString*)returnAttributedText:(NSString*)stg {
    NSMutableAttributedString *aStg = [[NSMutableAttributedString alloc]initWithString:stg];
    
    [aStg addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, stg.length)];
    [aStg addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, stg.length)];
    
    return aStg;
}

@end

UIKit回顾总结----UILabel_第4张图片
Simulator Screen Shot - iPhone 12 - 2021-05-18 at 11.23.46.png

写到这发现,富文本可以说的东西太多了,而且不局限于只是Label里面可以使用,所以打算接下来用一遍新的文章讲解回顾NSMutableAttributedString的使用,下一章object-c 回顾总结---NSMutableAttributedString的使用。

你可能感兴趣的:(UIKit回顾总结----UILabel)