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

  • Home
  • SEARCH
  • 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 8238253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:44:52+00:00 2026-06-07T19:44:52+00:00

So first, I needed to set the placeholder text color, so I subclassed the

  • 0

So first, I needed to set the placeholder text color, so I subclassed the UITextfield as shown in several stack overflow posts. This works fine. Here is an example post of this: How do I subclass UITextField and override drawPlaceholderInRect to change Placeholder color.

But, when I try to use searchNameField.textAlignment = UITextAlignmentCenter; It is no longer centering the placeholder. The input text is still centered fine.

So then, assuming that the centering is not working because of the subclassing, what do I have to add to my UITextField subclass in order to have the placeholder text centered?

Thanks

Edit

Here is the code I used to solve this problem. This is placed inside my subclass of UITextField.

- (CGRect)placeholderRectForBounds:(CGRect)bounds 
{
    CGSize size = [[self placeholder] sizeWithFont:[UIFont fontWithName:@"Arial" size:placeholdTextSize]];

    return CGRectMake( (bounds.size.width - size.width)/2 , bounds.origin.y , bounds.size.width , bounds.size.height);
}

Note that placeholdTextSize is a property that I set during initialization.

  • 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-06-07T19:44:54+00:00Added an answer on June 7, 2026 at 7:44 pm

    Here you go buddy

    subclassing UITextField will do the work:

    // CustomTextField.h
    @interface CustomTextField : UITextField {
    }
    @end
    

    override the methods:

    @implementation
    - (CGRect)placeholderRectForBounds:(CGRect)bounds {
        return CGRectMake(x,y,width,height);//Return your desired x,y position and width,height
    }
    
    - (void)drawPlaceholderInRect:(CGRect)rect {
        //draw place holder.
     [[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:12]];
    
    }
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I set up my code without considering this fact needed to be detected, and
In the code below is the initial first loop needed when creating a 2D
First steps in FreeBSD: trying to run my installation script. Fast help needed: #
Remove first comma on each line in a file. I presume sed is needed.
When I first started with Javascript, I usually just put whatever I needed into
I'm developing my first java application using Eclipse. I've recently needed to adjust the
I have a set of input text boxes that activate a calendar for selecting
First I want to thank you guys here, because your posts have gotten me
I'm writing a python program that needs to set the environment CFLAGS as needed.
I'm trying to set up a web application for the first time on Amazon

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.