From bfc14516f0c54209034a9b6e6d9b67aa372dfbbc Mon Sep 17 00:00:00 2001 From: Matteo Crippa Date: Fri, 2 Jun 2017 20:34:49 +0200 Subject: [PATCH] update for latest swift 3.1 --- .../NSMutableAttributedOperators.swift | 10 +- ...leAttributedString+ChainedAttributes.swift | 456 +++++++++--------- Example/Example.xcodeproj/project.pbxproj | 9 +- 3 files changed, 241 insertions(+), 234 deletions(-) diff --git a/ChainedAttributedString/NSMutableAttributedOperators.swift b/ChainedAttributedString/NSMutableAttributedOperators.swift index d8ed674..f702852 100644 --- a/ChainedAttributedString/NSMutableAttributedOperators.swift +++ b/ChainedAttributedString/NSMutableAttributedOperators.swift @@ -9,9 +9,9 @@ import Foundation public func +(left:NSMutableAttributedString, right:NSMutableAttributedString) -> NSMutableAttributedString { - - let resultString = left - left.append(right) - - return resultString + + let resultString = left + left.append(right) + + return resultString } diff --git a/ChainedAttributedString/NSMutableAttributedString+ChainedAttributes.swift b/ChainedAttributedString/NSMutableAttributedString+ChainedAttributes.swift index 1eac828..559fd81 100644 --- a/ChainedAttributedString/NSMutableAttributedString+ChainedAttributes.swift +++ b/ChainedAttributedString/NSMutableAttributedString+ChainedAttributes.swift @@ -10,249 +10,249 @@ import Foundation import UIKit public extension NSMutableAttributedString { + + // MARK: Chained attributes + + /** + This function adds text color attribute to attributed string. + + - warning: If text passed in "text" parameter is not found, attribute will be applied to whole attributed string. Only first occurence of "text" is styled. + + - parameter value - UIColor which should be applied as text color + - parameter text - String for which should be applied text color (optional, default = whole attributed string) + + - returns: Modified NSMutableAttributedString + */ + func textColor(value:UIColor, forText text:String? = nil) -> NSMutableAttributedString { - // MARK: Chained attributes - - /** - This function adds text color attribute to attributed string. - - - warning: If text passed in "text" parameter is not found, attribute will be applied to whole attributed string. Only first occurence of "text" is styled. - - - parameter value - UIColor which should be applied as text color - - parameter text - String for which should be applied text color (optional, default = whole attributed string) - - - returns: Modified NSMutableAttributedString - */ - func textColor(_ value:UIColor, forText text:String? = nil) -> NSMutableAttributedString { - - var attributeRange:NSRange? = nil - if let textForAttribute = text { - - attributeRange = self.getRangeOfStringInSelf(textForAttribute) - } - - self.applyAttribute(NSForegroundColorAttributeName, withValue: value, forRange: attributeRange) - - return self + var attributeRange:NSRange? = nil + if let textForAttribute = text { + + attributeRange = self.getRangeOfStringInSelf(value: textForAttribute) } - /** - This function adds font attribute to attributed string. - - - warning: If text passed in "text" parameter is not found, attribute will be applied to whole attributed string. Only first occurence of "text" is styled. - - - parameter value - UIFont which should be applied as font for attributed string - - parameter text - String for which should be applied font (optional, default = whole attributed string) - - - returns: Modified NSMutableAttributedString - */ - func font(_ value:UIFont, forText text:String? = nil) -> NSMutableAttributedString { - - var attributeRange:NSRange? = nil - if let textForAttribute = text { - - attributeRange = self.getRangeOfStringInSelf(textForAttribute) - } - - self.applyAttribute(NSFontAttributeName, withValue: value, forRange: attributeRange) - - return self - } - - /** - This function adds background color attribute to attributed string. - - - warning: If text passed in "text" parameter is not found, attribute will be applied to whole attributed string. Only first occurence of "text" is styled. - - - parameter value - UIColor which should be applied as background color for attributed string - - parameter text - String for which should be applied font (optional, default = whole attributed string) - - - returns: Modified NSMutableAttributedString - */ - func backgroundColor(_ value:UIColor, forText text:String? = nil) -> NSMutableAttributedString { - - var attributeRange:NSRange? = nil - if let textForAttribute = text { - - attributeRange = self.getRangeOfStringInSelf(textForAttribute) - } - - self.applyAttribute(NSBackgroundColorAttributeName, withValue: value, forRange: attributeRange) - - return self - } - - /** - This function adds kern spacing attribute to attributed string. - - - warning: If text passed in "text" parameter is not found, attribute will be applied to whole attributed string. Only first occurence of "text" is styled. - - - parameter value - CGFloat which should be applied as kern spacing for attributed string - - parameter text - String for which should be applied font (optional, default = whole attributed string) - - - returns: Modified NSMutableAttributedString - */ - func kernSpacing(_ value:CGFloat, forText text:String? = nil) -> NSMutableAttributedString { - - var attributeRange:NSRange? = nil - if let textForAttribute = text { - - attributeRange = self.getRangeOfStringInSelf(textForAttribute) - } - - self.applyAttribute(NSKernAttributeName, withValue: value as AnyObject, forRange: attributeRange) - - return self - } - - /** - This function adds underline attribute to attributed string. - - - warning: If text passed in "text" parameter is not found, attribute will be applied to whole attributed string. Only first occurence of "text" is styled. - - - parameter value - CGFloat which should be applied as underline for attributed string - - parameter text - String for which should be applied font (optional, default = whole attributed string) - - - returns: Modified NSMutableAttributedString - */ - func underline(_ value:CGFloat, forText text:String? = nil) -> NSMutableAttributedString { - - var attributeRange:NSRange? = nil - if let textForAttribute = text { - - attributeRange = self.getRangeOfStringInSelf(textForAttribute) - } - - self.applyAttribute(NSUnderlineStyleAttributeName, withValue: value as AnyObject, forRange: attributeRange) - - return self - } - - /** - This function adds underline color attribute to attributed string. - - - warning: If text passed in "text" parameter is not found, attribute will be applied to whole attributed string. Only first occurence of "text" is styled. - - - parameter value - UIColor which should be applied as underline color for attributed string - - parameter text - String for which should be applied font (optional, default = whole attributed string) - - - returns: Modified NSMutableAttributedString - */ - func underlineColor(_ value:UIColor, forText text:String? = nil) -> NSMutableAttributedString { - - var attributeRange:NSRange? = nil - if let textForAttribute = text { - - attributeRange = self.getRangeOfStringInSelf(textForAttribute) - } - - self.applyAttribute(NSUnderlineColorAttributeName, withValue: value, forRange: attributeRange) - - return self + self.applyAttribute(attributeName: NSForegroundColorAttributeName, withValue: value, forRange: attributeRange) + + return self + } + + /** + This function adds font attribute to attributed string. + + - warning: If text passed in "text" parameter is not found, attribute will be applied to whole attributed string. Only first occurence of "text" is styled. + + - parameter value - UIFont which should be applied as font for attributed string + - parameter text - String for which should be applied font (optional, default = whole attributed string) + + - returns: Modified NSMutableAttributedString + */ + func font(value:UIFont, forText text:String? = nil) -> NSMutableAttributedString { + + var attributeRange:NSRange? = nil + if let textForAttribute = text { + + attributeRange = self.getRangeOfStringInSelf(value: textForAttribute) } - - /** - This function adds strike through attribute to attributed string. - - - warning: If text passed in "text" parameter is not found, attribute will be applied to whole attributed string. Only first occurence of "text" is styled. - - - parameter value - CGFloat which should be applied as strike through for attributed string - - parameter text - String for which should be applied font (optional, default = whole attributed string) - - - returns: Modified NSMutableAttributedString - */ - func strikeThrough(_ value:CGFloat, forText text:String? = nil) -> NSMutableAttributedString { - - var attributeRange:NSRange? = nil - if let textForAttribute = text { - - attributeRange = self.getRangeOfStringInSelf(textForAttribute) - } - - self.applyAttribute(NSStrikethroughStyleAttributeName, withValue: value as AnyObject, forRange: attributeRange) - - return self + + self.applyAttribute(attributeName: NSFontAttributeName, withValue: value, forRange: attributeRange) + + return self + } + + /** + This function adds background color attribute to attributed string. + + - warning: If text passed in "text" parameter is not found, attribute will be applied to whole attributed string. Only first occurence of "text" is styled. + + - parameter value - UIColor which should be applied as background color for attributed string + - parameter text - String for which should be applied font (optional, default = whole attributed string) + + - returns: Modified NSMutableAttributedString + */ + func backgroundColor(value:UIColor, forText text:String? = nil) -> NSMutableAttributedString { + + var attributeRange:NSRange? = nil + if let textForAttribute = text { + + attributeRange = self.getRangeOfStringInSelf(value: textForAttribute) } - - /** - This function adds strike through color attribute to attributed string. - - - warning: If text passed in "text" parameter is not found, attribute will be applied to whole attributed string. Only first occurence of "text" is styled. - - - parameter value - UIColor which should be applied as strike through color for attributed string - - parameter text - String for which should be applied font (optional, default = whole attributed string) - - - returns: Modified NSMutableAttributedString - */ - func strikeThroughColor(_ value:UIColor, forText text:String? = nil) -> NSMutableAttributedString { - - var attributeRange:NSRange? = nil - if let textForAttribute = text { - - attributeRange = self.getRangeOfStringInSelf(textForAttribute) - } - - self.applyAttribute(NSStrikethroughColorAttributeName, withValue: value, forRange: attributeRange) - - return self + + self.applyAttribute(attributeName: NSBackgroundColorAttributeName, withValue: value, forRange: attributeRange) + + return self + } + + /** + This function adds kern spacing attribute to attributed string. + + - warning: If text passed in "text" parameter is not found, attribute will be applied to whole attributed string. Only first occurence of "text" is styled. + + - parameter value - CGFloat which should be applied as kern spacing for attributed string + - parameter text - String for which should be applied font (optional, default = whole attributed string) + + - returns: Modified NSMutableAttributedString + */ + func kernSpacing(value:CGFloat, forText text:String? = nil) -> NSMutableAttributedString { + + var attributeRange:NSRange? = nil + if let textForAttribute = text { + + attributeRange = self.getRangeOfStringInSelf(value: textForAttribute) } - - /** - This function adds link attribute to attributed string. - - - warning: If text passed in "text" parameter is not found, attribute will be applied to whole attributed string. Only first occurence of "text" is styled. - - - parameter value - UIColor which should be applied as link for attributed string - - parameter text - String for which should be applied font (optional, default = whole attributed string) - - - returns: Modified NSMutableAttributedString - */ - func link(_ value:String, forText text:String? = nil) -> NSMutableAttributedString { - - var attributeRange:NSRange? = nil - if let textForAttribute = text { - - attributeRange = self.getRangeOfStringInSelf(textForAttribute) - } - - self.applyAttribute(NSLinkAttributeName, withValue: value as AnyObject, forRange: attributeRange) - - return self + + self.applyAttribute(attributeName: NSKernAttributeName, withValue: value as AnyObject, forRange: attributeRange) + + return self + } + + /** + This function adds underline attribute to attributed string. + + - warning: If text passed in "text" parameter is not found, attribute will be applied to whole attributed string. Only first occurence of "text" is styled. + + - parameter value - CGFloat which should be applied as underline for attributed string + - parameter text - String for which should be applied font (optional, default = whole attributed string) + + - returns: Modified NSMutableAttributedString + */ + func underline(value:CGFloat, forText text:String? = nil) -> NSMutableAttributedString { + + var attributeRange:NSRange? = nil + if let textForAttribute = text { + + attributeRange = self.getRangeOfStringInSelf(value: textForAttribute) } - - // MARK: Clear attributes - - /** - This function removes all attributes added to this attributed string. - - - returns: Modified NSMutableAttributedString - */ - func clearAllAttributes() -> NSMutableAttributedString { - - self.setAttributes(nil, range: self.getRangeOfSelf()) - - return self + + self.applyAttribute(attributeName: NSUnderlineStyleAttributeName, withValue: value as AnyObject, forRange: attributeRange) + + return self + } + + /** + This function adds underline color attribute to attributed string. + + - warning: If text passed in "text" parameter is not found, attribute will be applied to whole attributed string. Only first occurence of "text" is styled. + + - parameter value - UIColor which should be applied as underline color for attributed string + - parameter text - String for which should be applied font (optional, default = whole attributed string) + + - returns: Modified NSMutableAttributedString + */ + func underlineColor(value:UIColor, forText text:String? = nil) -> NSMutableAttributedString { + + var attributeRange:NSRange? = nil + if let textForAttribute = text { + + attributeRange = self.getRangeOfStringInSelf(value: textForAttribute) } - // MARK: Range + self.applyAttribute(attributeName: NSUnderlineColorAttributeName, withValue: value, forRange: attributeRange) + + return self + } + + /** + This function adds strike through attribute to attributed string. + + - warning: If text passed in "text" parameter is not found, attribute will be applied to whole attributed string. Only first occurence of "text" is styled. + + - parameter value - CGFloat which should be applied as strike through for attributed string + - parameter text - String for which should be applied font (optional, default = whole attributed string) + + - returns: Modified NSMutableAttributedString + */ + func strikeThrough(value:CGFloat, forText text:String? = nil) -> NSMutableAttributedString { - fileprivate func getRangeOfSelf() -> NSRange { - - return NSRange(location: 0, length: self.length) + var attributeRange:NSRange? = nil + if let textForAttribute = text { + + attributeRange = self.getRangeOfStringInSelf(value: textForAttribute) } - fileprivate func getRangeOfStringInSelf(_ value:String) -> NSRange? { - - let range = (self.string as NSString).range(of: value) - return range.location == NSNotFound ? nil : range + self.applyAttribute(attributeName: NSStrikethroughStyleAttributeName, withValue: value as AnyObject, forRange: attributeRange) + + return self + } + + /** + This function adds strike through color attribute to attributed string. + + - warning: If text passed in "text" parameter is not found, attribute will be applied to whole attributed string. Only first occurence of "text" is styled. + + - parameter value - UIColor which should be applied as strike through color for attributed string + - parameter text - String for which should be applied font (optional, default = whole attributed string) + + - returns: Modified NSMutableAttributedString + */ + func strikeThroughColor(value:UIColor, forText text:String? = nil) -> NSMutableAttributedString { + + var attributeRange:NSRange? = nil + if let textForAttribute = text { + + attributeRange = self.getRangeOfStringInSelf(value: textForAttribute) } - // MARK: Applying attributes + self.applyAttribute(attributeName: NSStrikethroughColorAttributeName, withValue: value, forRange: attributeRange) + + return self + } + + /** + This function adds link attribute to attributed string. + + - warning: If text passed in "text" parameter is not found, attribute will be applied to whole attributed string. Only first occurence of "text" is styled. + + - parameter value - UIColor which should be applied as link for attributed string + - parameter text - String for which should be applied font (optional, default = whole attributed string) + + - returns: Modified NSMutableAttributedString + */ + func link(value:String, forText text:String? = nil) -> NSMutableAttributedString { - fileprivate func applyAttribute(_ attributeName:String, withValue value:AnyObject, forRange range:NSRange? = nil) { - - let attributeRange = range ?? self.getRangeOfSelf() - self.addAttribute(attributeName, value: value, range: attributeRange) + var attributeRange:NSRange? = nil + if let textForAttribute = text { + + attributeRange = self.getRangeOfStringInSelf(value: textForAttribute) } + self.applyAttribute(attributeName: NSLinkAttributeName, withValue: value as AnyObject, forRange: attributeRange) + + return self + } + + // MARK: Clear attributes + + /** + This function removes all attributes added to this attributed string. + + - returns: Modified NSMutableAttributedString + */ + func clearAllAttributes() -> NSMutableAttributedString { + + self.setAttributes(nil, range: self.getRangeOfSelf()) + + return self + } + + // MARK: Range + + private func getRangeOfSelf() -> NSRange { + + return NSRange(location: 0, length: self.length) + } + + private func getRangeOfStringInSelf(value:String) -> NSRange? { + + let range = (self.string as NSString).range(of: value) + return range.location == NSNotFound ? nil : range + } + + // MARK: Applying attributes + + private func applyAttribute(attributeName:String, withValue value:AnyObject, forRange range:NSRange? = nil) { + + let attributeRange = range ?? self.getRangeOfSelf() + self.addAttribute(attributeName, value: value, range: attributeRange) + } + } diff --git a/Example/Example.xcodeproj/project.pbxproj b/Example/Example.xcodeproj/project.pbxproj index 147288c..f22b855 100644 --- a/Example/Example.xcodeproj/project.pbxproj +++ b/Example/Example.xcodeproj/project.pbxproj @@ -127,7 +127,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0730; - LastUpgradeCheck = 0730; + LastUpgradeCheck = 0830; ORGANIZATIONNAME = Example; TargetAttributes = { E0C323FD1D2463D000712168 = { @@ -265,8 +265,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -311,8 +313,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -331,6 +335,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 9.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; }; @@ -340,6 +345,7 @@ isa = XCBuildConfiguration; baseConfigurationReference = 1B5DE12EDCA817CEB23756C0 /* Pods-Example.debug.xcconfig */; buildSettings = { + ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; DEVELOPMENT_TEAM = ""; INFOPLIST_FILE = Example/Info.plist; @@ -355,6 +361,7 @@ isa = XCBuildConfiguration; baseConfigurationReference = FA3411A5A628154C440137FA /* Pods-Example.release.xcconfig */; buildSettings = { + ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; DEVELOPMENT_TEAM = ""; INFOPLIST_FILE = Example/Info.plist;