1

I am using the below code to get text but i dont know how to do text alignment in iOS 6.0..So,Please someone Help me...

        cell.textLabel.font=[UIFont fontWithName:@"Arial" size:16.0];
        cell.textLabel.textColor=[UIColor colorWithRed:15.0/255.0 green:122.0/255.0 blue:202.0/255.0 alpha:1.0];
        cell.textLabel.text=[titleArray objectAtIndex:indexPath.row];

5 Answers 5

8

finally after a lot of research i found the answer of my question...

label5.textAlignment = NSTextAlignmentLeft;

tnx to all who helped me...

5

This is what you need to do in iOS Devices

cell.textLabel.textAlignment = UITextAlignmentCenter;

If your are developing something for MAC, you should use:

cell.textLabel.textAlignment = NSTextAlignmentCenter;

Assign Alignment accordingly.

4
  • 2
    What is the difference between first and second?
    – iDev
    Dec 14, 2012 at 6:51
  • 3
    But question is about IOS 6 and there is No use of UITextAlignment instead of it we use NSTextAlignment ..... !!
    – TheTiger
    Dec 14, 2012 at 7:16
  • This really should not be the highest voted answer as it is just wrong as of iOS5
    – Rambatino
    Dec 28, 2013 at 14:54
  • UITextAlignmentCenter is deprecated as of iOS 6.
    – Beanwah
    Feb 3, 2014 at 23:33
1
 cell.textLabel.textAlignment=NSTextAlignmentLeft;//or whatever you want buddy

let me know it is working or not...

2
  • I tried it several time with all its possible alignment but there is no change in the alignment
    – Kundan
    Dec 14, 2012 at 6:51
  • If your text label is small, you won't notice any change because the text has nowhere to go.
    – borrrden
    Dec 14, 2012 at 7:21
0

try this man...let see working or not...

write below statement in your common file (for example constant.h)

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

//then to check textalignment for ios 6 or ios 5 write below code .m file where you want to use

if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0"))
{
 cell.textLabel.textAlignment=NSTextAlignmentLeft;
}
else
{
   cell.textLabel.textAlignment = UITextAlignmentCenter;
}

try this ...

Happy Coding!!!!!

7
  • it showing error in this line..if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0"))
    – Kundan
    Dec 14, 2012 at 6:59
  • Error is Expected identifier or (
    – Kundan
    Dec 14, 2012 at 7:01
  • Yes..i copied it exactly..and paste it in list.h,u have written put it in common file..which is common file ..please let me know i am new to iOS...
    – Kundan
    Dec 14, 2012 at 7:06
  • have you imported list.h file in a .m file where you have written that code?
    – NiravPatel
    Dec 14, 2012 at 7:09
  • which simulator you are using iphone 6.0 or iphone 5.0?
    – NiravPatel
    Dec 14, 2012 at 7:12
0

You can use the following code:

cell.textLabel.textAlignment = NSTextAlignmentCenter;

Hope this helps.

Please try with another label and add it to the cell:

Write the following code on top of the class:

#ifdef __IPHONE_6_0
# define ALIGN_CENTER NSTextAlignmentCenter
#else
# define ALIGN_CENTER UITextAlignmentCenter
#endif

Then add a label:

UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
label.text = @"This is a sample text";
label.textAlignment = ALIGN_CENTER;
[cell.contentView addSubview:label];
[label release];

Is it works?

3
  • can you please see this link: stackoverflow.com/questions/11920321/… please read this thread + what you are getting now ? any error or no changes regarding the code ? + are you getting right output for label color and the font also ?
    – sumon
    Dec 14, 2012 at 6:54
  • are you testing in simulator? Is it iPhone 6.0 simulator ? It should work and the issue is also simple. Please don't be frustrated! I think you can solve your problem, can you please recheck the code and ensure that there is no duplicate alignment code + which alignment are you expecting ? Left, right or center?
    – sumon
    Dec 14, 2012 at 7:06
  • I have edited the answer, can you please check with another label and check that is it working or not. thanks.
    – sumon
    Dec 14, 2012 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.