ios - 具有不同 NSMutableParagraphStyle 的 Nsattributedstring

标签 ios attributes nsattributedstring

我有 1 个带有多个段落的属性字符串。
我给了 FirstLineHeadIndent = 2.12.
现在我想把 FirstLineHeadIndent=0 给第一段 和 FirstLineHeadIndent=2 到属性字符串中的第 2 段。

如果我像这样设置属性

 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,
                              };

它将为两个段落提供顶部空间。 请帮我。 我正在上传图片以获得更多帮助:- enter image description here

最佳答案

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

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

关于ios - 具有不同 NSMutableParagraphStyle 的 Nsattributedstring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23650373/

相关文章:

ios - AvPlayerLayer 大小调整不正确?

ios - 具有完全不同布局的通用应用程序

python - 如何知道变量是否在列表中对象的属性中?

objective-c - NSAttributedString 的 boundingRectWithSize 返回错误的大小

ios - UICollectionview cell/imageview 不会全屏点击

c# - 将类的实例作为参数传递给属性构造函数

visual-studio - 添加到默认具体类的接口(interface)的属性?

swift - 调用中的额外 'data' 参数

ios - 在 UITextView 上替换属性文本时维护自定义属性

ios - iPhone Jabber/XMPP 客户端... "TURN Connection failed"