2012-07-02 112 views
4

這是我的.m我試圖讓一個UILabel到一個UIScrollView內滾動,但它不滾動

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

我的標籤是連接到View Controller,我的contentView連接到View Controller,我的summaryLabel連接到View Controller。我需要標籤滾動,而不是。

回答

28

一個非常簡單的答案,如果你只是想要一個可滾動的標籤,將使用UITextView來代替(reference)。禁用編輯,你會得到一個可滾動的標籤。

(來自幾乎一字不差:how to add a scroll function to a UILabel),如果它的內容比它的可視面積不大於

+0

哇我不能相信這是多麼容易。謝謝。 –

+0

您正在考慮使用UITextView的人們必須意識到這絕對是錯誤。如果你想滾動,以及設置字體大小/家庭,UITextView可能讓你熬夜。 (特別是如果您以編程方式設置文本,而不是故事板)。 另請參閱http://stackoverflow.com/a/19168661/4493512 –

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; 
} 
+2

這是更好的答案。 UITextView總是允許選擇(可以禁用編輯),只有在UserInteraction被禁用時才能禁用選擇。但是,禁用UserInteraction也會禁用TextView的滾動功能。所以,如果你不介意用戶能夠選擇/高亮顯示文本(複製,定義等),那麼使用TextView可能適合你。如果你不想要任何用戶選擇,這是更好的答案。 – emdog4

+1

如果你想要選擇,那麼使用UITextView是「更好的答案」。我會滿足的,這兩者都不是更好,沒有背景。這完全取決於你想要做什麼。 –

相關問題