Swift

Swift4 字符串

2017-08-31  本文已影响2377人  小笨憨

字符串截取

public extension UIColor {
    
    public class func colorWithString(hex:String) -> UIColor {
        
        var cString = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()
        if cString.hasPrefix("#"){
            
            let index   = cString.index(cString.startIndex, offsetBy: 1)
            cString     = cString.substring(from: index)
        }
        if cString.characters.count != 6 {
            
            return UIColor.red
        }
        
        let rIndex      = cString.index(cString.startIndex, offsetBy: 2)
        let rString     = cString.substring(to: rIndex)
        let otherString = cString.substring(from: rIndex)
        let gIndex      = otherString.index(otherString.startIndex, offsetBy: 2)
        let gString     = otherString.substring(to: gIndex)
        let bIndex      = cString.index(cString.endIndex, offsetBy: -2)
        let bString     = cString.substring(from: bIndex)
        
        var r:CUnsignedInt  = 0,g:CUnsignedInt = 0 ,b:CUnsignedInt = 0
        Scanner(string: rString).scanHexInt32(&r)
        Scanner(string: gString).scanHexInt32(&g)
        Scanner(string: bString).scanHexInt32(&b)
        return UIColor(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: CGFloat(1))
    }
}
extension UIColor {
    
    var cString = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()
        if cString.hasPrefix("#"){
            
            let index   = cString.index(cString.startIndex, offsetBy: 1)
            cString     = String(cString.prefix(upTo: index))
        }
        if cString.count != 6 {
            return UIColor.red
        }
        
        let rIndex      = cString.index(cString.startIndex, offsetBy: 2)
        let rString     = cString.prefix(upTo: rIndex)
        
        let otherString = cString.suffix(from: rIndex)
        let gIndex      = otherString.index(otherString.startIndex, offsetBy: 2)
        let gString     = otherString.prefix(upTo: gIndex)
        let bIndex      = cString.index(cString.endIndex, offsetBy: -2)
        let bString     = cString.suffix(from: bIndex)
        
        var r:CUnsignedInt  = 0,g:CUnsignedInt = 0 ,b:CUnsignedInt = 0
        Scanner(string: String(rString)).scanHexInt32(&r)
        Scanner(string: String(gString)).scanHexInt32(&g)
        Scanner(string: String(bString)).scanHexInt32(&b)
        return UIColor(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: CGFloat(1))
   }
}

多行字符串

func printHelp() {
    print(
        """
        🚘  Test Drive
        --------------
        Quickly try out any Swift pod or framework in a playground.

        Usage:
        - Simply pass a list of pod names or URLs that you want to test drive.
        - You can also specify a platform (iOS, macOS or tvOS) using the '-p' option
        - To use a specific version or branch, use the '-v' argument (or '-m' for master)

        Examples:
        - testdrive Unbox Wrap Files
        - testdrive https://github.com/johnsundell/unbox.git Wrap Files
        - testdrive Unbox -p tvOS
        - testdrive Unbox -v 2.3.0
        - testdrive Unbox -v swift3
        """
    )
}

待补充。。。

上一篇 下一篇

猜你喜欢

热点阅读