zoukankan      html  css  js  c++  java
  • iOS attributeString 换行

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

    }

  • 相关阅读:
    [TCP/IP] HTTPS的工作原理
    [TCP/IP] SSL的通讯原理
    ESLint笔记
    MacBook Pro维修过程
    论前端工程化
    听书,怅然若失
    js实现Mac触摸板双指事件(上/下/左/右/放大/缩小)
    Windows和Mac浏览器启动本地程序
    科目三(番禺化龙展贸东路)考试录20170224
    写jQuery插件该注意的
  • 原文地址:https://www.cnblogs.com/ldc529/p/5675723.html
Copyright © 2011-2022 走看看