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

Joh*_*Cox 5 singleton nsmutablearray ios

这是在我的.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;
}
Run Code Online (Sandbox Code Playgroud)

这是我的.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;
Run Code Online (Sandbox Code Playgroud)

我的标签连接到视图控制器,我的contentView连接到视图控制器,我的summaryLabel连接到视图控制器.我需要标签滚动而不是.

Daw*_*oth 28

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

(几乎逐字逐句:如何向UILabel添加滚动功能)


Jer*_*nes 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;
}
Run Code Online (Sandbox Code Playgroud)

  • 这是更好的答案.UITextView始终允许选择(可以禁用编辑),只有禁用UserInteraction时才能禁用选择.但是,禁用UserInteraction还会禁用TextView的滚动功能.因此,如果您不介意用户能够选择/突出显示文本(复制,定义等),那么使用TextView可能对您有用.如果您不想要任何用户选择,这是更好的答案. (2认同)