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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:09:02+00:00 2026-05-25T19:09:02+00:00

I used [button setAlignment:NSCenterTextAlignment]; to make the text display at the center of the

  • 0

I used

[button setAlignment:NSCenterTextAlignment];

to make the text display at the center of the button.

It worked.

But if I set button title attribute before the code, button ‘setAlignment’ will not work

- (void)setButtonTitle:(NSButton*)button fontName:(NSString*)fontName fontSize:(CGFloat)fontSize fontColor:(NSColor*)fontColor;
{

    NSMutableAttributedString *attributedString =
    [[NSMutableAttributedString alloc] initWithString:[button title]
                                     attributes:[NSDictionary dictionaryWithObject:[NSFont fontWithName:fontName size:fontSize]
                                                                            forKey:NSFontAttributeName]];
    [attributedString addAttribute:NSForegroundColorAttributeName 
                              value:fontColor 
                              range:NSMakeRange(0, [[button title] length] )];


    [button setAttributedTitle: attributedString];
    [button setAlignment:NSCenterTextAlignment];//button title alignment always displayed as 'NSLeftTextAlignment' rather than 'NSCenterTextAlignment'.

}

title alignment always displayed as ‘NSLeftTextAlignment’ rather than ‘NSCenterTextAlignment’.

Welcome any comment

  • 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-25T19:09:03+00:00Added an answer on May 25, 2026 at 7:09 pm

    Since you’re using an attributed string for the button title, the attributes in that string are responsible for setting the alignment.

    To centre that attributed string, add an NSParagraphStyleAttributeName attribute with a centred alignment value:

    NSMutableParagraphStyle *centredStyle = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
    [centredStyle setAlignment:NSCenterTextAlignment];
    
    NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:centredStyle,
                           NSParagraphStyleAttributeName,
                           [NSFont fontWithName:fontName size:fontSize],
                           NSFontAttributeName,
                           fontColor,
                           NSForegroundColorAttributeName,
                           nil];
    NSMutableAttributedString *attributedString =
    [[NSMutableAttributedString alloc] initWithString:[button title]
                                     attributes:attrs];
    
    [button setAttributedTitle: attributedString];
    

    In the code above, I’ve created a single attrs dictionary holding all the attributes for the attributed string. From your code, it looks like the font colour should be applied to the whole string anyway.

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

Sidebar

Related Questions

I used the following code to post a search value via an html button.
The following code is being used to disable a Submit button once it has
I'm trying to use $(button).button(); but jquery-ui-1.8.16.custom.min.js used by SlickGrid doesn't have the button
I need to get the class attribute of checked radio button, with name=radio. Used
I have used the twitter button resources to generate code for a follow button,
i have my script working but currently it used a button input, i want
I have a jCombobox and i want to move the button used to display
Recently i worked in a project where i used 5 radio button to select
I added this bit of code for the input button used by.net for hover,
In Application i have two buttons in which one button is used as a

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.