iOS cell自动换行


 

//

//  DynamicHeightsViewController.h

//  DynamicHeights

//

//  Created by Matt Long on 9/22/09.

//  Copyright Skye Road Systems, Inc. 2009. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface DynamicHeightsViewController : UIViewController {

    IBOutlet UITableView *dataTableView;

    

    NSMutableArray *items;

}


@end


 


//

//  DynamicHeightsViewController.m

//  DynamicHeights

//

//  Created by Matt Long on 9/22/09.

//  Copyright Skye Road Systems, Inc. 2009. All rights reserved.

//


#import "DynamicHeightsViewController.h"


#define FONT_SIZE 14.0f

#define CELL_CONTENT_WIDTH 320.0f

#define CELL_CONTENT_MARGIN 10.0f


@implementation DynamicHeightsViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    

    items = [[NSMutableArray alloc] init];

    [itemsaddObject:@"After two years in Washington, I often long for the realism and sincerity of Hollywood.\n\n\t\t-Fred Thompson, Speech before the Commonwealth Club of Californiaaaagdd阿黑哥是否规范的阿黑哥是否规范的阿黑哥是否规范的阿黑哥是否规范的阿黑哥是否规范的阿黑哥是否规范的阿黑哥是否ll"];

    [itemsaddObject:@"It is a profitable thing, if one is wise, to seem foolish.\n\n\t\t-Aeschylus (525 BC - 456 BC)"];

    [itemsaddObject:@"Bill Gates is a very rich man today... and do you want to know why? The answer is one word: versions.\n\n\t\t-Dave Barry"];

    [itemsaddObject:@"At the worst, a house unkept cannot be so distressing as a life unlived.\n\n\t\t-Dame Rose Macaulay (1881 - 1958)"];

    [itemsaddObject:@"It is curious that physical courage should be so common in the world and moral courage so rare.\n\n\t\t-Mark Twain (1835 - 1910)"];

    [itemsaddObject:@"The knowledge of the world is only to be acquired in the world, and not in a closet.\n\n\t\t-Lord Chesterfield (1694 - 1773), Letters to His Son, 1746, published 1774"];

    [itemsaddObject:@"What lies behind us and what lies before us are tiny matters compared to what lies within us.\n\n\t\t-Ralph Waldo Emerson (1803 - 1882), (attributed)"];

     

}


- (void)dealloc {

    [items release], items = nil;

    [super dealloc];

}


#pragma mark -

#pragma mark UITableView Delegaates


- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section

{

return [items count];

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

{

return1;

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

{

    NSString *text = [items objectAtIndex:[indexPath row]];

    

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

    

    CGSize size = [text sizeWithFont:[UIFontsystemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

    

    CGFloat height = MAX(size.height, 44.0f);

    

    return height + (CELL_CONTENT_MARGIN * 2);

}


- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    UITableViewCell *cell;

    UILabel *label = nil;

    

    cell = [tv dequeueReusableCellWithIdentifier:@"Cell"];

    if (cell == nil)

    {

        cell = [[[UITableViewCellalloc] initWithFrame:CGRectZeroreuseIdentifier:@"Cell"] autorelease];

        

        label = [[UILabel alloc] initWithFrame:CGRectZero];

        [label setLineBreakMode:UILineBreakModeWordWrap];

        [label setMinimumFontSize:FONT_SIZE];

        [label setNumberOfLines:0];

        [label setFont:[UIFontsystemFontOfSize:FONT_SIZE]];

        [label setTag:1];

        

        [[label layer] setBorderWidth:2.0f];

        

        [[cell contentView] addSubview:label];

        

    }

    NSString *text = [items objectAtIndex:[indexPath row]];

    

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

    

    CGSize size = [text sizeWithFont:[UIFontsystemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

    

    if (!label)

        label = (UILabel*)[cell viewWithTag:1];

    

    [label setText:text];

    [label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];

    

    return cell;


}

 

转载于:https://www.cnblogs.com/james1207/p/3341743.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值