UILabel 表现得很奇怪

Posted

技术标签:

【中文标题】UILabel 表现得很奇怪【英文标题】:UILabel is acting weird 【发布时间】:2013-03-05 16:34:02 【问题描述】:

我有一个UIPageControl 和一个UIScrollView,您需要滑动它们以更改以UILabel 为标题、UITextView 为内容的视图。

纵向视图中,UILabelUITextView 都很好,除了一个小问题。无论我的UILabeltextAlignmentCenter 还是Right,它始终显示在视图的Left 手角。

现在进入横向视图。这是大部分问题发生的地方。

这就是它的样子,

至于代码,

- (void)viewDidLoad

    [super viewDidLoad];

    pageControl.currentPage = 0;
    pageControl.numberOfPages = 4;

    scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    pageControl.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;


- (void)contentSize

    for (int i = 0; i < 4; i++) 
        CGRect textViewFrame = CGRectMake(scrollView.frame.size.width * i, 30.0, 320.0, scrollView.frame.size.height - 30.0);

        CGRect labelFrame = CGRectMake(scrollView.frame.size.width * i, 0.0, 320.0, 0.0);

        UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame];
        UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];

        switch (i) 
            case 0:
                label.text = @"Test 1";
                textView.text = @"Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.";
                break;

            case 1:
                label.text = @"Test 2";
                textView.text = @"2";
                break;

            case 2:
                label.text = @"Test 3";
                textView.text = @"3";
                break;

            case 3:
                label.text = @"Test 4";
                textView.text = @"4";
                break;

            default:
                break;
        

        textView.editable = NO;
        textView.textColor = [UIColor grayColor];
        textView.textAlignment = NSTextAlignmentRight;
        [textView setFont:[UIFont fontWithName:@"Symbol" size:13]];

        label.textAlignment = NSTextAlignmentCenter;
        [label setFont:[UIFont fontWithName:@"AmericanTypewriter" size:20]];
        [label sizeToFit];

        [scrollView addSubview:label];
        [scrollView addSubview:textView];
    

    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 4, scrollView.frame.size.height);


- (void)viewDidLayoutSubviews

    [self contentSize];


- (void)scrollViewDidScroll:(UIScrollView *)sender

    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.pageControl.currentPage = page;

【问题讨论】:

【参考方案1】:

首先,使用[label sizeToFit]; 会减少标签的宽度,这就是为什么它总是在左边。这就是它应该做的,如果你不想减小它的大小并将标签的宽度保持为页面的宽度,那就不要使用它。

您的滚动页面出现问题是因为当您调整滚动视图的大小时滚动的内容不会改变。你需要做的是,当方向改变时(可以在(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration):

设置新的框架尺寸后,调整滚动视图的内容大小(页面现在具有更小的高度和更大的宽度)。

将子视图的框架更改为新方向(即,为页面分配滚动视图的新边界并重新分配 origin.x)

如果需要,调整每个页面中的内容

滚动到您当前正在观看的页面:

代码(来自我拥有的寻呼机组件,可能需要调整一些内容以供您工作):

-(void) scrollToCurrentPage 
    CGRect frameCurrent =  scrollview.frame;
    frameCurrent.origin.x = _currentPageNumber*frameCurrent.size.width;
    frameCurrent.origin.y = 0;
    //_currentPage is a reference to the page that was being watched before rotation
    _currentPage.frame = frameCurrent; 
    [_container scrollRectToVisible: frameCurrent animated: NO];

【讨论】:

我会用willAnimateRotationToInterfaceOrientation替换我的viewDidLayoutSubviews吗? @Jahm,不一定,我使用 willAnimateRotationToInterfaceOrientation 因为我需要根据方向执行某些动画和逻辑。

以上是关于UILabel 表现得很奇怪的主要内容,如果未能解决你的问题,请参考以下文章

UITableViewCell中两个基本相同的UILabel的奇怪约束差异

如何在UILabel iOS中显示表情符号

UILabel 上的奇怪行为与 StoryBoard 上的 \n

更改文本时 UILabel 中等宽字体的奇怪行为

UICollectionViewCell 中的 UILabel 布局怪异

动画改变 UILabel 的行数