具有不同NSMutableParagraphStyle的Nsattributedstring [英] Nsattributedstring with different NSMutableParagraphStyle

查看:64
本文介绍了具有不同NSMutableParagraphStyle的Nsattributedstring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有1个具有多个段落的属性字符串.
我给了 FirstLineHeadIndent = 2.12.
现在,我想将 FirstLineHeadIndent = 0 分配给第一段和 FirstLineHeadIndent = 2 到属性字符串中的第二段.

I have 1 attributed string with multiple paragraph.
I had given the FirstLineHeadIndent = 2.12.
Now I want to give the FirstLineHeadIndent=0 to 1st paragraph and FirstLineHeadIndent=2 to 2nd paragraph in the attributed string.

如果我将属性设置为

 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing =Line_space;
        paragraphStyle.firstLineHeadIndent =2;
        paragraphStyle.headIndent =margin_space;
        paragraphStyle.tailIndent =-margin_space;
        paragraphStyle.paragraphSpacing=paragraph_space;

        NSDictionary *ats = @{
                              NSFontAttributeName : [UIFont fontWithName:self.bookView.defaultFontFamily size:self.bookView.defaultFontSize],
                              NSParagraphStyleAttributeName : paragraphStyle,
                              };

这将为两个副翼提供顶部空间.请帮我.我正在上传图片以获取更多帮助:-

It will give the head space to both the paragaraph. Please help me. I am uploading the image for more help:-

推荐答案

因此,解决方案是使用2个段落样式.
您的 NSString \ n 分隔,表示两个段落之间的分隔.

So, the solution is to use 2 paragraph styles.
Your NSString is separated by a \n, that indicate the separations between the 2 paragraphs.

NSMutableParagraphStyle *firstParagraphStyle = [[NSMutableParagraphStyle alloc] init];
[firstParagraphStyle setFirstLineHeadIndent:0];
//Do the rest of the settings

NSMutableParagraphStyle *secondParagraphStyle = [[NSMutableParagraphStyle alloc] init];
[secondParagraphStyle setFirstLineHeadIndent:2];
//Do the rest of the settings

//You may use the same paragraphStyle, changing just the firstLineHeadIndent, and set the attributes, but for a clearer explanation, I used 2 paragraph styles
NSRange range = [[yourAttributedString string] rangeOfString:@"\n"];

[yourAttributedString addAttribute:NSParagraphStyleAttributeName value: firstParagraphStyle range:NSMakeRange(0, range.location)]; //The range is from the start to the \n

[yourAttributedString addAttribute:NSParagraphStyleAttributeName value: secondParagraphStyle range:NSMakeRange(range.location, [[yourAttributedString string] length]-range.location)]; //The range is from the start of \n to the end

这篇关于具有不同NSMutableParagraphStyle的Nsattributedstring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆