Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • A simple function returning UIColor from HexaColor String in swift

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 394
    Comment on it

    Here are step-by-step guide to obtain UIColor:

    1- Firstly you need to do is to remove all spaces and new line character sets from the string and then covert the string to UPPER Case example AABBCC.

    var cString:String = hexColorString.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).uppercaseString


    2-  Check if hexastring has hash(#) or not, If yes, then remove #.

     if (cString.hasPrefix("#")) {
          cString = (cString as NSString).substringFromIndex(1)
        }

     

    3- If obtained string has character count 6 then start trimming the hexa string in three parts else return any default color.

     if (cString.characters.count != 6) {
          return UIColor.grayColor() // Returning default graycolor, one can change to any color
        }
       
        let rString = (cString as NSString).substringToIndex(2) // first two are red
        let gString = ((cString as NSString).substringFromIndex(2) as NSString).substringToIndex(2) // next two are green
        let bString = ((cString as NSString).substringFromIndex(4) as NSString).substringToIndex(2) // rest are blue


    4- Now use NSScanner class to converts the characters of an NSString object into number and string values. Once it’s done we can call “scanHexInt()” function that scans for an unsigned value from a hexadecimal representation, returning a found value by reference.

    var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;
        NSScanner(string: rString).scanHexInt(&r)
        NSScanner(string: gString).scanHexInt(&g)
        NSScanner(string: bString).scanHexInt(&b)

     

    5- Now you have unsigned value for all R,G,B which can be used to obtain UIColor as depicted below:

    UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(1))
    
    Code Snippet
    
      func colorWithHexString (hexColorString:String) -> UIColor {
        // removing all spaces and new line character set and then coverting the string to UPPERCASE. example AABBCC
        var cString:String = hexColorString.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).uppercaseString
       
        // checking if hex string contains #, If yes then consider the screen from index 1 otherwise 0
        if (cString.hasPrefix("#")) {
          cString = (cString as NSString).substringFromIndex(1)
        }
       
        // checking length of hexacolor, it should always be 6 else default color(say gray color) will be returned. one can change it to any color.
        if (cString.characters.count != 6) {
          return UIColor.grayColor()
        }
       
        let rString = (cString as NSString).substringToIndex(2) // first two are red
        let gString = ((cString as NSString).substringFromIndex(2) as NSString).substringToIndex(2) // next two are green
        let bString = ((cString as NSString).substringFromIndex(4) as NSString).substringToIndex(2) // rest are blue
       
        var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;
        NSScanner(string: rString).scanHexInt(&r)
        NSScanner(string: gString).scanHexInt(&g)
        NSScanner(string: bString).scanHexInt(&b)
       
        return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(1))
      }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: