如何使用 NSMutableAttributedString、NSMutableParagraphStyle 和 swift 制作带有图片而不是标记的项目符号列表

问题描述

一天中的好时光。我正在尝试实现一个带有列表的屏幕,并以项目的形式标有图片。为了实现,我选择了带有属性文本的 UITextView,我正在尝试为其准备 NSMutableAttributedString

desired screen layout

文本中的短语可以有不同的样式(常规和粗体)。我正在尝试通过 NSMutableParagraphStyle 配置间隔:

        static var textBlock1: NSMutableAttributedString {
        let text1 =  "\thow much does it cost to suspend"
        let text2 =  "Basic services"
        let text3 =  "Internet,TV,Telephony — 20$ / month for one"
        let text4 =  "Additional services -"
        let text5 =  "free."

        let completeText = NSMutableAttributedString(string: "")

        completeText.append(Const.image)
        completeText.append(NSAttributedString(string: text1,attributes: Const.regularattributes))
        completeText.append(NSAttributedString(string: text2,attributes: Const.boldAttributes))
        completeText.append(NSAttributedString(string: text3,attributes: Const.regularattributes))
        completeText.append(NSAttributedString(string: text4,attributes: Const.boldAttributes))
        completeText.append(NSAttributedString(string: text5,attributes: Const.regularattributes))

        completeText.enumerateAttributes(in: NSMakeRange(0,completeText.string.count),options: []) { (attribute,range,stop) -> Void in
            if attribute.keys.contains(.font) {
                completeText.addAttributes(
                    [.paragraphStyle : Const.paragraphStyle],range: NSMakeRange(range.lowerBound - 1,range.length))
            }
        }

        return completeText
    }

当文本中没有换行符(“\n”)时,一切正常,文本缩进正确:

correct paragraphStyle

但是当我尝试在文本中添加换行符“\n”时,一切都中断了

        let text1 =  "\thow much does it cost to suspend\n"
        let text2 =  "Basic services\n"
        let text3 =  "Internet,Telephony — 20$ / month for one\n"
        let text4 =  "Additional services -"
        let text5 =  "free."

incorrect paragraphStyle

告诉我我做错了什么以及我如何解决这个问题。预先感谢您的回答。

Github: example of a test implementation

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)