标签:
- (void)awakeFromNib {
[self.descriptionLabel addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil];
[super awakeFromNib];
// Initialization code
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
self.descriptionLabel.attributedText = [[[self class] attributeWithString:self.descriptionLabel.text] copy];
}
+ (NSMutableAttributedString *)attributeWithString:(NSString *)string
{
NSMutableAttributedString *muAttrString = [[NSMutableAttributedString alloc] initWithString:string];
NSMutableParagraphStyle *paragtaphStyle = [[NSMutableParagraphStyle alloc] init];
paragtaphStyle.alignment = NSTextAlignmentJustified;
paragtaphStyle.paragraphSpacing = 11.0;
paragtaphStyle.paragraphSpacingBefore = 10.0;
paragtaphStyle.headIndent = 0.0;
paragtaphStyle.lineBreakMode = NSLineBreakByWordWrapping;
// paragtaphStyle.tailIndent = 30.0;
// paragtaphStyle.firstLineHeadIndent = self.descriptionLabel.font.pointSize * 2.0;
NSDictionary *dic = @{
NSForegroundColorAttributeName:ColorS(@"#585C67"),
NSFontAttributeName:FontWithSize(14.0),
NSParagraphStyleAttributeName:paragtaphStyle,
NSUnderlineStyleAttributeName:@(NSUnderlineStyleNone)
};
[muAttrString setAttributes:dic range:NSMakeRange(0, muAttrString.length)];
return muAttrString;
}
+ (CGFloat)descriptionLabelWidth
{
return SCREEN_WIDTH - 30*2;
}
+ (CGFloat)descriptionHeightWithText:(NSString *)text width:(CGFloat)width
{
NSMutableParagraphStyle *paragtaphStyle = [[NSMutableParagraphStyle alloc] init];
paragtaphStyle.alignment = NSTextAlignmentJustified;
paragtaphStyle.paragraphSpacing = 11.0;
paragtaphStyle.paragraphSpacingBefore = 10.0; //段落之间的间距
paragtaphStyle.headIndent = 0.0;
paragtaphStyle.lineBreakMode = NSLineBreakByWordWrapping;
CGSize size = [text boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:FontWithSize(14.0),
NSParagraphStyleAttributeName:paragtaphStyle}
context:nil].size;
return size.height + 10.0 + 75.0 + paragtaphStyle.paragraphSpacingBefore;
}
标签:
原文地址:http://www.cnblogs.com/ldc529/p/5675723.html