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;

    }

  • 相关阅读:
    转载CSDN(educast):c# 对两个Datatable的结构相同进行合并
    .net升级到4.0之后,出现'System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' or one of its depen
    转载来自博客园(poleices):C#和JavaScript交互(asp.net前台和后台互调)总结
    Bat文件 执行MSSQL 脚本
    多功能jQuery对话框插件 jBox 2.3 正式版
    十一种Web网站程序性能测试工具介绍
    大型电子商务网站架构
    Nginx负载均衡
    文语通5.0破解版下载 有详细图解安装说明
    分享8个超棒的免费高质量图标搜索引擎
  • 原文地址:https://www.cnblogs.com/ldc529/p/5675723.html
Copyright © 2011-2022 走看看