【问题标题】:iOS 7 - Dynamically resize the height of the label and scrollviewiOS 7 - 动态调整标签和滚动视图的高度
【发布时间】:2014-03-01 11:30:22
【问题描述】:

Sooo...我在 iOS7/xcode5 上使用滚动视图和适合文本标签时遇到问题,我对这个问题感到沮丧,因为在 iOS6/xcode4 上很容易处理这个问题...

这是我的代码预览...

ViewController.h

#import <UIKit/UIKit.h>
#import "ViewControllerOther.h"

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITableView *tableview;

@property (strong, nonatomic) NSArray *meepo;
@property (strong, nonatomic) NSArray *icon;

@property (strong, nonatomic) NSArray *taunt;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.tableview setDataSource:self];
    [self.tableview setDelegate:self];

    self.meepo = [[NSArray alloc] initWithObjects:
                    @"Meepo No. 1",
                    @"Meepo No. 2",
                    @"Meepo No. 3",
                    @"Meepo No. 4",
                    @"Meepo No. 5",
                    nil];
    self.icon = [[NSArray alloc] initWithObjects:
                   [UIImage imageNamed:@"meepo.jpg"],
                   [UIImage imageNamed:@"meepo.jpg"],
                   [UIImage imageNamed:@"meepo.jpg"],
                   [UIImage imageNamed:@"meepo.jpg"],
                   [UIImage imageNamed:@"meepo.jpg"],
                   nil];

    self.taunt = [[NSArray alloc] initWithObjects:
                 @"Meepo No. 1 reporting, Meepo No. 1 reporting, Meepo No. 1 reporting, Meepo No. 1 reporting, Meepo No. 1 reporting, Meepo No. 1 reporting, Meepo No. 1 reporting, Meepo No. 1 reporting, Meepo No. 1 reporting, Meepo No. 1 reporting, Meepo No. 1 reporting, Meepo No. 1 reporting, Meepo No. 1 reporting, ",
                 @"Meepo No. 2 reporting, Meepo No. 2 reporting, Meepo No. 2 reporting, Meepo No. 2 reporting, Meepo No. 2 reporting, Meepo No. 2 reporting, Meepo No. 2 reporting, Meepo No. 2 reporting, Meepo No. 2 reporting, Meepo No. 2 reporting, Meepo No. 2 reporting, Meepo No. 2 reporting, ",
                 @"Meepo No. 3 reporting, Meepo No. 3 reporting, Meepo No. 3 reporting, Meepo No. 3 reporting, Meepo No. 3 reporting, Meepo No. 3 reporting, Meepo No. 3 reporting, Meepo No. 3 reporting, Meepo No. 3 reporting, Meepo No. 3 reporting, Meepo No. 3 reporting, ",
                 @"Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, ",
                 @"Meepo No. 5 reporting, Meepo No. 5 reporting, Meepo No. 5 reporting, Meepo No. 5 reporting, Meepo No. 5 reporting, Meepo No. 5 reporting, Meepo No. 5 reporting, Meepo No. 5 reporting, Meepo No. 5 reporting, Meepo No. 5 reporting, Meepo No. 5 reporting, ",
                 nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.meepo count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }

    NSString *meepos = [self.meepo objectAtIndex:indexPath.row];
    UIImage *icon = [self.icon objectAtIndex:indexPath.row];
    [cell.textLabel setText:meepos];
    cell.imageView.image = icon;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ViewControllerOther *other = [self.storyboard instantiateViewControllerWithIdentifier:@"Delegate"];
    other.meepono = [self.meepo objectAtIndex:indexPath.row];
    other.index = [self.taunt objectAtIndex:indexPath.row];

    [self.navigationController pushViewController:other animated:YES];
}


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

@end

ViewControllerOther.h

#import <UIKit/UIKit.h>

@interface ViewControllerOther : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *meepo;
@property (weak, nonatomic) IBOutlet UILabel *taunt;
@property (weak, nonatomic) IBOutlet UIScrollView *scroller;

@property (nonatomic, strong) NSString *meepono;
@property (nonatomic, strong) NSString *index;

@end

ViewControllerOther.m (*)

#import "ViewControllerOther.h"

@interface ViewControllerOther ()

@end

static CGRect size;
static CGSize maxSize;
static CGRect labelRect;

@implementation ViewControllerOther

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.meepo setText:self.meepono];

    [self.scroller setScrollEnabled:YES];



    labelRect = [self.taunt frame];
}

- (void)viewWillAppear:(BOOL)animated
{

}

-(void)viewWillLayoutSubviews
{
    [self.taunt setNumberOfLines:0];
    [self.taunt setText:self.index];
    maxSize = CGSizeMake(self.taunt.frame.size.width, MAXFLOAT);
    size = [self.taunt.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.taunt.font} context:nil];

    labelRect.size.height = size.size.height;

    [self.taunt setFrame:labelRect];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)sample:(id)sender {
    [self.scroller setContentSize:CGSizeMake(320, 1000)];
}
@end

这是输出

第一个 - http://i.imagefra.me/b52js8zk

第二个 - http://i.imagefra.me/313js8zl

我在第二个视图上的目标是根据文本的长度动态调整标签的高度,同时调整滚动视图的高度并使其可滚动......

注意: 这只是我制作的一个示例项目,因此我可以提出这类问题。

更新 1:

好的,所以我设法通过将代码从 viewDidLoad 方法转移到 viewWillLayoutSubviews 方法来使调整大小标签起作用,但不幸的是,当我把[self.scroller setContentSize:CGSizeMake(320, 1000)]; 放回正常大小时。我错过了什么吗?

【问题讨论】:

    标签: ios objective-c resize label scrollview


    【解决方案1】:

    要使 UILabel 适合其内容,请使用 sizeToFit

    1) 将行数设置为零 yourLabel.numberOfLines = 0; // 重要的事情

    2) 设置文本后调用sizeToFit [YourLabel sizeToFit];

    3) 这将根据标签的内容动态调整标签的高度。

    UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0,320,100)];
    testLabel.numberOfLines = 0;
    testLabel.text = @"add your string here";
    [testLabel sizeToFit];
    

    现在可以使用label的高度来动态设置scrollview的ContentSize了。

    [self.scroller setContentSize:CGSizeMake(self.scroller.frame.size.width,testLabel.frame.size.height)];
    

    希望对你有所帮助..

    【讨论】:

    • 当我用代码分配初始化 UILabel 时它工作正常。但如果我使用 xib 文件,它就不起作用。 xib中的UILabel怎么办?
    • 制作一个 IBOutlet.. 将它与 xib 文件中的对象连接.. 然后将 numberOfLines 和 sizeToFit 设置为它。
    • 非常感谢。我发现这是因为我将这些代码放在 viewdidload 中,而应该放在 viewDidLayoutSubviews 中。
    • 问题已更新...将代码从 viewDidLoad 转移到 viewDidLayoutSubviews 确实有效...我现在的问题是,当我输入代码[self.scroller setContentSize:CGSizeMake(320, 1000)]; 时,标签大小恢复正常...跨度>
    • 当您的视图旋转时这不起作用,更好的答案在这里,查看接受的答案:stackoverflow.com/questions/15927086/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多