【问题标题】:How to add label inside ScrollView如何在 ScrollView 中添加标签
【发布时间】:2014-02-06 18:07:22
【问题描述】:

我是 Xcode 5 和 Objective C 的新手。我想开发文字处理应用程序。不过,我不知道如何用 Xcode 来做。

如何在ScrollView 内添加标签,从左到右滑动:从左到右预览,从右到下一个单词?

如何在 Xcode 中将 100 个单词添加到 UIlabel

例如,向右滑动选择下一个单词“Apple”,然后再次向右滑动选择下一个单词“Bananas”;向左滑动预览:“Apple”。

【问题讨论】:

  • 请添加您尝试过的代码。
  • 下面是我尝试过的代码。

标签: objective-c uiscrollview label uilabel xcode5


【解决方案1】:

//scrollVC.h

#import <UIKit/UIKit.h>

@interface scrollVC : UIViewController
@property (weak, nonatomic) IBOutlet UIScrollView *mainScroll;

@end

//scrollVC.m

#import "scrollVC.h"

@interface scrollVC ()

@end

@implementation scrollVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.


    [self.mainScroll setContentSize:CGSizeMake(960, 995)];
    [self.mainScroll setPagingEnabled:YES];

    UILabel *testLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(350, 100, 100, 44)];
    [testLabel1 setText:@"testLabel1"];
    [testLabel1 setBackgroundColor:[UIColor redColor]];
    [self.mainScroll addSubview:testLabel1];

    UILabel *testLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(660, 100, 100, 44)];
    [testLabel2 setText:@"testLabel2"];
    [testLabel2 setBackgroundColor:[UIColor redColor]];
    [self.mainScroll addSubview:testLabel2];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

【讨论】:

    【解决方案2】:

    这是 UIScrollView 分页的行为。在这里你可以找到documentation

    如何将 UILabel(通常是 UIView 上的任何实例)添加到 UIScrollView。

    UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.pagingEnabled = YES; // replace default scroll with paging
    for ( int i = 0; i < mWordsCount; i++ )
    {
        UILabel* textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width * i, self.view.frame.size.height)];
        textLabel.text = @"currnet word"; // wordsArray[i]; or [wordsArray objectAtIndex:i]; 
    
        [scrollView addSubview:textLabel];
        // Uncomment if you don't use ARC
        //[textLabel release];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-13
      • 1970-01-01
      • 2019-08-07
      • 2015-05-15
      • 1970-01-01
      相关资源
      最近更新 更多