微博计算frame

M

//

//  Weibo.h

//  微博

//

//  Created by Tony on 15/9/28.

//  Copyright (c) 2015 Tony. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface Weibo : NSObject


@property(nonatomic,strong)NSString *text;


@property(nonatomic,strong)NSString *ico;


@property(nonatomic,strong)NSString *picture;


@property(nonatomic,strong)NSString *name;


@property(nonatomic,assign, getter=isVip)BOOL vip;


-(instancetype)initWithDict:(NSDictionary*)dict;

+(instancetype)weibiWithDict:(NSDictionary*)dict;

@end



//

//  Weibo.m

//  微博

//

//  Created by Tony on 15/9/28.

//  Copyright (c) 2015 Tony. All rights reserved.

//


#import "Weibo.h"


@implementation Weibo


-(instancetype)initWithDict:(NSDictionary*)dict

{

    if (self=[super init]) {

        [self setValuesForKeysWithDictionary:dict];

    }

    return self;

}

+(instancetype)weibiWithDict:(NSDictionary*)dict

{

    return [[self alloc] initWithDict:dict];

}


@end




//

//  WeiboFrame.h

//  微博

//

//  Created by Tony on 15/9/28.

//  Copyright (c) 2015 Tony. All rights reserved.

//


#import <Foundation/Foundation.h>

#import <CoreGraphics/CoreGraphics.h>

#import "Weibo.h"


@interface WeiboFrame : NSObject


@property(nonatomic,strong)Weibo *weibo;

//头像的frame

@property(nonatomic,assign)CGRect iconFrame;

//昵称

@property(nonatomic,assign)CGRect nameFrame;

//vip

@property(nonatomic,assign)CGRect vipFrame;

//正文

@property(nonatomic,assign)CGRect textFrame;

//配图

@property(nonatomic,assign)CGRect picFrame;

//行高

@property(nonatomic,assign)CGFloat rowHeight;

@end



//

//  WeiboFrame.m

//  微博

//

//  Created by Tony on 15/9/28.

//  Copyright (c) 2015 Tony. All rights reserved.

//


#import "WeiboFrame.h"

#import <UIKit/UIKit.h>


#define fontSize [UIFont systemFontOfSize:12]

#define textSize [UIFont systemFontOfSize:14]


@implementation WeiboFrame

- (void)setWeibo:(Weibo *)weibo

{

    _weibo=weibo;

    

    //计算每个控件的frame 和行高

    //提取统一的间距

    CGFloat margin=10;

    CGFloat iconW=35;

    CGFloat iconH=35;

    CGFloat iconX=margin;

    CGFloat iconY=margin;

    

    //1头像

    _iconFrame=CGRectMake(iconX,iconY,iconW , iconH);

    

    //2昵称

    //获取昵称字符串

    NSString *nickName=weibo.name;

    //取到左边的边距

    CGFloat nameX=CGRectGetMaxX(_iconFrame)+margin;

    //根据label中的文字的内容。来动态计算LABEL的高和宽

    CGSize nameSize=[self sizeWithText:nickName MaxSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) font:fontSize];

    

    CGFloat nameW=nameSize.width;

    CGFloat nameH=nameSize.height;

    CGFloat nameY=iconY+(iconH-nameH)/2;

    _nameFrame=CGRectMake(nameX, nameY, nameW, nameH);

    

    //3会员

    CGFloat vipW=10;

    CGFloat vipH=10;

    CGFloat vipX=CGRectGetMaxX(_nameFrame)+margin;

    CGFloat vipY=nameY;

    _vipFrame=CGRectMake(vipX, vipY, vipW, vipH);

    

    //4正文

    CGFloat textX=iconX;

    CGFloat textY=CGRectGetMaxY(_iconFrame)+margin;

    CGSize textSize1=[self sizeWithText:weibo.text MaxSize:CGSizeMake(320, CGFLOAT_MAX) font:textSize];


    _textFrame=CGRectMake(textX, textY, textSize1.width, textSize1.height);

    

    //5配图

    CGFloat picW=100;

    CGFloat picH=100;

    CGFloat picX=iconX;

    CGFloat picY=CGRectGetMaxY(_textFrame)+margin;

    _picFrame=CGRectMake(picX, picH, picW, picY);

    

    //6计算高度

    CGFloat cellH=0;

    if (self.weibo.picture) {

        cellH=CGRectGetMaxY(_picFrame)+margin;

    }else

    {

        cellH=CGRectGetMaxY(_textFrame)+margin;

    }

    self.rowHeight=cellH;

    NSLog(@"%f",cellH);

}


//根据给定的字符串,最大值的size,给定的字体,来计算文字的大小

-(CGSize)sizeWithText:(NSString*)text MaxSize:(CGSize)maxSize font:(UIFont*)fron

{

    NSDictionary *attr=@{NSFontAttributeName:fron};

    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil].size;

}




@end



V

//

//  WeiboCell.h

//  微博

//

//  Created by Tony on 15/9/28.

//  Copyright (c) 2015 Tony. All rights reserved.

//


#import <UIKit/UIKit.h>

#import "Weibo.h"

#import "WeiboFrame.h"


@interface WeiboCell : UITableViewCell


//公开属性,用于外部赋值,M文件重写set方法,用于赋值子控件

@property(nonatomic,strong)WeiboFrame *model;


+(instancetype)weibo:(UITableView*)tableView;

@end



//

//  WeiboCell.m

//  微博

//

//  Created by Tony on 15/9/28.

//  Copyright (c) 2015 Tony. All rights reserved.

//


#import "WeiboCell.h"

#define fontSize [UIFont systemFontOfSize:12]

#define textSize [UIFont systemFontOfSize:14]



@interface WeiboCell ()


@property(nonatomic,weak)UIImageView *imgViewIcon;

@property(nonatomic,weak)UILabel * lblNickName;

@property(nonatomic,weak)UIImageView *imgVip;

@property(nonatomic,weak)UILabel *lblbText;

@property(nonatomic,weak)UIImageView *imgViewPicture;



@end


@implementation WeiboCell


//类方法,用来初始化这个类

+ (instancetype)weibo:(UITableView *)tableView

{

    //2.创建单元格

    static NSString *ID=@"weibo_cell";

    WeiboCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];

    if (cell==nil) {

        cell=[[WeiboCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];

    }

    return cell;

}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        //创建5个字控件

        //头像

        UIImageView *imgViewIcon=[[UIImageView alloc] init];

        [self.contentView addSubview:imgViewIcon];

        self.imgViewIcon=imgViewIcon;

        //昵称

        UILabel * lblNickName=[[UILabel alloc] init];

        lblNickName.font=fontSize ;

        [self.contentView addSubview:lblNickName];

        self.lblNickName=lblNickName;

        //会员

        UIImageView *imgVip=[[UIImageView alloc] init];

        imgVip.image=[UIImage imageNamed:@"vip"];

        [self.contentView addSubview:imgVip];

        self.imgVip=imgVip;

        //正文

        UILabel *lblbText=[[UILabel alloc] init];

        lblbText.font=textSize;

        lblbText.numberOfLines=0;

        [self.contentView addSubview:lblbText];

        self.lblbText=lblbText;

        //配图

        UIImageView *imgViewPicture=[[UIImageView alloc] init];

        [self.contentView addSubview:imgViewPicture];

        self.imgViewPicture=imgViewPicture;

        

    }

    return self;

}




- (void)awakeFromNib {

    // Initialization code

}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    

    // Configure the view for the selected state

}



-(void)setModel:(WeiboFrame *)model

{

    _model=model;

    

    //1.设置子控件的数据

    [self settingData];

    //2控制frame

    [self settingFrame];

    

}



-(void)settingData

{

    //头像

    self.imgViewIcon.image=[UIImage imageNamed:self.model.weibo.ico];

    //昵称

    self.lblNickName.text=self.model.weibo.name;

    //会员

    if (self.model.weibo.isVip) {

        self.imgVip.hidden=NO;

        self.lblNickName.textColor=[UIColor redColor];

    }else

    {

        self.imgVip.hidden=YES;

        self.lblNickName.textColor=[UIColor blackColor];

    }

    //正文

    self.lblbText.text=self.model.weibo.text;

    //配图

    if (self.model.weibo.picture) {

        self.imgViewPicture.image=[UIImage imageNamed:self.model.weibo.picture];

        self.imgViewPicture.hidden=NO;

    }else

    {

        self.imgViewPicture.hidden=YES;

    }

}



-(void)settingFrame

{

    //1头像

    self.imgViewIcon.frame=self.model.iconFrame;

    

    //2昵称

    self.lblNickName.frame=self.model.nameFrame;

    

    //3会员

    self.imgVip.frame=self.model.vipFrame;

    

    //4正文

    self.lblbText.frame=self.model.textFrame;

    

    //5配图

    self.imgViewPicture.frame=self.model.picFrame;

    

}







C控制器

//

//  TableViewController.m

//  微博

//

//  Created by Tony on 15/9/28.

//  Copyright (c) 2015 Tony. All rights reserved.

//


#import "TableViewController.h"

#import "Weibo.h"

#import "WeiboFrame.h"

#import "WeiboCell.h"


@interface TableViewController ()


@property(nonatomic,strong)NSArray *weiboFrames;

@end


@implementation TableViewController


- (NSArray *)weiboFrames

{

    if (_weiboFrames==nil) {

        NSString * path=[[NSBundle mainBundle] pathForResource:@"weibos.plist" ofType:nil];

        NSArray *arrayDict=[NSArray arrayWithContentsOfFile:path];

        NSMutableArray *arrayModel=[NSMutableArray array];

        for (NSDictionary *dict in arrayDict) {

            Weibo *model=[Weibo weibiWithDict:dict];

            WeiboFrame *modelFrame=[[WeiboFrame alloc] init];

            modelFrame.weibo=model;

            [arrayModel addObject:modelFrame];

        }

        _weiboFrames=arrayModel;

    }

    return _weiboFrames;


}



- (void)viewDidLoad {

    [super viewDidLoad];

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


#pragma mark - Table view data source


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

    return 1;

}


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


    return self.weiboFrames.count;

}


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

{

    //1获取模型数据

    WeiboFrame *model=self.weiboFrames[indexPath.row];

    //2.创建单元格

    WeiboCell *cell=[WeiboCell weibo:tableView];

    //3.设置单元格数据

    cell.model=model;

    //4返回单元格

    return cell;

}

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

{

    

 WeiboFrame *model=  self.weiboFrames[indexPath.row];

    return model.rowHeight;

}

- (BOOL)prefersStatusBarHidden

{

    return YES;

}

@end



















@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值