" let dic = ToolsHelper.parserRichText(txt: storyText) DispatchQueue.main.async { self.richTextBox.attributedText = dic["attributedText"] as? NSAttributedString self.contentHeight = dic["height"] as? CGFloat ?? 0 self.contentWidth = dic["width"] as? CGFloat ?? 0 self.updateAttributedString() } } let tag = 200 DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() 1) { let subView = self.view.viewWithTag(tag) if subView != nil { subView?.removeFromSuperview() } } } // 编辑文本 func editText(txt: String) -> Void { let tags = [RichTextTag.Bold: "strong", RichTextTag.Italic: "em", RichTextTag.Underline: "ul", RichTextTag.Deleteline: "del"] var result = "" var otherTxt = txt for item in tags.keys { var tagStart = "", tagEnd = "" if otherTxt.contains(item.begin) && otherTxt.contains(item.end) { tagStart = "<(tags[item] ?? "")>" otherTxt.components(separatedBy: item.begin)[1] tagEnd = tagStart.components(separatedBy: item.end)[0] "(tags[item] ?? "")>" tagEnd tagStart = otherTxt.components(separatedBy: item.begin)[0] tagStart tagEnd = otherTxt.components(separatedBy: item.end)[1] tagEnd } if (tagStart == "") { continue } result = tagStart otherTxt = tagEnd } result = otherTxt print(result) } // 更新AttributedString func updateAttributedString() -> Void { let attributedtext = NSMutableAttributedString(attributedString: richTextBox.attributedText) let attr = attributedtext.attributes(at: 0, longestEffectiveRange: nil, in: NSRange(location: attributedtext.length - 1, length: 1)) let b = (contentHeight 20) > self.view.frame.height let font = UIFont.systemFont(ofSize:Config.TEMPMENUSIZE (b ? -1 : 1)) let paragraphyStyle = NSMutableParagraphStyle() paragraphyStyle.lineSpacing = 5 paragraphyStyle.lineBreakMode = .byTruncatingTail attributedtext.addAttribute(.paragraphStyle, value: paragraphyStyle, range: NSRange(location: 0, length: attributedtext.length)) attributedtext.addAttribute(.font, value: font, range: NSRange(location: 0, length: attributedtext.length)) for item in attr.keys { attributedtext.removeAttribute(item, range: NSRange(location: attributedtext.length - 1, length: 1)) } let constraints = [NSLayoutConstraint(item: view, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 10), NSLayoutConstraint(item: view, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: -10), NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 10), NSLayoutConstraint(item: view, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: -10)] let width = view.frame.width - 20 for constraint in constraints { switch constraint.firstAttribute { case .trailing: constraint.constant = width case .leading: constraint.constant = width default:break } } view.addConstraints(constraints) }}