修改UILabel的行间距、字间距、获取内容显示高度、快速创建UILabel
创建UILabel类
.h文件
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interfaceUILabel(Category)
/// 快速创建label
/// @param frame设置label的frame
/// @paramtext 显示内容
/// @param textColor 文字颜色
/// @param font文字字体及大小
/// @param alignment内容对其方式<0/1/2>
+ (instancetype)initWithFrame:(CGRect)frame
text:(NSString*)text
textColor:(UIColor*)textColor
font:(UIFont*)font
alignment:(NSInteger)alignment;
/// 计算label的内容显示的高度
/// @param label需要计算的label
+ (CGFloat)calculateHeightForLabel:(UILabel *)label;
/// 改变label的行间距
/// @param label需要改变的label
/// @paramspace 行间距
+ (void)changeLineSpaceForLabel:(UILabel *)label
withSpace:(CGFloat)space;
/// 改变label的字间距
/// @param label需要改变的label
/// @paramspace 字间距
+ (void)changeWordSpaceForLabel:(UILabel *)label
withSpace:(CGFloat)space;
/// 改变label的行间距、字间距
/// @param label需要改变的label
/// @param lineSpace 行间距
/// @param wordSpace 字间距
+ (void)changeSpaceForLabel:(UILabel *)label
withLineSpace:(CGFloat)lineSpace
withWordSpace:(CGFloat)wordSpace;
/// 改变label某位置的字体颜色
/// @param label需要改变的label
/// @paramcolor 颜色
/// @paramrange 位置
+ (void)changeTextColorForLabel:(UILabel *)label
color:(UIColor*)color
range:(NSRange)range;
@end
NS_ASSUME_NONNULL_END
.m文件
#import "UILabel+Category.h"
@implementation UILabel (Category)
/// 快速创建label
/// @param frame设置label的frame
/// @paramtext 显示内容
/// @param textColor 文字颜色
/// @param font文字字体及大小
/// @param alignment内容对其方式<0/1/2>
+ (instancetype)initWithFrame:(CGRect)frame
text:(NSString*)text
textColor:(UIColor*)textColor
font:(UIFont*)font
alignment:(NSInteger)alignment {
UILabel*label = [[UILabel alloc]initWithFrame:frame];
label.text= text;
if(textColor !=nil) {
label.textColor= textColor;
}
//默认使用系统字体
label.font= font;
//默认自动换行
label.numberOfLines=0;
label.textAlignment= alignment;
//默认使用尾部省略
label.lineBreakMode = NSLineBreakByTruncatingTail;
return label;
}
/// 计算label的内容显示的高度
/// @param label需要计算的label
+ (CGFloat)calculateHeightForLabel:(UILabel *)label {
CGRect rect = [label.text boundingRectWithSize:CGSizeMake(label.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:label.font} context:nil];
return rect.size.height;
}
/// 改变label的行间距
/// @param label需要改变的label
/// @paramspace 行间距
+ (void)changeLineSpaceForLabel:(UILabel *)label
withSpace:(CGFloat)space {
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:label.text];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[stylesetLineSpacing:space];
[attraddAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, label.text.length)];
label.attributedText= attr;
[label sizeToFit];
}
/// 改变label的字间距
/// @param label需要改变的label
/// @paramspace 字间距
+ (void)changeWordSpaceForLabel:(UILabel *)label
withSpace:(CGFloat)space {
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:label.text attributes:@{NSKernAttributeName:@(space)}];
NSParagraphStyle *style = [[NSParagraphStyle alloc] init];
[attr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, label.text.length)];
label.attributedText= attr;
[label sizeToFit];
}
/// 改变label的行间距、字间距
/// @param label需要改变的label
/// @param lineSpace 行间距
/// @param wordSpace 字间距
+ (void)changeSpaceForLabel:(UILabel *)label
withLineSpace:(CGFloat)lineSpace
withWordSpace:(CGFloat)wordSpace {
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:label.text attributes:@{NSKernAttributeName:@(wordSpace)}];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[stylesetLineSpacing:lineSpace];
[attr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, label.text.length)];
label.attributedText= attr;
[label sizeToFit];
}
/// 改变label某位置的字体颜色
/// @param label需要改变的label
/// @paramcolor 颜色
/// @paramrange 位置
+ (void)changeTextColorForLabel:(UILabel *)label
color:(UIColor*)color
range:(NSRange)range {
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:label.text];
[attraddAttribute:NSForegroundColorAttributeName value:color range:range];
label.attributedText= attr;
}
@end
网友评论