@property(nullable, nonatomic,copy) NSString *text;
设置文字
@property(null_resettable, nonatomic,strong) UIFont *font;
设置字体
@property(null_resettable, nonatomic,strong) UIColor *textColor;
设置字体颜色
@property(nullable, nonatomic,strong) UIColor *shadowColor;
设置阴影颜色
@property(nonatomic) CGSize shadowOffset;
设置阴影偏移,默认是CGSizeMake(0, -1)
@property(nonatomic) NSTextAlignment textAlignment;
设置文字对齐方式。默认是NSTextAlignmentNatural,iOS 9之前默认是NSTextAlignmentLeft
typedef NS_ENUM(NSInteger, NSTextAlignment) {
NSTextAlignmentLeft = 0, //左对齐
#if TARGET_OS_IPHONE
NSTextAlignmentCenter = 1, //中间对齐
NSTextAlignmentRight = 2, //右对齐
#else /* !TARGET_OS_IPHONE */
NSTextAlignmentRight = 1, //右对齐
NSTextAlignmentCenter = 2, //中间对齐
#endif
NSTextAlignmentJustified = 3, //最后一行自然对齐
NSTextAlignmentNatural = 4, //默认对齐脚本
}
@property(nonatomic) NSLineBreakMode lineBreakMode;
换行模式。默认是NSLineBreakByTruncatingTail
typedef NS_ENUM(NSInteger, NSLineBreakMode) {
NSLineBreakByWordWrapping = 0, //当文字内容过长,以单词为单位,后面不显示
NSLineBreakByCharWrapping, //当文字内容过长,以字符为单位,后面不显示
NSLineBreakByClipping, //当文字内容过长,删除过长
NSLineBreakByTruncatingHead, //当文字内容过长,显示尾部,前面使用...显示
NSLineBreakByTruncatingTail, //当文字内容过长,显示头部,后面使用...显示
NSLineBreakByTruncatingMiddle //当文字内容过长,显示前后,中间使用...显示
}
@property(nullable, nonatomic,copy) NSAttributedString *attributedText
显示富文本。文本可以显示不同颜色,字体,下划线…
@property(nullable, nonatomic,strong) UIColor *highlightedTextColor;
高亮状态下显示的颜色
@property(nonatomic,getter=isHighlighted) BOOL highlighted;
获取和设置高亮状态
@property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled;
是否响应事件,默认为NO
@property(nonatomic,getter=isEnabled) BOOL enabled;
如果设置为No,则文字颜色会变暗,表明其是不可用的,默认值为YES。
@property(nonatomic) NSInteger numberOfLines;
获取和设置显示的行数
以下3个方法用于文字收缩
@property(nonatomic) BOOL adjustsFontSizeToFitWidth;
是否根据文本宽度改变字体大小
@property(nonatomic) UIBaselineAdjustment baselineAdjustment;
myLabel.adjustsFontSizeToFitWidth = YES;//调整基线位置需将此属性设置为YES
myLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines;
typedef NS_ENUM(NSInteger, UIBaselineAdjustment) {
UIBaselineAdjustmentAlignBaselines = 0, //文本最上端与Label中线对齐,默认值
UIBaselineAdjustmentAlignCenters,//文本中线与Label中线对齐
UIBaselineAdjustmentNone,//文本最下端与Label中线对齐
};
@property(nonatomic) CGFloat minimumScaleFactor
设置最小收缩比例,如果Label宽度小于文字长度时,文字进行收缩,收缩超过比例后,停止收缩。
@property(nonatomic) BOOL allowsDefaultTighteningForTruncation
是否允许在没有收缩再提前让字距变紧,默认NO
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;
计算UIlabel 随字体多行后的高度
- (void)drawTextInRect:(CGRect)rect;
绘制text到指定区域
需要重载此方法,然后由子类调用,重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了
@property(nonatomic) CGFloat preferredMaxLayoutWidth
这个属性是用来设置多行label的最大宽度的
当自动布局的时候约束这个label的时候这个属性会起作用
在自动布局添加约束中,若文本超过了指定的最大宽度的时候 文本会另起一行 从而增加了label的高度
@property(nonatomic) CGFloat minimumFontSize
废弃的方法。被minimumScaleFactor替代
@property(nonatomic) BOOL adjustsLetterSpacingToFitWidth
改变字母之间的间距来适应Label大小