一、概述
1. 说明
UILabel 是 iOS 开发中常用的控件,也是最基本的一个控件,通过 UILabel 可以系那是一段文本
2. 属性
1)设置显示内容;默认为 nil
@property(nullable,nonatomic,copy) NSString * text;
2)设置字体大小;默认为 nil,采用 系统字体,17号
@property(null_resettable,nonatomic,strong)UIFont * font;
3)设置文本颜色;默认为 nil,文本为黑色
@property(null_resettable,nonatomic,strong)UIColor * textColor;
4)设置阴影颜色;默认为 nil
@property(nullable,nonatomic,strong)UIColor * shadowColor;
5)设置阴影偏移量;默认为 CGSizeMake(0, -1)
@property(nonatomic) CGSize shadowOffset;
6)设置文本对齐方式;默认为 NSTextAlignmentNatural,在 iOS 9之前默认为 NSTextAlignmentLeft
@property(nonatomic) NSTextAlignment textAlignment;
NSTextAlignment 枚举如下 :
typedef NS_ENUM(NSInteger, NSTextAlignment) {
NSTextAlignmentLeft = 0, // 左对齐
NSTextAlignmentCenter = 1, // 居中
NSTextAlignmentRight = 2, // 右对齐
NSTextAlignmentJustified = 3, // 文本的最后是自然对齐
NSTextAlignmentNatural = 4, // 遵循 app 的设置
}
@property(nonatomic) NSLineBreakMode lineBreakMode;
NSLineBreakMode枚举如下 :
typedef NS_ENUM(NSInteger, NSLineBreakMode) {
NSLineBreakByWordWrapping = 0, // 以单词为显示单位显示,后面部分省略不显示。
NSLineBreakByCharWrapping, // 以字符为显示单位显示,后面部分省略不显示
NSLineBreakByClipping, // 剪切与文本宽度相同的内容长度,后半部分被删除
NSLineBreakByTruncatingHead, // 以单词为单位换行。如果是单行,则开始部分有省略号。如果是多行,则中间有省略号,省略号后面有4个字符
NSLineBreakByTruncatingTail, // 以单词为单位换行。无论是单行还是多行,都是末尾有省略号
NSLineBreakByTruncatingMiddle // 以单词为单位换行。无论是单行还是多行,都是中间有省略号
}
@property(nullable,nonatomic,copy) NSAttributedString * attributedText;
9)设置高亮时的文本颜色;默认为 nil
@property(nullable,nonatomic,strong) UIColor * highlightedTextColor;
10)设置是否是高亮状态;默认为 NO
@property(nonatomic,getter=isHighlighted)BOOL highlighted;
11)设置是否可以响应用户事件,例如 触摸事件
@property(nonatomic,getter=isUserInteractionEnabled)BOOL userInteractionEnabled;
12)设置是否处于激活状态;默认为 YES,如果为 NO,则表示没有激活,文本变暗,此时向 label 设置颜色是无效的
@property(nonatomic,getter=isEnabled) BOOL enabled;
13)设置显示的行数(即自动换行功能);默认为 1,设置为 0 时,可按照内容自动调整行数
@property(nonatomic)NSInteger numberOfLines;
14)设置字体大小是否随 label 的宽度变化而变化,即文本内容会自动调整大小以便能够在 label 中全部显示,当 numberOfLines 为 0 时没有用;只有在 numberOfLines 为 1 时才有用
@property(nonatomic)BOOL adjustsFontSizeToFitWidth;
15)设置基准线的位置;默认为 UIBaselineAdjustmentAlignBaselines;只有在 numberOfLines 为 1 时才有用
@property(nonatomic)UIBaselineAdjustment baselineAdjustment;
UIBaselineAdjustment 枚举如下 :
typedefNS_ENUM(NSInteger, UIBaselineAdjustment) {
UIBaselineAdjustmentAlignBaselines =0, // 文本最高端与 label 中线对齐
UIBaselineAdjustmentAlignCenters, // 文本中线与 label 中线对齐
UIBaselineAdjustmentNone, // 文本最低端与 label 中线对齐
};
@property(nonatomic)CGFloat minimumScaleFactor;
17)这个属性是用来设置多行label的最大宽度的;当自动布局的时候约束这个label的时候这个属性会起作用;在自动布局添加约束中,若文本超过了指定的最大宽度的时候文本会另起一行从而增加了label的高度
@property(nonatomic)CGFloat preferredMaxLayoutWidth;
18)这两个方法是 UILabel 的子类需要重写的,不直接调用
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;
- (void)drawTextInRect:(CGRect)rect;