NSAttributedString 富文本 图文混排 首行缩进

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

- (void)viewDidLoad {
    [super viewDidLoad];  
  NSString *str2 =  @"Apple/苹果iPhone 6 零首付生活有你足够王木木";
    NSString *str1 =  @"Apple/苹果iPhone 6 零首付生活有你足够";
    //计算文字高度
    CGSize size = [str2 boundingRectWithSize:CGSizeMake(([UIScreen mainScreen].bounds.size.width-30)/2-10, 999) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size;
    
    
    /*
     
     NSMutableParagraphStyle与NSParagraphStyle包括一下属性
     
     
     alignment //对齐方式
     firstLineHeadIndent //首行缩进
     headIndent //缩进
     tailIndent  //尾部缩进
     lineBreakMode  //断行方式
     maximumLineHeight  //最大行高
     minimumLineHeight  //最低行高
     lineSpacing  //行距
     paragraphSpacing  //段距
     paragraphSpacingBefore  //段首空间
     baseWritingDirection  //句子方向
     lineHeightMultiple  //可变行高,乘因数。
     hyphenationFactor //连字符属性
     NSString *const NSForegroundColorAttributeName;//值为UIColor,字体颜色,默认为黑色。
     
     
     */
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    //首行缩进
    paragraphStyle.firstLineHeadIndent = 30;
    
    //换行剪裁
    paragraphStyle.lineBreakMode = NSLineBreakByClipping;
    
    NSDictionary *ats = @{ NSParagraphStyleAttributeName : paragraphStyle};
    
    UILabel *lab = [[UILabel alloc]init];
    lab.numberOfLines = 0;
    lab.text = str1;
    
    lab.font = [UIFont systemFontOfSize:13];
    lab.frame = CGRectMake(100, 100, 200,size.height);
    
    lab.attributedText = [[NSAttributedString alloc] initWithString:str1 attributes:ats];
    
    lab.backgroundColor = [UIColor blueColor];
    [self.view addSubview:lab];
    }