Skip to content

Commit

Permalink
Swift 4.2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
psharanda committed Sep 19, 2018
1 parent 08affd1 commit 099dd56
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 103 deletions.
2 changes: 1 addition & 1 deletion Atributika.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "Atributika"
s.version = "4.6.4"
s.version = "4.6.5"
s.summary = "Convert text with HTML tags, hashtags, mentions, links into NSAttributedString. Make them clickable with UILabel drop-in replacement."
s.description = <<-DESC
`Atributika` is an easy and painless way to build NSAttributedString. It is able to detect HTML-like tags, links, phone numbers, hashtags, any regex or even standard ios data detectors and style them with various attributes like font, color, etc. `Atributika` comes with drop-in label replacement `AttributedLabel` which is able to make any detection clickable.
Expand Down
9 changes: 8 additions & 1 deletion Demo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@

import UIKit


#if swift(>=4.2)
typealias ApplicationLaunchOptionsKey = UIApplication.LaunchOptionsKey
#else
typealias ApplicationLaunchOptionsKey = UIApplicationLaunchOptionsKey
#endif

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [ApplicationLaunchOptionsKey: Any]?) -> Bool {

let tbc = UITabBarController()

Expand Down
12 changes: 11 additions & 1 deletion Demo/AttributedLabelDemoViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@
import UIKit
import Atributika

#if swift(>=4.2)
public typealias TableViewCellStyle = UITableViewCell.CellStyle
#else
public typealias TableViewCellStyle = UITableViewCellStyle
#endif

class AttributedLabelDemoViewController: UIViewController {

private lazy var tableView: UITableView = {
let tableView = UITableView(frame: CGRect(), style: .plain)

tableView.delegate = self
tableView.dataSource = self
#if swift(>=4.2)
tableView.rowHeight = UITableView.automaticDimension
#else
tableView.rowHeight = UITableViewAutomaticDimension
#endif
tableView.estimatedRowHeight = 50
return tableView
}()
Expand Down Expand Up @@ -72,7 +82,7 @@ extension AttributedLabelDemoViewController: UITableViewDelegate, UITableViewDat
class TweetCell: UITableViewCell {
private let tweetLabel = AttributedLabel()

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
override init(style: TableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)

tweetLabel.onClick = { label, detection in
Expand Down
52 changes: 37 additions & 15 deletions Demo/Snippet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,14 @@ func stringWithManyDetectables() -> NSAttributedString {
let phoneNumbers = Style.backgroundColor(.yellow)
let mentions = Style.font(.italicSystemFont(ofSize: 12)).foregroundColor(.black)
let b = Style("b").font(.boldSystemFont(ofSize: 12))

#if swift(>=4.2)
let u = Style("u").underlineStyle(.single)
#else
let u = Style("u").underlineStyle(.styleSingle)
#endif


let all = Style.font(.systemFont(ofSize: 12)).foregroundColor(.gray)

let str = "@all I found <u>really</u> nice framework to manage attributed strings. It is called <b>Atributika</b>. Call me if you want to know more (123)456-7890 #swift #nsattributedstring https://github.com/psharanda/Atributika"
Expand Down Expand Up @@ -140,15 +147,25 @@ func stringWithHref() -> NSAttributedString {

func stringWithBoldItalicUnderline() -> NSAttributedString {
let font = UIFont(name: "HelveticaNeue-BoldItalic", size: 12)!
#if swift(>=4.2)
let uib = Style("uib").font(font).underlineStyle(.single)
#else
let uib = Style("uib").font(font).underlineStyle(.styleSingle)
#endif

let str = "<br><uib>Italicunderline</uib>".style(tags: uib)
.attributedString
return str
}

func stringWithImage() -> NSAttributedString {
let font = UIFont(name: "HelveticaNeue-BoldItalic", size: 12)!

#if swift(>=4.2)
let uib = Style("b").font(font).underlineStyle(.single)
#else
let uib = Style("b").font(font).underlineStyle(.styleSingle)
#endif
let str = "<b>Running</b> with <img id=\"scissors\"></img>!".style(tags: uib)

let mutableAttrStr = NSMutableAttributedString(attributedString: str.attributedString)
Expand All @@ -175,7 +192,12 @@ func stringWithImage() -> NSAttributedString {

func stringWithStrikethrough() -> NSAttributedString {
let all = Style.font(.systemFont(ofSize: 20))
#if swift(>=4.2)
let strike = Style("strike").strikethroughStyle(.single).strikethroughColor(.black)
#else
let strike = Style("strike").strikethroughStyle(.styleSingle).strikethroughColor(.black)
#endif

let code = Style("code").foregroundColor(.red)

let str = "<code>my code</code> <strike>test</strike> testing"
Expand All @@ -187,21 +209,21 @@ func stringWithStrikethrough() -> NSAttributedString {

func allSnippets() -> [NSAttributedString] {
return [
stringWithAtributikaLogo(),
stringWithTagsAndEmoji(),
stringWithHashTagAndMention(),
stringWithPhone(),
stringWithLink(),
stringWithManyDetectables(),
stringWith3Tags(),
stringWithGrams(),
stringWithStrong(),
stringWithTagAndHashtag(),
stringWithUnorderedList(),
stringWithHref(),
stringWithBoldItalicUnderline(),
stringWithImage(),
stringWithStrikethrough()
// stringWithAtributikaLogo(),
// stringWithTagsAndEmoji(),
// stringWithHashTagAndMention(),
// stringWithPhone(),
// stringWithLink(),
// stringWithManyDetectables(),
// stringWith3Tags(),
// stringWithGrams(),
// stringWithStrong(),
// stringWithTagAndHashtag(),
// stringWithUnorderedList(),
// stringWithHref(),
// stringWithBoldItalicUnderline(),
// stringWithImage(),
// stringWithStrikethrough()
]
}

4 changes: 4 additions & 0 deletions Demo/SnippetsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ class SnippetsViewController: UIViewController {

tableView.delegate = self
tableView.dataSource = self
#if swift(>=4.2)
tableView.rowHeight = UITableView.automaticDimension
#else
tableView.rowHeight = UITableViewAutomaticDimension
#endif
tableView.estimatedRowHeight = 50
return tableView
}()
Expand Down
7 changes: 6 additions & 1 deletion Sources/AttributedLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,12 @@ open class AttributedLabel: UIView {
let button = DetectionAreaButton(detection: detection)
button.accessibilityLabel = text
button.isAccessibilityElement = true
#if swift(>=4.2)
button.accessibilityTraits = UIAccessibilityTraits.button
#else
button.accessibilityTraits = UIAccessibilityTraitButton
#endif

button.isUserInteractionEnabled = state.isEnabled
button.addTarget(self, action: #selector(handleDetectionAreaButtonClick), for: .touchUpInside)
detectionAreaButtons.append(button)
Expand Down Expand Up @@ -236,7 +241,7 @@ extension NSAttributedString {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = textAlignment

let inheritedAttributes = [NSAttributedStringKey.font: font as Any, NSAttributedStringKey.paragraphStyle: paragraphStyle as Any]
let inheritedAttributes = [AttributedStringKey.font: font as Any, AttributedStringKey.paragraphStyle: paragraphStyle as Any]
let result = NSMutableAttributedString(string: string, attributes: inheritedAttributes)

result.beginEditing()
Expand Down
2 changes: 1 addition & 1 deletion Sources/AttributedText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public protocol AttributedTextProtocol {

extension AttributedTextProtocol {

private func makeAttributedString(getAttributes: (Style)-> [NSAttributedStringKey: Any]) -> NSAttributedString {
private func makeAttributedString(getAttributes: (Style)-> [AttributedStringKey: Any]) -> NSAttributedString {
let attributedString = NSMutableAttributedString(string: string, attributes: getAttributes(baseStyle))

for d in detections {
Expand Down
83 changes: 49 additions & 34 deletions Sources/Style.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ public enum StyleType {
case highlighted
}

// workaround for https://github.com/psharanda/Atributika/issues/27
#if swift(>=4.1)
extension NSAttributedStringKey: Equatable { }
extension NSAttributedStringKey: Hashable { }
#if swift(>=4.2)
public typealias AttributedStringKey = NSAttributedString.Key
#else
public typealias AttributedStringKey = NSAttributedStringKey
#endif

public struct Style {

public let name: String

public var attributes: [NSAttributedStringKey: Any] {
public var attributes: [AttributedStringKey: Any] {
return typedAttributes[.normal] ?? [:]
}

public var highlightedAttributes: [NSAttributedStringKey: Any] {
public var highlightedAttributes: [AttributedStringKey: Any] {
var attrs = attributes

typedAttributes[.highlighted]?.forEach { key, value in
Expand All @@ -49,7 +49,7 @@ public struct Style {
return attrs
}

public var disabledAttributes: [NSAttributedStringKey: Any] {
public var disabledAttributes: [AttributedStringKey: Any] {
var attrs = attributes

typedAttributes[.disabled]?.forEach { key, value in
Expand All @@ -59,23 +59,38 @@ public struct Style {
return attrs
}

public let typedAttributes: [StyleType: [NSAttributedStringKey: Any]]
public let typedAttributes: [StyleType: [AttributedStringKey: Any]]

public init(_ name: String = "", _ attributes: [NSAttributedStringKey: Any] = [:], _ type: StyleType = .normal) {
self.name = name
typedAttributes = [type: attributes]
public init() {
self.name = ""
self.typedAttributes = [:]
}

public init(_ name: String = "", _ typedAttributes: [StyleType: [NSAttributedStringKey: Any]] = [:]) {
public init(_ name: String) {
self.name = name
self.typedAttributes = typedAttributes
self.typedAttributes = [:]
}

public init(_ name: String, style: Style) {
self.name = name
self.typedAttributes = style.typedAttributes
}

public init(_ name: String, _ attributes: [AttributedStringKey: Any]) {
self.name = name
typedAttributes = [.normal: attributes]
}

public init(_ name: String, _ attributes: [AttributedStringKey: Any], _ type: StyleType) {
self.name = name
typedAttributes = [type: attributes]
}

public init(_ name: String, _ typedAttributes: [StyleType: [AttributedStringKey: Any]]) {
self.name = name
self.typedAttributes = typedAttributes
}

public func named(_ name: String) -> Style {
return Style(name, style: self)
}
Expand Down Expand Up @@ -182,90 +197,90 @@ public struct Style {


public static func font(_ value: Font, _ type: StyleType = .normal) -> Style {
return Style("", [NSAttributedStringKey.font: value], type)
return Style("", [AttributedStringKey.font: value], type)
}

public static func paragraphStyle(_ value: NSParagraphStyle, _ type: StyleType = .normal) -> Style {
return Style("", [NSAttributedStringKey.paragraphStyle: value], type)
return Style("", [AttributedStringKey.paragraphStyle: value], type)
}

public static func foregroundColor(_ value: Color, _ type: StyleType = .normal) -> Style {
return Style("", [NSAttributedStringKey.foregroundColor: value], type)
return Style("", [AttributedStringKey.foregroundColor: value], type)
}

public static func backgroundColor(_ value: Color, _ type: StyleType = .normal) -> Style {
return Style("", [NSAttributedStringKey.backgroundColor: value], type)
return Style("", [AttributedStringKey.backgroundColor: value], type)
}

public static func ligature(_ value: Int, _ type: StyleType = .normal) -> Style {
return Style("", [NSAttributedStringKey.ligature: value], type)
return Style("", [AttributedStringKey.ligature: value], type)
}

public static func kern(_ value: Float, _ type: StyleType = .normal) -> Style {
return Style("", [NSAttributedStringKey.kern: value], type)
return Style("", [AttributedStringKey.kern: value], type)
}

public static func strikethroughStyle(_ value: NSUnderlineStyle, _ type: StyleType = .normal) -> Style {
return Style("", [NSAttributedStringKey.strikethroughStyle : value.rawValue], type)
return Style("", [AttributedStringKey.strikethroughStyle : value.rawValue], type)
}

public static func strikethroughColor(_ value: Color, _ type: StyleType = .normal) -> Style {
return Style("", [NSAttributedStringKey.strikethroughColor: value], type)
return Style("", [AttributedStringKey.strikethroughColor: value], type)
}

public static func underlineStyle(_ value: NSUnderlineStyle, _ type: StyleType = .normal) -> Style {
return Style("", [NSAttributedStringKey.underlineStyle : value.rawValue], type)
return Style("", [AttributedStringKey.underlineStyle : value.rawValue], type)
}

public static func underlineColor(_ value: Color, _ type: StyleType = .normal) -> Style {
return Style("", [NSAttributedStringKey.underlineColor: value], type)
return Style("", [AttributedStringKey.underlineColor: value], type)
}

public static func strokeColor(_ value: Color, _ type: StyleType = .normal) -> Style {
return Style("", [NSAttributedStringKey.strokeColor: value], type)
return Style("", [AttributedStringKey.strokeColor: value], type)
}

public static func strokeWidth(_ value: Float, _ type: StyleType = .normal) -> Style {
return Style("", [NSAttributedStringKey.strokeWidth: value], type)
return Style("", [AttributedStringKey.strokeWidth: value], type)
}

#if !os(watchOS)
public static func shadow(_ value: NSShadow, _ type: StyleType = .normal) -> Style {
return Style("", [NSAttributedStringKey.shadow: value], type)
return Style("", [AttributedStringKey.shadow: value], type)
}
#endif

public static func textEffect(_ value: String, _ type: StyleType = .normal) -> Style {
return Style("", [NSAttributedStringKey.textEffect: value], type)
return Style("", [AttributedStringKey.textEffect: value], type)
}

#if !os(watchOS)
public static func attachment(_ value: NSTextAttachment, _ type: StyleType = .normal) -> Style {
return Style("", [NSAttributedStringKey.attachment: value], type)
return Style("", [AttributedStringKey.attachment: value], type)
}
#endif

public static func link(_ value: URL, _ type: StyleType = .normal) -> Style {
return Style("", [NSAttributedStringKey.link: value], type)
return Style("", [AttributedStringKey.link: value], type)
}

public static func link(_ value: String, _ type: StyleType = .normal) -> Style {
return Style("", [NSAttributedStringKey.link: value], type)
return Style("", [AttributedStringKey.link: value], type)
}

public static func baselineOffset(_ value: Float, _ type: StyleType = .normal) -> Style {
return Style("", [NSAttributedStringKey.baselineOffset: value], type)
return Style("", [AttributedStringKey.baselineOffset: value], type)
}

public static func obliqueness(_ value: Float, _ type: StyleType = .normal) -> Style {
return Style("", [NSAttributedStringKey.obliqueness: value], type)
return Style("", [AttributedStringKey.obliqueness: value], type)
}

public static func expansion(_ value: Float, _ type: StyleType = .normal) -> Style {
return Style("", [NSAttributedStringKey.expansion: value], type)
return Style("", [AttributedStringKey.expansion: value], type)
}

public static func writingDirection(_ value: NSWritingDirection, _ type: StyleType = .normal) -> Style {
return Style("", [NSAttributedStringKey.writingDirection: value.rawValue], type)
return Style("", [AttributedStringKey.writingDirection: value.rawValue], type)
}
}
Loading

0 comments on commit 099dd56

Please sign in to comment.