码迷,mamicode.com
首页 > 移动开发 > 详细

iOS attributeString 换行

时间:2016-07-16 11:27:33      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:

- (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;

}

 

iOS attributeString 换行

标签:

原文地址:http://www.cnblogs.com/ldc529/p/5675723.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!