user3744932
user3744932

Reputation: 89

UILabel NSTextAlignmentRight not working?

I have problem with text right alignment . Actually in my DetailedViewController there is UILabel Called "lblnNewsTitle", For this Lable i am trying to set NSTextAlignmentRight but it doesn't work for me . can any one help me to solve this issue ?

Screenshot 1:

enter image description here

Screenshot 2 :

enter image description here

My Storyboard screenshot 3 :

enter image description here

Here is My Code :

   - (void)viewDidLoad

    {

        [DetailTittle setFont:[UIFont fontWithName:@"GEEast-ExtraBold" size:12]];
        [super viewDidLoad];

        [self.lblDescription setNumberOfLines:0];

        [scrollView setScrollEnabled:YES];
        [scrollView setContentSize:CGSizeMake(320, 765)];
        self.txtViwNews.text=self.strDescription;
        self.lblnNewsTitle.text=self.strTitle;
        self.lblnNewsTitle.font=[UIFont fontWithName:@"GEEast-ExtraBold" size:14];
        self.txtViwNews.font=[UIFont fontWithName:@"GE SS Unique" size:12];

        self.lblnNewsTitle.textAlignment = NSTextAlignmentRight;

        self.txtViwNews.textAlignment= NSTextAlignmentRight;

        self.lblDateinDetail.text=self.strDate;
        self.lblDateinDetail.font=[UIFont fontWithName:@"GEEast-ExtraBold" size:14];
        self.lblDateinDetail.textColor=[UIColor blackColor];

        [lblnNewsTitle setNumberOfLines:0];
        [lblnNewsTitle sizeToFit];


        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.strDetailImage]];
        self.strImage.image = [UIImage imageWithData:data];
        // Set border color and width of image
        [_strImage.layer setBorderColor:[UIColor blackColor].CGColor];
        [_strImage.layer setBorderWidth:1.0];
        self.lblDescription.font=[UIFont fontWithName:@"GE SS Unique" size:kLblIntalFont];
        self.lblDescription.textAlignment = NSTextAlignmentRight;
        [self estimatedHeight:kLblIntalFont];

        if (_strImage.image == nil)
        {
            [_strImage.layer setBorderColor:[UIColor blackColor].CGColor];
            [_strImage.layer setBorderWidth:0];

            [self.lblnNewsTitle setNumberOfLines:0];
            self.lblnNewsTitle.frame = CGRectMake(self.lblnNewsTitle.frame.origin.x, self.lblnNewsTitle.frame.origin.y, self.lblnNewsTitle.frame.size.width, self.lblnNewsTitle.frame.size.height );

self.viwMidImage.frame=CGRectMake(self.viwMidImage.frame.origin.x, self.strImage.frame.origin.y, self.viwMidImage.frame.size.width, self.viwMidImage.frame.size.height);
            [self.lblDescription setNumberOfLines:0];
            [self.lblDescription sizeToFit];
            self.lblDescription.frame = CGRectMake(self.lblDescription.frame.origin.x, self.viwMidImage.frame.origin.y+  self.viwMidImage.frame.size.height,self.lblDescription.frame.size.width,self.lblDescription.frame.size.height );
            self.lblDescription.textAlignment = NSTextAlignmentRight;
               }
        else {

            //  if (!cell.imgNews ==nil)
            self.lblnNewsTitle.frame = CGRectMake(self.lblnNewsTitle.frame.origin.x, self.lblnNewsTitle.frame.origin.y, self.lblnNewsTitle.frame.size.width, self.lblnNewsTitle.frame.size.height );
            [self.lblDescription setNumberOfLines:0];
            [self.lblDescription sizeToFit];

            }

    }

Upvotes: 0

Views: 509

Answers (2)

Teja Nandamuri
Teja Nandamuri

Reputation: 11201

From the Documentation:

- sizeToFit

Resizes and moves the receiver’s content view so it just encloses its subviews.

You should invoke this method after:

  • Adding a subview (to the content view)

  • Altering the size or location of such a subview

  • Setting the margins around the content view

So, if you do sizeTofit under any alignment, the text will aligned to the left only. To fix this:

You can save the original label's width in a variable and set it after sizeToFit, or give it a fixed width to counter these problems:

If you still wish to use sizeToFit,Do something like this:

myLabel.textAlignment = NSTextAlignmentRight;

    [myLabel setNumberOfLines:0];
    [myLabel sizeToFit];

    CGRect myFrame = myLabel.frame;
    // Resize the frame's width to your requirement)
    // width could also be myOriginalLabelFrame.size.width
    myFrame = CGRectMake(myFrame.origin.x, myFrame.origin.y, myWidth, myFrame.size.height);
    myLabel.frame = myFrame;

you can find more useful info here

Upvotes: 0

Anbu.Karthik
Anbu.Karthik

Reputation: 82766

add NSTextAlignmentRight in directly in your Attribute Inspector at the same time remove [lblnNewsTitle sizeToFit]; and check

try this

enter image description here

Upvotes: 1

Related Questions