-1

Code for my UILabel:

        //Create the title of the tab here
UILabel *title=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 500, 500)];
title.text=@"Useful Websites";
title.textAlignment=UITextAlignmentCenter;
[self.view addSubview:title];

I know that the assigner UITextAlignmentCenter is deprecated from iOS6. Could you please help with the correct assigner/attribute to center a label in Xcode 5 and iOS7? Thanks.

DK19
  • 11
  • 4
  • possible duplicate of [Label Alignment in iOS 6 - UITextAlignment deprecated](http://stackoverflow.com/questions/11920321/label-alignment-in-ios-6-uitextalignment-deprecated) – jbouaziz Mar 17 '14 at 18:10
  • While you are writing in XCode, when you start typing "title.textAlignment", you'll see a hint telling you which type that property accepts. In this case NSTextAlignment. – Odrakir Mar 17 '14 at 18:23

2 Answers2

1

Use:

title.textAlignment = NSTextAlignmentCenter;
alexandresoli
  • 918
  • 1
  • 9
  • 18
1

Switch w/ nstext alignment

title.textAlignment=NSTextAlignmentCenter;
Logan
  • 52,262
  • 20
  • 99
  • 128
  • Do I need to delete the initWithFrame? It seems to mess with the alignment of the text. – DK19 Mar 17 '14 at 18:15
  • That shouldn't be affecting it, is it for iPhone? If so, setting it as you do with 500 px will be beyond the bounds of the screen. If the text is centered on the label, it will appear off on the screen because the label is hanging 180px off the edge. – Logan Mar 17 '14 at 18:46
  • Thanks so much. You're one of the few people who's actually helped me and done it sincerely! This worked perfectly for me. – DK19 Mar 17 '14 at 19:24