mirror of
https://github.com/zhigang1992/GitHawk.git
synced 2026-05-22 23:42:50 +08:00
* add highlightr to local pods * update highlight.js to 9.12.0 * remove 3.2 post install * fix 9.12 to use browser based hljs * trim down supported languages * smaller code size
69 lines
1.9 KiB
Swift
69 lines
1.9 KiB
Swift
//
|
|
// CodeView.swift
|
|
// Freetime
|
|
//
|
|
// Created by Ryan Nystrom on 11/26/17.
|
|
// Copyright © 2017 Ryan Nystrom. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
final class CodeView: UIScrollView {
|
|
|
|
private lazy var textView: UITextView = {
|
|
let view = UITextView()
|
|
view.font = Styles.Fonts.code
|
|
view.isScrollEnabled = false
|
|
view.isEditable = false
|
|
view.contentInset = .zero
|
|
view.textContainerInset = UIEdgeInsets(
|
|
top: Styles.Sizes.rowSpacing,
|
|
left: Styles.Sizes.columnSpacing,
|
|
bottom: Styles.Sizes.rowSpacing,
|
|
right: Styles.Sizes.columnSpacing
|
|
)
|
|
self.addSubview(view)
|
|
return view
|
|
}()
|
|
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
backgroundColor = .clear
|
|
isDirectionalLockEnabled = true
|
|
}
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
// MARK: Public API
|
|
|
|
func set(code: String) {
|
|
set(attributedCode: NSAttributedString(
|
|
string: code,
|
|
attributes: [
|
|
// match Highlightr size
|
|
.font: UIFont(name: "Courier", size: 14)!,
|
|
.foregroundColor: Styles.Colors.Gray.dark.color,
|
|
]))
|
|
}
|
|
|
|
func set(code: String, language: String?) {
|
|
if let language = language,
|
|
let highlighted = GithubHighlighting.highlight(code, as: language) {
|
|
set(attributedCode: highlighted)
|
|
} else {
|
|
set(code: code)
|
|
}
|
|
}
|
|
|
|
func set(attributedCode: NSAttributedString) {
|
|
textView.attributedText = attributedCode
|
|
let max = CGFloat.greatestFiniteMagnitude
|
|
let size = textView.sizeThatFits(CGSize(width: max, height: max))
|
|
textView.frame = CGRect(origin: .zero, size: size)
|
|
contentSize = size
|
|
}
|
|
|
|
}
|