SwiftUI中的UIViewController

时间:2019-11-03 09:37:54

标签: ios xcode uiviewcontroller uikit swiftui

我正在创建新闻应用程序。本文是一个.txt / .html / .md文件,因此我在UIKit中创建了一个markdown阅读器。我已经在新的Xcode Single View项目中创建了它,这是我的代码:

[enter link description here][1]

import UIKit
import markymark

class AdvancedMarkdownViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    let scrollView = UIScrollView()


    let markDownView = getMarkDownView()

    scrollView.isScrollEnabled = false

    view.addSubview(scrollView)
    scrollView.addSubview(markDownView)

    let views: [String: Any] = [
        "view": view,
        "scrollView": scrollView,
        "markDownView": markDownView
        ]

    scrollView.translatesAutoresizingMaskIntoConstraints = false
    markDownView.translatesAutoresizingMaskIntoConstraints = false


    var constraints: [NSLayoutConstraint] = []
    constraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|[scrollView]|", options: [], metrics: [:], views: views)
    constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|[scrollView]|", options: [], metrics: [:], views: views)
    constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|[markDownView(==scrollView)]|", options: [], metrics: [:], views: views)
    constraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|[markDownView]|", options: [], metrics: [:], views: views)
    view.addConstraints(constraints)

}
}

private extension AdvancedMarkdownViewController {

func getMarkDownView() -> UIView {

    // Parsing to MarkDownItem's
    let markDownString = getMarkDownString()

    let markyMark = MarkyMark(build: {
        // Choose flavor (set of rules)
        $0.setFlavor(ContentfulFlavor())

        //Example: Add single custom rules
        //$0.addRule(CustomHeaderRule())
    })

    let markDownItems = markyMark.parseMarkDown(markDownString)

    // Configure styling, see README for more styling options or Implement your own Styling object instead of DefaultStyling.
    let styling = DefaultStyling()
    styling.listStyling.bulletImages = [
        UIImage(named: "circle"),
        UIImage(named: "emptyCircle"),
        UIImage(named: "line"),
        UIImage(named: "square")
    ]

    styling.headingStyling.contentInsetsForLevels = [
        UIEdgeInsets(top: 16, left: 6, bottom: 15, right: 10), // H1
        UIEdgeInsets(top: 5, left: 0, bottom: 5, right: 10) // H2, ...
    ]

    styling.headingStyling.textColorsForLevels = [
        .label,  // H1
        .label, // H2
        .secondaryLabel, // H3
        .tertiaryLabel // H4, ...
    ]

    // Only uppercase H1 headers
    styling.headingStyling.capitalizationForLevels = [
        nil //H1, ...
    ]

    styling.headingStyling.fontsForLevels = [
        UIFont.preferredFont(forTextStyle: .largeTitle), //H1
        UIFont.preferredFont(forTextStyle: .title1), //H2
        UIFont.preferredFont(forTextStyle: .title2), //H3
        UIFont.preferredFont(forTextStyle: .title3), //H4
        UIFont.preferredFont(forTextStyle: .headline), //H5
        UIFont.preferredFont(forTextStyle: .footnote), //H6
    ]

    styling.headingStyling.isBold = true
  // Link

    styling.linkStyling.textColor = .systemBlue
    styling.linkStyling.baseFont = nil // Default: nil. Setting baseFont to nil will inherit font from paragraphStyling

    styling.linkStyling.isBold = false
    styling.linkStyling.isItalic = true
    styling.linkStyling.isUnderlined = false


//image

    styling.imageStyling.contentInsets.left = 20
    styling.imageStyling.contentInsets.right = 20

//paragraph

    styling.paragraphStyling.textColor = .label

  // HorizonLinesStyling

    styling.horizontalLineStyling.backgroundColor = UIColor.secondaryLabel
    styling.horizontalLineStyling.contentInsets.left = 20
    styling.horizontalLineStyling.contentInsets.right = 20


    let configuration = MarkdownToViewConverterConfiguration(styling: styling)
    let converter = MarkDownConverter(configuration: configuration)

    // Converter hook, only meant for very specific (otherwise impossible) usecases. The hook is triggered every time a MarkdownItem is converted to a view.
    // In this example the headers are rotated, which is normally not supported in MarkyMark styling.

    /**
    converter.didConvertElement = {
        markdownItem, view in

        // When a header is converted and it's H1, rotate it slighly for example
        if let headerMarkDownItem = markdownItem as? HeaderMarkDownItem, headerMarkDownItem.level == 1 {
            view.transform = CGAffineTransform(rotationAngle: -0.07)
        }
    }

    */


    return converter.convert(markDownItems)
}

func getMarkDownString() -> String {
    var markdownString: String = ""
    if let filepath = Bundle.main.path(forResource: "markdown", ofType: "txt") {
        markdownString = try! String(contentsOfFile: filepath)
    }

    return markdownString
}

}

这是结果: Vidéo

在尝试将其添加到我的主项目中之后,在SwiftUI avec中编写了一个结构。

     struct ViewControllerBISController : UIViewControllerRepresentable {



   func makeUIViewController(context: UIViewControllerRepresentableContext<ViewControllerBISController>) -> AdvancedMarkdownViewController {
       return AdvancedMarkdownViewController()
   }


   func updateUIViewController(_ uiViewController: AdvancedMarkdownViewController, context: UIViewControllerRepresentableContext<ViewControllerBISController>) {

   }
}

但是当我将其嵌入代码中时,我只有一个小矩形This is the result 我希望快速UI视图中的Markdown分析器的高度等于我的markdown文件的高度。但是我不想在markdown视图中滚动,因为我的swift UI视图中已经滚动了。

需要您的帮助。

0 个答案:

没有答案