iOS UILable的分类

在做项目的时候我们会经常遇到label的中划线/下划线/修改行行间距/修改字间距的问题
/// 中划线
/// @param text 字符
/// @param color 颜色
-(void)setMiddlelineStyleSingle:(NSString *)text color:(UIColor *)color{
//添加中划线
if (!LMStringIsEmpty(text))
{
NSDictionary * centerAttribtDic = @{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle],NSForegroundColorAttributeName:color};
NSMutableAttributedString * centerAttr = [[NSMutableAttributedString alloc] initWithString:text attributes:centerAttribtDic];
self.attributedText = centerAttr;
}
}
/// 下划线
/// @param text 字符
/// @param color 颜色
-(void)setUnderlineStyleSingle:(NSString )text color:(UIColor )color {
//添加下划线
if (!LMStringIsEmpty(text))
{
NSDictionary * underAttribtDic = @{NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle],NSForegroundColorAttributeName:color};
NSMutableAttributedString * underAttr = [[NSMutableAttributedString alloc] initWithString:text attributes:underAttribtDic];
self.attributedText = underAttr;
}
}
/

  • 改变行间距
    */
  • (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space {
    NSString *labelText = label.text;
    if (labelText){
    NSMutableAttributedString attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
    NSMutableParagraphStyle paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:space];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    label.attributedText = attributedString;
    [label sizeToFit];
    }
    }
    /
  • 改变字间距
    */
  • (void)changeWordSpaceForLabel:(UILabel *)label WithSpace:(float)space {
    NSString *labelText = label.text;
    if (labelText){
    NSMutableAttributedString attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(space)}];
    NSMutableParagraphStyle paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    label.attributedText = attributedString;
    [label sizeToFit];
    }
    }
    /
  • 改变行间距和字间距
    */
  • (void)changeSpaceForLabel:(UILabel *)label withLineSpace:(float)lineSpace WordSpace:(float)wordSpace {
    NSString *labelText = label.text;
    if (labelText) {
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(wordSpace)}];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpace];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    label.attributedText = attributedString;
    [label sizeToFit];
    }
    }

你可能感兴趣的:(iOS UILable的分类)