如何为 NSAttributedString 的不同部分添加不同的行距? [英] How can I add different line spacing for different parts of NSAttributedString?

查看:41
本文介绍了如何为 NSAttributedString 的不同部分添加不同的行距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让一个 NSAttributedString 包含多条消息.如果单个消息有一个长文本并且它环绕,我希望有一个行距,比如说,5.因为我有一个 NSAttributedString 包含多个消息,我希望每条消息之间有一个更大的行距;比方说 20.

I want to have a single NSAttributedString contain multiple messages. If a single message has a long text and it wraps around, I want to have a line spacing of, say, 5. Because I have a single NSAttributedString contain multiple messages, I want to have a bigger line spacing between each message; let's say 20.

我看到"是一条信息.我认为两者兼而有之……"是一条消息,尽管它可以分为两行,而像单向聊天一样"是一条消息.

The 'I see' is one message. The 'I'd think it'd be both...' is one message, although it wraps down to two lines and 'Like a one way chat' is one message.

注意第 2 行和第 3 行之间的行距如何小于第 1 行和第 2 行以及第 3 行和第 4 行之间的行距.

Notice how the line spacing between the 2nd and 3rd is smaller than the 1st and 2nd and between the 3rd and 4th.

我在每条消息的末尾附加一个 \n 并且我尝试使用 NSParagraphStyle 来控制行间距,但它似乎是全有或全无:

I am appending a \n to the end of each message and I've tried using NSParagraphStyle which gives me control of the line spacing, but it seems to be all or nothing:

        // index is the index of the group of messages as I iterate through them
        // contentText is an NSMutableAttributedString
        if index != messages.count - 1 {
            let style = NSMutableParagraphStyle()
            style.lineSpacing = 40.0

            let lineReturn = NSMutableAttributedString(string: "\n")
            contentText.appendAttributedString(lineReturn)

            if index == 0 {
                contentText.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSMakeRange(contentText.length-lineReturn.length, lineReturn.length))
            } else {
                contentText.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSMakeRange(contentText.length-lineReturn.length-1, lineReturn.length+1))
            }
        }

如果我将行距添加到开头,它将设置整个标签的行距.

If I add the line spacing to the beginning it will set the line spacing for the entire label.

        if index == 0 {
            let style = NSMutableParagraphStyle()
            style.lineSpacing = 40.0
            contentText.addAttribute(NSParagraphStyleAttributeName, value: style1, range: NSMakeRange(start, 1))
        }

(这真的只是我最近的一次尝试.)

(This is really only my latest try.)

感谢您的帮助!:)

推荐答案

问题!

您需要使用 baselineOffset 属性:

let contentText = NSMutableAttributedString(
string: "I see\nI'd think it`d be both a notification and a\nplace to see past announcements\nLike a one way chat.")

contentText.addAttribute(.baselineOffset, value: 10, range: NSRange(location: 0, length: 5))
contentText.addAttribute(.baselineOffset, value: -10, range: NSRange(location: 85, length: 20))


结果:


Result:

我明白了

我认为它既是通知又是
查看过去公告的地方

就像单向聊天一样."

"I see

I'd think it`d be both a notification and a
place to see past announcements

Like a one way chat."

这篇关于如何为 NSAttributedString 的不同部分添加不同的行距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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