Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1108419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:04:48+00:00 2026-05-17T02:04:48+00:00

I have an RGB hex code like #ffffff as NSString and want to convert

  • 0

I have an RGB hex code like #ffffff as NSString and want to convert that into an UIColor. Is there a simple way to do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T02:04:48+00:00Added an answer on May 17, 2026 at 2:04 am

    In some code of mine, I use 2 different functions:

    void SKScanHexColor(NSString * hexString, float * red, float * green, float * blue, float * alpha) {
      NSString *cleanString = [hexString stringByReplacingOccurrencesOfString:@"#" withString:@""];
      if([cleanString length] == 3) {
          cleanString = [NSString stringWithFormat:@"%@%@%@%@%@%@", 
                         [cleanString substringWithRange:NSMakeRange(0, 1)],[cleanString substringWithRange:NSMakeRange(0, 1)],
                         [cleanString substringWithRange:NSMakeRange(1, 1)],[cleanString substringWithRange:NSMakeRange(1, 1)],
                         [cleanString substringWithRange:NSMakeRange(2, 1)],[cleanString substringWithRange:NSMakeRange(2, 1)]];
      }
      if([cleanString length] == 6) {
          cleanString = [cleanString stringByAppendingString:@"ff"];
      }
    
      unsigned int baseValue;
      [[NSScanner scannerWithString:cleanString] scanHexInt:&baseValue];
    
      if (red) { *red = ((baseValue >> 24) & 0xFF)/255.0f; }
      if (green) { *green = ((baseValue >> 16) & 0xFF)/255.0f; }
      if (blue) { *blue = ((baseValue >> 8) & 0xFF)/255.0f; }
      if (alpha) { *alpha = ((baseValue >> 0) & 0xFF)/255.0f; }
    }
    

    And then I use it like this:

    UIColor * SKColorFromHexString(NSString * hexString) {
      float red, green, blue, alpha;
      SKScanHexColor(hexString, &red, &green, &blue, &alpha);
    
      return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
    }
    

    If you prefer to use this as a UIColor category, then it’s just a matter of altering a few lines:

    + (UIColor *) colorFromHexString:(NSString *)hexString {
      NSString *cleanString = [hexString stringByReplacingOccurrencesOfString:@"#" withString:@""];
      if([cleanString length] == 3) {
          cleanString = [NSString stringWithFormat:@"%@%@%@%@%@%@", 
                         [cleanString substringWithRange:NSMakeRange(0, 1)],[cleanString substringWithRange:NSMakeRange(0, 1)],
                         [cleanString substringWithRange:NSMakeRange(1, 1)],[cleanString substringWithRange:NSMakeRange(1, 1)],
                         [cleanString substringWithRange:NSMakeRange(2, 1)],[cleanString substringWithRange:NSMakeRange(2, 1)]];
      }
      if([cleanString length] == 6) {
          cleanString = [cleanString stringByAppendingString:@"ff"];
      }
    
      unsigned int baseValue;
      [[NSScanner scannerWithString:cleanString] scanHexInt:&baseValue];
    
      float red = ((baseValue >> 24) & 0xFF)/255.0f;
      float green = ((baseValue >> 16) & 0xFF)/255.0f;
      float blue = ((baseValue >> 8) & 0xFF)/255.0f;
      float alpha = ((baseValue >> 0) & 0xFF)/255.0f;
    
      return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
    }
    

    This will handle strings like “#abc”, “#abcdef31”, etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have RGB values that I need to convert into CMYK values. I'm using
Is there any way to convert HEX encoded RGB colors to CMYK in PHP?
I have RGB integer value (ex:00255), i want to convert into this format 0XFF0000FF
I have a hex color, e.g. #F4F8FB (or rgb(244, 248, 251) ) that I
I have found answers to this question that explain how to convert an NSString
Is there a way to match Hex/RGB colors to the closest PMS (Pantone Matching
I have RGB for setting colour of my label it is like this R-:0
I have an int* of rgb data,width, height, and scanlinestride where i would like
I have an array that contains the RGB colour values for each pixel in
I have a need to convert images from CMYK to RGB - not necessarily

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.