|
//
// TPLabel.m
// LYCoreLabelDemo
//
// Created by zhousan on 15/11/2.
// Copyright © 2015年 zhousan. All rights reserved.
//
#import "TPEmojiLabel.h"
@implementation TPEmojiLabel
- (id)initWithFrame:(CGRect)frame LineSpace:(CGFloat)lineSpace ParagraphSpacing:(CGFloat)paragraphSpacing {
self = [super initWithFrame:frame];
if (self) {
self.lineSpace = lineSpace;
self.paragraphSpacing = paragraphSpacing;
self.numberOfLines = 0;
}
return self;
}
- (void)setText:(NSString *)text {
// [super setText:text];
NSMutableString *string = [text mutableCopy];
// dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSMutableAttributedString *str = [NSMutableAttributedString stringWithText:string];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
// paragraphStyle.lineSpacing = self.lineSpace;// 字体的行间距
paragraphStyle.paragraphSpacing = self.paragraphSpacing;
CGFloat lineHeight = self.font.lineHeight;
CGFloat norLineSpace = lineHeight - 15;
paragraphStyle.lineSpacing = self.lineSpace-norLineSpace;
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
paragraphStyle,NSParagraphStyleAttributeName,
nil];
NSRange range = NSMakeRange(0, str.length);
[str addAttributes:attributes range:range];
// dispatch_async(dispatch_get_main_queue(), ^{
self.attributedText = str;
// });
// });
}
- (void)setTextTogetHeight:(NSString*)text{
NSMutableAttributedString *str = [NSMutableAttributedString stringWithText:[text mutableCopy]];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
CGFloat lineHeight = self.font.lineHeight;
CGFloat norLineSpace = lineHeight - TEXT_FOUR_LEVELSIZE;
paragraphStyle.lineSpacing = self.lineSpace-norLineSpace;// 字体的行间距
paragraphStyle.paragraphSpacing = self.paragraphSpacing;
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
paragraphStyle,NSParagraphStyleAttributeName,
nil];
NSRange range = NSMakeRange(0, str.length);
[str addAttributes:attributes range:range];
self.attributedText = str;
}
+ (CGFloat)heightForLabelWithFont:(UIFont *)font Width:(CGFloat)width Text:(NSString *)text LineSpace:(CGFloat)lineSpace ParagraphSpacing:(CGFloat)paragraphSpacing{
TPEmojiLabel *label = [[TPEmojiLabel alloc] initWithFrame:CGRectZero LineSpace:lineSpace ParagraphSpacing:paragraphSpacing];
label.font = font;
[label setTextTogetHeight:text];
CGFloat lineHeight = font.lineHeight;
CGFloat norLineSpace = lineHeight - TEXT_FOUR_LEVELSIZE;
CGFloat height = [label sizeThatFits:CGSizeMake(width, CGFLOAT_MAX)].height+lineSpace-norLineSpace;
label = nil;
return height;
}
- (void)addLineViewWithHeight:(CGFloat)height Color:(UIColor *)color{
CGFloat labelHeight = [self sizeThatFits:CGSizeMake(self.frame.size.width, CGFLOAT_MAX)].height;
CGFloat oneLine = self.font.lineHeight + self.lineSpace;
int numberLines = (labelHeight + self.lineSpace)/ oneLine;
for (int i=0; i<numberLines; i++) {
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, i*oneLine + self.font.lineHeight, self.frame.size.width, 1)];
lineView.backgroundColor = color;
[self addSubview:lineView];
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
|