我的文字右对齐有问题。实际上在我的DetailedViewController中有一个UILabel称为"lblnNewsTitle",为此Lable我正在尝试设置NSTextAlignmentRight,但对我不起作用。谁能帮助我解决这个问题?

屏幕截图1:

ios - UILabel NSTextAlignmentRight不起作用吗?-LMLPHP

屏幕截图2:

ios - UILabel NSTextAlignmentRight不起作用吗?-LMLPHP

我的故事板截图3:

ios - UILabel NSTextAlignmentRight不起作用吗?-LMLPHP

这是我的代码:

   - (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];

            }

    }

最佳答案

NSTextAlignmentRight直接添加到您的Attribute Inspector中,同时删除[lblnNewsTitle sizeToFit];并检查

尝试这个

ios - UILabel NSTextAlignmentRight不起作用吗?-LMLPHP

09-08 05:18