spiegel-keyman/ios/keyman/Keyman/Keyman/ActivityItemProvider.swift
2017-09-28 09:51:17 +07:00

63 lines
2 KiB
Swift

//
// ActivityItemProvider.swift
// Keyman
//
// Created by Gabriel Wong on 2017-09-04.
// Copyright © 2017 SIL International. All rights reserved.
//
import UIKit
private let htmlMailFormat = """
<html><head><style type=\"text/css\">pre {font-family:\"%@\";font-size:%@;}</style>
</head><body><pre>%@</pre>%@</body></html>
"""
private let mailFooterTextForPad =
"<br><br>Sent from&nbsp;<a href=\"https://keyman.com/ipad\">Keyman for iPad</a>"
private let mailFooterTextForPhone =
"<br><br>Sent from&nbsp;<a href=\"https://keyman.com/iphone\">Keyman for iPhone</a>"
private let fbText = "Can't read this? Help at https://keyman.com/fonts"
// Prepares the share text
class ActivityItemProvider: UIActivityItemProvider {
private let text: String
private let font: UIFont
init(text: String, font: UIFont?) {
self.text = text
self.font = font ?? UIFont.systemFont(ofSize: UIFont.systemFontSize)
super.init(placeholderItem: text)
}
override var item: Any {
switch activityType {
case UIActivityType.mail?:
return htmlMail(withText: text, font: font)
case UIActivityType.postToFacebook?:
return "\(text)\n\n\(fbText)"
case UIActivityType.postToTwitter?:
if text.characters.count > 140 {
return text[..<text.index(text.startIndex, offsetBy: 140)]
}
return text
default:
return text
}
}
private func htmlMail(withText text: String, font: UIFont) -> String {
let mailText = text.replacingOccurrences(of: "&", with: "&amp;")
.replacingOccurrences(of: "<", with: "&lt;")
.replacingOccurrences(of: ">", with: "&gt;")
let familyName = font.familyName
let fontSize = String(Int(font.pointSize))
let footerText: String
if UIDevice.current.userInterfaceIdiom == .pad {
footerText = mailFooterTextForPad
} else {
footerText = mailFooterTextForPhone
}
// html mail format: family-name, font-size, text, footer-text
return String(format: htmlMailFormat, familyName, fontSize, mailText, footerText)
}
}