@property(nullable, nonatomic,copy) NSString *text;
//设置字体(默认为17号)
@property(null_resettable, nonatomic,strong) UIFont *font;
//设置字体颜色(默认黑色)
@property(null_resettable, nonatomic,strong) UIColor *textColor;
//设置阴影颜色
@property(nullable, nonatomic,strong) UIColor *shadowColor;
//设置阴影偏移量
@property(nonatomic) CGSize shadowOffset;
//设置文字对齐方式
@property(nonatomic) NSTextAlignment textAlignment;
/*
typedef NS_ENUM(NSInteger, NSTextAlignment) {
NSTextAlignmentLeft = 0, //左对齐
NSTextAlignmentCenter = 1, // 居中
NSTextAlignmentRight = 2, // 右对齐
NSTextAlignmentJustified = 3, // 充满View来显示
NSTextAlignmentNatural = 4, // 根据显示的文字特性对齐
} NS_ENUM_AVAILABLE_IOS(6_0);
*/
//设置段落样式
@property(nonatomic) NSLineBreakMode lineBreakMode; // default is
/*
typedef NS_ENUM(NSInteger, NSLineBreakMode) {
NSLineBreakByWordWrapping = 0, // 在字边界处换行,默认
NSLineBreakByCharWrapping, // 在字符边界处换行
NSLineBreakByClipping, // 简单剪辑
NSLineBreakByTruncatingHead, // 截断在行首:"...wxyz"
NSLineBreakByTruncatingTail, // 截尾在行尾:"abcd..."
NSLineBreakByTruncatingMiddle // 截断行中间:“ab ... yz”
}
*/
//富文本,默认nil
@property(nullable, nonatomic,copy) NSAttributedString *attributedText;
//设置高亮颜色
@property(nullable, nonatomic,strong) UIColor *highlightedTextColor;
@property(nonatomic,getter=isHighlighted) BOOL highlighted;
//用户是否可交互
@property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled;
@property(nonatomic,getter=isEnabled) BOOL enabled;
//显示行数 默认1行
@property(nonatomic) NSInteger numberOfLines;
//设置基线
@property(nonatomic) UIBaselineAdjustment baseline;
/*
typedef NS_ENUM(NSInteger, UIBaselineAdjustment) {
UIBaselineAdjustmentAlignBaselines = 0, //默认,文本最上端与中线对齐。
UIBaselineAdjustmentAlignCenters, //文本中线与label中线对齐。
UIBaselineAdjustmentNone, //文本最低端与label中线对齐。
};
*/
//设置字体大小适应label宽度
@property(nonatomic) BOOL adjustsFontSizeToFitWidth;
//设置最小收缩比例
@property(nonatomic) CGFloat minimumScaleFactor;
//改变字母之间的间距来适应Label大小
@property(nonatomic) BOOL allowsDefaultTighteningForTruncation;
// 计算UILabel随字体多行后的高度
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;
- (void)drawTextInRect:(CGRect)rect;
//最大约束宽
@property(nonatomic) CGFloat preferredMaxLayoutWidth;
调整Label中text的行间距
NSString * textStr = @"这是测试UILabel行间距的text。这是测试UILabel行间距的text。\n这是测试UILabel行间距的text。\n这是测试UILabel行间距的text。这是测试UILabel行间距的text。这是测试UILabel行间距的text。这是测试UILabel行间距的text。";
UILabel * cLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, 280, 200)];
cLabel.numberOfLines = 0;
cLabel.font = [UIFont systemFontOfSize:16];
cLabel.textColor = [UIColor grayColor];
NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:textStr];
NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle1 setLineSpacing:8];
[attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [textStr length])];
[cLabel setAttributedText:attributedString1];
[cLabel sizeToFit];
[self.view addSubview:cLabel];