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 5840485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:45:10+00:00 2026-05-22T11:45:10+00:00

hey, I want to be able to have a gradient fill on the text

  • 0

hey, I want to be able to have a gradient fill on the text in a UILabel I know about CGGradient but i dont know how i would use it on a UILabel’s text

i found this on google but i cant manage to get it to work

http://silverity.livejournal.com/26436.html

  • 1 1 Answer
  • 1 View
  • 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-22T11:45:12+00:00Added an answer on May 22, 2026 at 11:45 am

    (Skip to bottom for full class source code)

    Really useful answers by both Brad Larson and Bach. The second worked for me but it requires an image to be present in advance. I wanted something more dynamic so I combined both solutions into one:

    • draw the desired gradient on a UIImage
    • use the UIImage to set the color pattern

    The result works and in the screenshot below you can see some Greek characters rendered fine too. (I have also added a stroke and a shadow on top of the gradient)

    iOS stylized UILabel, the big brown fox

    Here’s the custom init method of my label along with the method that renders a gradient on a UIImage (part of the code for that functionality I got from a blog post I can not find now to reference it):

    - (id)initWithFrame:(CGRect)frame text:(NSString *)aText {
        self = [super initWithFrame:frame];
        if (self) {
            self.backgroundColor = [UIColor clearColor];
            self.text = aText;
    
            self.textColor = [UIColor colorWithPatternImage:[self gradientImage]];
    
        }
        return self;
    }
    
    - (UIImage *)gradientImage
    {
        CGSize textSize = [self.text sizeWithFont:self.font];
        CGFloat width = textSize.width;         // max 1024 due to Core Graphics limitations
        CGFloat height = textSize.height;       // max 1024 due to Core Graphics limitations
    
        // create a new bitmap image context
        UIGraphicsBeginImageContext(CGSizeMake(width, height));
    
        // get context
        CGContextRef context = UIGraphicsGetCurrentContext();       
    
        // push context to make it current (need to do this manually because we are not drawing in a UIView)
        UIGraphicsPushContext(context);                             
    
        //draw gradient    
        CGGradientRef glossGradient;
        CGColorSpaceRef rgbColorspace;
        size_t num_locations = 2;
        CGFloat locations[2] = { 0.0, 1.0 };
        CGFloat components[8] = { 0.0, 1.0, 1.0, 1.0,  // Start color
                                1.0, 1.0, 0.0, 1.0 }; // End color
        rgbColorspace = CGColorSpaceCreateDeviceRGB();
        glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);
        CGPoint topCenter = CGPointMake(0, 0);
        CGPoint bottomCenter = CGPointMake(0, textSize.height);
        CGContextDrawLinearGradient(context, glossGradient, topCenter, bottomCenter, 0);
    
        CGGradientRelease(glossGradient);
        CGColorSpaceRelease(rgbColorspace); 
    
        // pop context 
        UIGraphicsPopContext();                             
    
        // get a UIImage from the image context
        UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
    
        // clean up drawing environment
        UIGraphicsEndImageContext();
    
        return  gradientImage;
    }
    

    I’ll try to complete that UILabel subclass and post it.

    EDIT:

    The class is done and it’s on my GitHub repository. Read about it here!

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

Sidebar

Related Questions

Hey i dont know if that is possible but i want to set a
Hey guys I have setup Nivo slider to my specification, but what I want
Hey all, I have a winforms virtualized Listview that i want to be able
Hey StackOverflow friends, I have a situation where I would like to be able
hey guys, I have mypage.com/user-login.php?action=register and I want to be able to call mypage.com/register
hey i want to be able to create multiple shapes and store them perhaps
hey i want to display a loading progress using ProgressDialog on my aplication, but
Hey, I want to store application configuration data. Currently, I'm using INIs but they
hey, hi i want put limit on object creation means a class can have
Hey I have this kinda structure in my ini file I want to be

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.