美文网首页
UILabel常用的类别

UILabel常用的类别

作者: 猪队友小L | 来源:发表于2017-08-19 11:10 被阅读140次

#import <UIKit/UIKit.h>

@interface UILabel (Extension)

+ (UILabel *)labelWithFrame:(CGRect)frame
                       text:(NSString *)text
                  textColor:(UIColor *)color
              textAlignment:(NSTextAlignment)alignment
                       font:(UIFont *)font;

//设置Label的行间距
- (void)setLineSpaceWithString:(CGFloat)lineSpace;

@end


#import "UILabel+Extension.h"

@implementation UILabel (Extension)

+ (UILabel *)labelWithFrame:(CGRect)frame
                       text:(NSString *)text
                  textColor:(UIColor *)color
              textAlignment:(NSTextAlignment)alignment
                       font:(UIFont *)font
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.textColor = color;
    label.textAlignment = alignment;
    label.font = font;
    return label;
}

- (void)setLineSpaceWithString:(CGFloat)lineSpace
{
    NSMutableAttributedString *attributedString =
    [[NSMutableAttributedString alloc] initWithString:self.text];
    NSMutableParagraphStyle *paragraphStyle =  [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpace];
    
    //调整行间距
    [attributedString addAttribute:NSParagraphStyleAttributeName
                             value:paragraphStyle
                             range:NSMakeRange(0, [self.text length])];
    self.attributedText = attributedString;
}


@end

相关文章

网友评论

      本文标题:UILabel常用的类别

      本文链接:https://www.haomeiwen.com/subject/pmhtdxtx.html