首页 > 解决方案 > Swift NSAttribured String CSS 属性宽度不起作用

问题描述

我有 HTML 文本,我想将其转换为 NSAttributedString,一切正常,但有一个问题,我想在左侧显示带有垂直线的 blockquote 标记,我希望这条线的宽度为 3px,但似乎宽度被忽略。如何更改此 div 的宽度?这是我的代码。我做错了什么?

public class func NSStringToAttributed(_ nsText: String,
                                           font: UIFont = UIFont.systemFont(ofSize: 15.0)) -> NSMutableAttributedString? {

        let stringWithFont = (NSString(string: nsText).appendingFormat("<style>body{font-family: '%@'; font-size:%fpx;}</style>",
                                                                       ".SFUIText",
                                                                       font.pointSize))

        let parsedCommentHTML = stringWithFont.replacingOccurrences(of: "<blockquote>",
                                                                    with: "<div style=\"max-width: 3px;  background: #82a43a; height: transform:translateY();\"><i style=\"color:#A9A9A9\"><blockquote>\n").replacingOccurrences(of: "</blockquote>",
                                                                                                                                          with: "</blockquote></i></div>")



        if let data = String(parsedCommentHTML).data(using: .unicode,
                                                  allowLossyConversion: false) {
            let style = NSMutableParagraphStyle()
            style.minimumLineHeight = 20.0
            style.headIndent = 6.0
            style.firstLineHeadIndent = 6.0
            style.alignment = .left
            let attributes = [
                NSParagraphStyleAttributeName: style ]
            if let attrString = try? NSMutableAttributedString(data: data,
                                                               options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
                                                               documentAttributes: nil) {
                attrString.addAttributes(attributes,
                                         range: NSString(string: attrString.string).range(of: attrString.string))
                attrString.addAttribute(NSKernAttributeName,
                                        value: -0.2,
                                        range: NSString(string: attrString.string).range(of: attrString.string))
                return attrString
            }
        }

这是结果 在此处输入图像描述

标签: htmlioscssswift

解决方案


推荐阅读