【问题标题】:NSAttributedString not working as expected in iOS 13NSAttributedString 在 iOS 13 中无法按预期工作
【发布时间】:2019-10-10 06:33:34
【问题描述】:

我正在使用 NSMutableAttributedString 在标签中显示多字体和彩色文本。 NSMutableAttributedString 在 iOS 13 中无法正常运行,但相同的代码在 iOS 11 和 12 版本中运行良好。

let hdAttributedText = NSMutableAttributedString(string: "Sample", attributes: [NSAttributedString.Key.font: UIFont(name: "HelveticaNeue", size: 14.0)!, NSAttributedString.Key.foregroundColor: UIColor.black])
hdAttributedText.append(NSAttributedString(string: " "))
hdAttributedText.append(NSAttributedString(string: "Description", attributes: [NSAttributedString.Key.font: UIFont(name: "HelveticaNeue-Medium", size: 14.0)!, NSAttributedString.Key.foregroundColor: UIColor(red: 0.29, green: 0.70, blue: 0.36, alpha: 1)]))
logoTextLabel.attributedText = hdAttributedText

预期结果是“示例描述”。在此文本中,“示例”应为黑色文本的常规字体,“描述”应为绿色的中等字体

【问题讨论】:

  • “没有按预期工作”是什么意思?它显示为什么? UIFont.eX_Regular_14 是什么?
  • @Sweeper 我正在使用上面的代码将标签文本显示为两种不同的字体和颜色,但它不起作用。相同的代码在 iOS 11 和 12 版本中运行良好... UIFont.eX_Regular_14 是我的 UIFont 扩展
  • 如何它不起作用?运行该代码的实际结果是什么?你能说明UIFont.eX_Regular_14是如何实现的吗?
  • static var eX_Regular_14: UIFont {return UIFont(name: "PFDinTextArabic-Regular", size: viewWidthPercent(percent: 3.73))!}
  • 您应该在您的问题中edit 该信息,并提供minimal reproducible example。你还没有说它是怎么不工作的。

标签: swift xcode nsattributedstring ios13 nsmutableattributedstring


【解决方案1】:

在 iOS 13 上,属性字符串在 tableviews 中不起作用,幸运的是我知道一个解决方法,它会像一个魅力一样工作。

您需要做的就是使属性字符串在主线程上运行(即使您在 awakeFromNib 中设置属性字符串),如下所示:

DispatchQueue.main.async {
                     //set your attributed text here
         }

【讨论】:

  • 我不认为这是正确的,如果您构建新项目并在那里运行它,只是它会起作用。
  • 我在一家价值十亿美元的公司工作,我已经在那里解决了这个问题。这不是关于思考,而是关于实施我在这里所说的。难怪人们为什么投反对票。
  • 这是一个很好的解决方案,但不完全清楚为什么。在尝试更改 viewDidLoad 中的文本时,我们假设它位于串行队列中。这很奇怪
【解决方案2】:

问题是您在“示例”和“描述”之间添加的空白没有任何属性。试着给它一些属性...

hdAttributedText.append(NSAttributedString(string: " ", attributes: [NSAttributedString.Key.font: UIFont(name: "HelveticaNeue-Medium", size: 14.0)!, NSAttributedString.Key.foregroundColor: UIColor(red: 0.29, green: 0.70, blue: 0.36, alpha: 1)]))

试试这样的..

【讨论】:

    【解决方案3】:

    扩展:Swift 中的 Noob

    在 iOS 13.0+ 中

    // 声明

    extension String {
        func htmlToAttributedString(_ completeHandler: @escaping (NSAttributedString?) -> Void) {
            if let data = data(using: .unicode) {
                DispatchQueue.main.async {
                    do {
                        completeHandler(try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.unicode.rawValue], documentAttributes: nil))
                    } catch {
                        completeHandler(nil)
                    }
                }
            } else { completeHandler(nil) }
        }
    }
    

    // 使用

    htmlString.htmlToAttributedString { NSAttributedString in
        if let NSAttributedString = NSAttributedString {
            self.m_Detail_Lbl.attributedText = NSAttributedString
        } else {
            // error handler
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-17
      • 2013-05-27
      相关资源
      最近更新 更多