Skip to content

Commit b1f2d15

Browse files
committed
Swift 4.2 support
1 parent c4fbcaa commit b1f2d15

File tree

8 files changed

+42
-35
lines changed

8 files changed

+42
-35
lines changed

Sources/SwiftRichString/Attributes/CommonsAttributes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import CoreGraphics
5454
public typealias Image = UIImage
5555
public typealias Font = UIFont
5656
public typealias FontDescriptor = UIFontDescriptor
57-
public typealias SymbolicTraits = UIFontDescriptorSymbolicTraits
57+
public typealias SymbolicTraits = UIFontDescriptor.SymbolicTraits
5858
public typealias LineBreak = NSLineBreakMode
5959

6060
let FontDescriptorFeatureSettingsAttribute = UIFontDescriptor.AttributeName.featureSettings

Sources/SwiftRichString/Attributes/FontData.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public class FontData {
100100
///
101101
/// - Parameter size: ignored. It will be overriden by `fontSize` property.
102102
/// - Returns: instance of the font
103-
var attributes: [NSAttributedStringKey:Any] {
103+
var attributes: [NSAttributedString.Key:Any] {
104104
guard !self.explicitFont else {
105105
return [:]
106106
}
@@ -139,8 +139,8 @@ public class FontData {
139139
/// - currentFont: current font.
140140
/// - currentSize: current font size.
141141
/// - Returns: attributes
142-
public func attributes(currentFont: FontConvertible?, size currentSize: CGFloat?) -> [NSAttributedStringKey:Any] {
143-
var finalAttributes: [NSAttributedStringKey:Any] = [:]
142+
public func attributes(currentFont: FontConvertible?, size currentSize: CGFloat?) -> [NSAttributedString.Key:Any] {
143+
var finalAttributes: [NSAttributedString.Key:Any] = [:]
144144

145145
// generate an initial font from passed FontConvertible instance
146146
guard let size = (self.size ?? currentSize) else { return [:] }

Sources/SwiftRichString/Extensions/AttributedString+Ext.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public extension AttributedString {
146146
/// - range: range of substring where style will be removed, `nil` to use the entire string.
147147
/// - Returns: same instance of the receiver with - eventually - modified attributes.
148148
@discardableResult
149-
public func removeAttributes(_ keys: [NSAttributedStringKey], range: NSRange) -> Self {
149+
public func removeAttributes(_ keys: [NSAttributedString.Key], range: NSRange) -> Self {
150150
keys.forEach { self.removeAttribute($0, range: range) }
151151
return self
152152
}

Sources/SwiftRichString/Style/Style.swift

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ public class Style: StyleProtocol {
5050

5151
/// Attributes defined by the style. This is the dictionary modified when you
5252
/// set a style attributed.
53-
private var innerAttributes: [NSAttributedStringKey : Any] = [:]
53+
private var innerAttributes: [NSAttributedString.Key : Any] = [:]
5454

5555
/// This is a cache array used to avoid the evaluation of font description and other
5656
/// sensitive data. Cache is invalidated automatically when needed.
57-
private var cachedAttributes: [NSAttributedStringKey : Any]? = nil
57+
private var cachedAttributes: [NSAttributedString.Key : Any]? = nil
5858

5959
//MARK: - PROPERTIES
6060

@@ -347,14 +347,14 @@ public class Style: StyleProtocol {
347347

348348
/// Enable spoken of all punctuation in the text.
349349
public var speaksPunctuation: Bool? {
350-
set { self.set(attribute: newValue, forKey: NSAttributedStringKey(UIAccessibilitySpeechAttributePunctuation)) }
351-
get { return self.get(attributeForKey: NSAttributedStringKey(UIAccessibilitySpeechAttributePunctuation)) }
350+
set { self.set(attribute: newValue, forKey: NSAttributedString.Key(convertFromNSAttributedStringKey(NSAttributedString.Key.accessibilitySpeechPunctuation))) }
351+
get { return self.get(attributeForKey: NSAttributedString.Key(convertFromNSAttributedStringKey(NSAttributedString.Key.accessibilitySpeechPunctuation))) }
352352
}
353353

354354
/// The language to use when speaking a string (value is a BCP 47 language code string).
355355
public var speakingLanguage: String? {
356-
set { self.set(attribute: newValue, forKey: NSAttributedStringKey(UIAccessibilitySpeechAttributeLanguage)) }
357-
get { return self.get(attributeForKey: NSAttributedStringKey(UIAccessibilitySpeechAttributeLanguage)) }
356+
set { self.set(attribute: newValue, forKey: NSAttributedString.Key(convertFromNSAttributedStringKey(NSAttributedString.Key.accessibilitySpeechLanguage))) }
357+
get { return self.get(attributeForKey: NSAttributedString.Key(convertFromNSAttributedStringKey(NSAttributedString.Key.accessibilitySpeechLanguage))) }
358358
}
359359

360360
/// Pitch to apply to spoken content. Value must be in range range 0.0 to 2.0.
@@ -364,16 +364,16 @@ public class Style: StyleProtocol {
364364
///
365365
/// The default value for this attribute is 1.0, which indicates a normal pitch.
366366
public var speakingPitch: Double? {
367-
set { self.set(attribute: newValue, forKey: NSAttributedStringKey(UIAccessibilitySpeechAttributePitch)) }
368-
get { return self.get(attributeForKey: NSAttributedStringKey(UIAccessibilitySpeechAttributePitch)) }
367+
set { self.set(attribute: newValue, forKey: NSAttributedString.Key(convertFromNSAttributedStringKey(NSAttributedString.Key.accessibilitySpeechPitch))) }
368+
get { return self.get(attributeForKey: NSAttributedString.Key(convertFromNSAttributedStringKey(NSAttributedString.Key.accessibilitySpeechPitch))) }
369369
}
370370

371371
/// No overview available.
372372
/// Note: available only from iOS 11, tvOS 11 and watchOS 4.
373373
@available(iOS 11.0, tvOS 11.0, iOSApplicationExtension 11.0, watchOS 4, *)
374374
public var speakingPronunciation: String? {
375-
set { self.set(attribute: newValue, forKey: NSAttributedStringKey(UIAccessibilitySpeechAttributeIPANotation)) }
376-
get { return self.get(attributeForKey: NSAttributedStringKey(UIAccessibilitySpeechAttributeIPANotation)) }
375+
set { self.set(attribute: newValue, forKey: NSAttributedString.Key(convertFromNSAttributedStringKey(NSAttributedString.Key.accessibilitySpeechIPANotation))) }
376+
get { return self.get(attributeForKey: NSAttributedString.Key(convertFromNSAttributedStringKey(NSAttributedString.Key.accessibilitySpeechIPANotation))) }
377377
}
378378

379379
/// Spoken text is queued behind, or interrupts, existing spoken content.
@@ -384,15 +384,15 @@ public class Style: StyleProtocol {
384384
@available(iOS 11.0, tvOS 11.0, iOSApplicationExtension 11.0, watchOS 4, *)
385385
public var shouldQueueSpeechAnnouncement: Bool? {
386386
set {
387-
let key = NSAttributedStringKey(UIAccessibilitySpeechAttributeQueueAnnouncement)
387+
let key = NSAttributedString.Key(convertFromNSAttributedStringKey(NSAttributedString.Key.accessibilitySpeechQueueAnnouncement))
388388
guard let v = newValue else {
389389
self.innerAttributes.removeValue(forKey: key)
390390
return
391391
}
392392
self.set(attribute: NSNumber.init(value: v), forKey: key)
393393
}
394394
get {
395-
let key = NSAttributedStringKey(UIAccessibilitySpeechAttributeQueueAnnouncement)
395+
let key = NSAttributedString.Key(convertFromNSAttributedStringKey(NSAttributedString.Key.accessibilitySpeechQueueAnnouncement))
396396
if let n: NSNumber = self.get(attributeForKey: key) {
397397
return n.boolValue
398398
} else { return false }
@@ -405,15 +405,15 @@ public class Style: StyleProtocol {
405405
@available(iOS 11.0, tvOS 11.0, iOSApplicationExtension 11.0, watchOS 4, *)
406406
public var headingLevel: HeadingLevel? {
407407
set {
408-
let key = NSAttributedStringKey(UIAccessibilityTextAttributeHeadingLevel)
408+
let key = NSAttributedString.Key(convertFromNSAttributedStringKey(NSAttributedString.Key.accessibilityTextHeadingLevel))
409409
guard let v = newValue else {
410410
self.innerAttributes.removeValue(forKey: key)
411411
return
412412
}
413413
self.set(attribute: v.rawValue, forKey: key)
414414
}
415415
get {
416-
let key = NSAttributedStringKey(UIAccessibilityTextAttributeHeadingLevel)
416+
let key = NSAttributedString.Key(convertFromNSAttributedStringKey(NSAttributedString.Key.accessibilityTextHeadingLevel))
417417
if let n: Int = self.get(attributeForKey: key) {
418418
return HeadingLevel(rawValue: n)
419419
} else { return nil }
@@ -546,7 +546,7 @@ public class Style: StyleProtocol {
546546
/// Font related attributes are not set automatically but are encapsulasted to the font object itself.
547547
///
548548
/// - Parameter dictionary: dictionary to set
549-
public init(dictionary: [NSAttributedStringKey: Any]?) {
549+
public init(dictionary: [NSAttributedString.Key: Any]?) {
550550
self.fontData?.style = self
551551
if let font = dictionary?[.font] as? Font {
552552
self.fontData?.font = font
@@ -578,7 +578,7 @@ public class Style: StyleProtocol {
578578
/// - Parameters:
579579
/// - value: valid value to set, `nil` to remove exiting value for given key.
580580
/// - key: key to set
581-
public func set<T>(attribute value: T?, forKey key: NSAttributedStringKey) {
581+
public func set<T>(attribute value: T?, forKey key: NSAttributedString.Key) {
582582
guard let value = value else {
583583
self.innerAttributes.removeValue(forKey: key)
584584
return
@@ -591,12 +591,12 @@ public class Style: StyleProtocol {
591591
///
592592
/// - Parameter key: key to read.
593593
/// - Returns: value or `nil` if value is not set.
594-
public func get<T>(attributeForKey key: NSAttributedStringKey) -> T? {
594+
public func get<T>(attributeForKey key: NSAttributedString.Key) -> T? {
595595
return (self.innerAttributes[key] as? T)
596596
}
597597

598598
/// Return all attributes defined by the style.
599-
public var attributes: [NSAttributedStringKey : Any] {
599+
public var attributes: [NSAttributedString.Key : Any] {
600600
if let cachedAttributes = self.cachedAttributes {
601601
return cachedAttributes
602602
}
@@ -617,3 +617,8 @@ public class Style: StyleProtocol {
617617
return styleCopy
618618
}
619619
}
620+
621+
// Helper function inserted by Swift 4.2 migrator.
622+
fileprivate func convertFromNSAttributedStringKey(_ input: NSAttributedString.Key) -> String {
623+
return input.rawValue
624+
}

Sources/SwiftRichString/Style/StyleGroup.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,11 @@ public class StyleGroup: StyleProtocol {
8383

8484
/// Return all attributes merge of each single `Style` of the group.
8585
/// Attributes are reported in order of the insertion regardeless the associated name.
86-
public var attributes: [NSAttributedStringKey : Any] {
87-
var composedAttributes: [NSAttributedStringKey: Any] = [:]
88-
self.styles.enumerated().forEach { (_,style) in
86+
public var attributes: [NSAttributedString.Key : Any] {
87+
var composedAttributes: [NSAttributedString.Key: Any] = [:]
88+
self.styles.enumerated().forEach { (arg) in
89+
90+
let (_, style) = arg
8991
composedAttributes.merge(style.attributes, uniquingKeysWith: { (_, new) in return new })
9092
}
9193
return composedAttributes
@@ -257,7 +259,7 @@ public extension Array where Array.Element == StyleProtocol {
257259
///
258260
/// - Returns: merged style
259261
public func mergeStyle() -> Style {
260-
var attributes: [NSAttributedStringKey:Any] = [:]
262+
var attributes: [NSAttributedString.Key:Any] = [:]
261263
self.forEach { attributes.merge($0.attributes, uniquingKeysWith: { (_, new) in return new }) }
262264
return Style(dictionary: attributes)
263265
}

Sources/SwiftRichString/Style/StyleProtocol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public typealias AttributedString = NSMutableAttributedString
3535
public protocol StyleProtocol: class {
3636

3737
/// Return the attributes of the style in form of dictionary `NSAttributedStringKey`/`Any`.
38-
var attributes: [NSAttributedStringKey : Any] { get }
38+
var attributes: [NSAttributedString.Key : Any] { get }
3939

4040
/// Font unique attributes dictionary.
4141
var fontData: FontData? { get }

Sources/SwiftRichString/Style/StyleRegEx.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class StyleRegEx: StyleProtocol {
5151
private var style: StyleProtocol
5252

5353
/// Style attributes
54-
public var attributes: [NSAttributedStringKey : Any] {
54+
public var attributes: [NSAttributedString.Key : Any] {
5555
return self.style.attributes
5656
}
5757

SwiftRichString.xcodeproj/project.pbxproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -853,11 +853,11 @@
853853
TargetAttributes = {
854854
52D6D97B1BEFF229002C0205 = {
855855
CreatedOnToolsVersion = 7.1;
856-
LastSwiftMigration = 0930;
856+
LastSwiftMigration = 1000;
857857
};
858858
52D6D9851BEFF229002C0205 = {
859859
CreatedOnToolsVersion = 7.1;
860-
LastSwiftMigration = 0800;
860+
LastSwiftMigration = 1000;
861861
};
862862
52D6D9E11BEFFF6E002C0205 = {
863863
CreatedOnToolsVersion = 7.1;
@@ -1471,7 +1471,7 @@
14711471
PRODUCT_NAME = SwiftRichString;
14721472
SKIP_INSTALL = YES;
14731473
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
1474-
SWIFT_VERSION = 4.0;
1474+
SWIFT_VERSION = 4.2;
14751475
};
14761476
name = Debug;
14771477
};
@@ -1493,7 +1493,7 @@
14931493
PRODUCT_NAME = SwiftRichString;
14941494
SKIP_INSTALL = YES;
14951495
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
1496-
SWIFT_VERSION = 4.0;
1496+
SWIFT_VERSION = 4.2;
14971497
};
14981498
name = Release;
14991499
};
@@ -1507,7 +1507,7 @@
15071507
PRODUCT_BUNDLE_IDENTIFIER = "com.SwiftRichString.SwiftRichString-iOS-Tests";
15081508
PRODUCT_NAME = "$(TARGET_NAME)";
15091509
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
1510-
SWIFT_VERSION = 4.0;
1510+
SWIFT_VERSION = 4.2;
15111511
};
15121512
name = Debug;
15131513
};
@@ -1521,7 +1521,7 @@
15211521
PRODUCT_BUNDLE_IDENTIFIER = "com.SwiftRichString.SwiftRichString-iOS-Tests";
15221522
PRODUCT_NAME = "$(TARGET_NAME)";
15231523
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
1524-
SWIFT_VERSION = 4.0;
1524+
SWIFT_VERSION = 4.2;
15251525
};
15261526
name = Release;
15271527
};

0 commit comments

Comments
 (0)