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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:21:06+00:00 2026-05-22T14:21:06+00:00

I find myself creating essentially the same UILabel repeatedly in my app. So I’m

  • 0

I find myself creating essentially the same UILabel repeatedly in my app. So I’m thinking this is a good time to create a class for it and just call the class?

For example, I reuse this code many times to create a UILabel for my UINavigationBar in views, with the only difference being the label.text that I’m thinking I can pass as a parameter:

// - - - - - Add a Navigation Bar

UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
navBar.barStyle = UIBarStyleBlack; 
navBar.translucent = YES;

[self.view addSubview:navBar];
[navBar release];

// - - - - - Add a label to the navbar

UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,8,280,30)];
navLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
navLabel.text = @"Some Label String";
navLabel.backgroundColor = [UIColor clearColor];
navLabel.textColor = [UIColor whiteColor];
navLabel.font = [UIFont boldSystemFontOfSize:20];
navLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
navLabel.textAlignment = UITextAlignmentCenter;
[navBar addSubview:navLabel];
[navLabel release];

So I’m thinking I should be able to create a UILabelNavBar Class and simply create the UILabel by calling it like this:

navBar addSubview:[UILabelNavBar createLabel:@"Some Label String"]];

Except I don’t know how to create the class.
Any help is appreciated.
lq

  • 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-22T14:21:07+00:00Added an answer on May 22, 2026 at 2:21 pm

    Thanks to the nudge from Adubr, the following works:

    // *** OftenUsedLabel.h ***
    
    #import <UIKit/UIKit.h>
    
    @interface OftenUsedLabel : UILabel 
    {
        UILabel *oftenUsedLabel;
    }
    
    @property (nonatomic, retain) UILabel *oftenUsedLabel;
    
    @end
    
    // *** OftenUsedLabel.m ***
    
    #import "OftenUsedLabel.h"
    
    @implementation OftenUsedLabel
    
    @synthesize oftenUsedLabel;
    
    - (id)initWithFrame:(CGRect)frame {
    
        self = [super initWithFrame:frame];
    
        if (self) {
    
            CGRect rect;
            rect = CGRectMake(0.0,0.0,280.0,30.0);
    
            oftenUsedLabel = [[UILabel alloc] initWithFrame:rect];
            oftenUsedLabel.backgroundColor = [UIColor clearColor];
            oftenUsedLabel.textColor = [UIColor whiteColor];
            oftenUsedLabel.font = [UIFont boldSystemFontOfSize:20];
            oftenUsedLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
            oftenUsedLabel.textAlignment = UITextAlignmentCenter;
    
            [self addSubview:oftenUsedLabel];
        }
    
        return self;    
    }
    
    - (void)dealloc {
        [oftenUsedLabel release];
        [super dealloc];
    }
    
    
    // *** SomeViewController.m ***
    
    #import "OftenUsedLabel.h"
    
    // CALL THE LABEL LIKE THIS:
    
        CGRect rect;
        rect = CGRectMake(0.0, 0.0, 320.0, 60.0);
        UILabel *myLabel = [[[OftenUsedLabel alloc] initWithFrame:rect] autorelease];
        myLabel.text = @"Some Text";
        [self.view addSubview:myLabel];
        [myLabel release];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I frequently find myself creating classes which use this form (A): abstract class Animal
Time and again I find myself creating a database utility class which has multiple
This is a very simple question. I often find myself wanting to create a
Often do I find myself creating the same unit tests methods to getters\setters, c'tors
Sometimes I find myself creating a CSS class for a single element. So instead,
I find myself creating Converters often and would like to be able to: right-click
While creating classes in Java I often find myself creating instance-level collections that I
I am working in C#. I find myself creating dialogs for editing my application
I find myself writing code that looks like this a lot: set<int> affected_items; while
I find myself having a lot of this in different methods in my code:

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.