【问题标题】:UILabel NSTextAlignmentRight not working?UILabel NSTextAlignmentRight 不起作用?
【发布时间】:2015-10-27 14:29:42
【问题描述】:

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

屏幕截图 1:

屏幕截图 2:

我的故事板截图 3:

这是我的代码:

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

            }

    }

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

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

    试试这个

    【讨论】:

    • @user3744932 - 祝你有美好的一天,我的兄弟
    【解决方案2】:

    来自文档:

    - sizeToFit

    调整并移动接收者的内容视图,使其仅包含其 子视图。

    您应该在以下情况下调用此方法:

    • 添加子视图(到内容视图)

    • 更改此类子视图的大小或位置

    • 设置内容视图的边距

    因此,如果您在任何对齐方式下执行 sizeTofit,则文本将仅向左对齐。要解决这个问题:

    您可以将原始标签的宽度保存在一个变量中并在 sizeToFit 之后设置它,或者给它一个固定宽度来解决这些问题:

    如果您仍想使用 sizeToFit,请执行以下操作:

    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;
    

    你可以找到更多有用的信息here

    【讨论】:

      猜你喜欢
      • 2013-11-06
      • 1970-01-01
      • 2015-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 1970-01-01
      相关资源
      最近更新 更多