我试图让UILabel在UIScrollView内部滚动,但它不会滚动

时间:2012-07-02 23:07:25

标签: ios singleton nsmutablearray

这是我的.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.scrollView addSubview:self.contentView];
    self.scrollView.contentSize = self.contentView.bounds.size;
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];

    NSMutableArray *arr = [[NSMutableArray alloc]init];
    arr = [Singleton getArray];

    NSString *str = [arr componentsJoinedByString:@"\n"];
    summaryLabel.text = str;
}

这是我的.h

@interface TotalViewController : UIViewController
{
    UIScrollView *scrollView;
    UIView *contentView;

}
@property (nonatomic, retain) IBOutlet UIScrollView * scrollView;
@property (nonatomic, retain) IBOutlet UIView       * contentView;
@property (nonatomic,strong) IBOutlet UILabel       * summaryLabel;

My Label已连接到View Controller,我的contentView已连接到View Controller,而我的summaryLabel已连接到View Controller。我需要标签滚动而不是。

2 个答案:

答案 0 :(得分:28)

一个非常简单的答案,如果你只想要一个可滚动的标签,那就是使用UITextView(reference)。禁用编辑,您将获得一个可滚动的标签。

(几乎逐字逐句:how to add a scroll function to a UILabel

答案 1 :(得分:10)

如果UIScrollView的内容不大于可见区域,则不会滚动。考虑到在设置scrollview的contentSize后将文本分配给标签,这种情况不太可能正确发生。我会尝试这样的事情......

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.scrollView addSubview:summaryLabel];

    .....

    summaryLabel.text = str;

    // Assuming your label has numberOfLines = 0, and you want to scroll vertical
    CGSize maxSize = CGSizeMake(summaryLabel.frame.size.width, CGFLOAT_MAX);
    CGSize labelSize = [summaryLabel sizeThatFits:maxSize];
    scrollview.contentSize = labelSize;
}