新浪微博的布局

//
// MJTabbarItem.m
// 新浪微博
//
// Created by mj on 13-4-21.
// Copyright (c) 2013年 itcast. All rights reserved.
//

#import "MJTabbarItem.h"

@implementation MJTabbarItem

- (id)initWithFrame:(CGRect)frame itemDesc:(MJTabbarItemDesc *)desc {
if (self = [super initWithFrame:frame]) {
// 设置高亮显示的背景
[self setHighlightedBg:@"tabbar_slider.png"];
// 设置selected=YES时的背景
[self setSelectedBg:@"tabbar_slider.png"];

// 设置默认的Image
[self setImage:[UIImage imageNamed:desc.normal] forState:UIControlStateNormal];
// 设置selected=YES时的image
[self setImage:[UIImage imageNamed:desc.highlighted] forState:UIControlStateSelected];

// 不需要在用户长按的时候调整图片为灰色
self.adjustsImageWhenHighlighted = NO;
// 设置UIImageView的图片居中
self.imageView.contentMode = UIViewContentModeCenter;

// 设置文字
[self setTitle:desc.title forState:UIControlStateNormal];
// 设置文字居中
self.titleLabel.textAlignment = NSTextAlignmentCenter;
// 设置字体大小
self.titleLabel.font = [UIFont systemFontOfSize:10];
}
return self;
}

#pragma mark - 覆盖父类的2个方法
#pragma mark 设置按钮标题的frame
- (CGRect)titleRectForContentRect:(CGRect)contentRect {
UIImage *image = [self imageForState:UIControlStateNormal];
CGFloat titleY = image.size.height - 3;
CGFloat titleHeight = self.bounds.size.height - titleY;
return CGRectMake(0, titleY, self.bounds.size.width, titleHeight);
}
#pragma mark 设置按钮图片的frame
- (CGRect)imageRectForContentRect:(CGRect)contentRect {
UIImage *image = [self imageForState:UIControlStateNormal];
return CGRectMake(0, 0, self.bounds.size.width, image.size.height);
}

@end

@implementation MJTabbarItemDesc
+ (id)itemWithTitle:(NSString *)title normal:(NSString *)normal highlighted:(NSString *)highlighted {
MJTabbarItemDesc *desc = [[MJTabbarItemDesc alloc] init];
desc.title = title;
desc.normal = normal;
desc.highlighted = highlighted;
return [desc autorelease];
}

- (void)dealloc {
[_title release];
[_normal release];
[_highlighted release];
[super dealloc];
}
@end

============================
- (id)initWithDict:(NSDictionary *)dict {
if (self = [super init]) {
self.ID = [dict objectForKey:@"id"];
self.text = [dict objectForKey:@"text"];

NSDictionary *userDict = [dict objectForKey:@"user"];
if (userDict) {
self.user = [[[User alloc] initWithDict:userDict] autorelease];
}

NSDictionary *retweetedDict = [dict objectForKey:@"retweeted_status"];
if (retweetedDict) {
self.retweetedStatus = [[[Status alloc] initWithDict:retweetedDict] autorelease];
}

self.thumbnailPic = [dict objectForKey:@"thumbnail_pic"];

self.frame = [[[StatusFrame alloc] initWithStatus:self] autorelease];
}
return self;
}

- (void)dealloc {
[_user release];
[_retweetedStatus release];
[_ID release];
[_thumbnailPic release];
[_frame release];
[_text release];
[super dealloc];
}
@end


// StatusCell.m
// 新浪微博
//
// Created by mj on 13-4-23.
// Copyright (c) 2013年 itcast. All rights reserved.
==========================================================

#import <Foundation/Foundation.h>

// 昵称的字体大小
#define kScreenNameSize 12
// 微博内容大小
#define kStatusSize 15

@class Status;
@interface StatusFrame : NSObject
- (id)initWithStatus:(Status *)statusContent;
// cell的高度
@property (nonatomic, readonly) CGFloat cellHeight;

// 用户头像
@property (nonatomic, readonly) CGRect icon;
// 用户昵称
@property (nonatomic, readonly) CGRect screenName;
// 微博内容
@property (nonatomic, readonly) CGRect status;
// 微博配图
@property (nonatomic, readonly) CGRect image;

// 转发微博的整体布局
@property (nonatomic, readonly) CGRect retweet;
// 转发微博的昵称
@property (nonatomic, readonly) CGRect retweetScreenName;
// 转发微博的内容
@property (nonatomic, readonly) CGRect retweetStatus;
// 转发微博的配图
@property (nonatomic, readonly) CGRect retweetImage;

@end


#define kPadding 10

#define kIconWidth 40
#define kIconHeight 40

#define kImageWidth 100
#define kImageHeight 100

#import "StatusFrame.h"
#import "Status.h"

@implementation StatusFrame
- (id)initWithStatus:(Status *)statusContent {
if (self = [super init]) {
CGFloat winWidth = [UIScreen mainScreen].bounds.size.width;

// 头像
CGFloat iconX = kPadding;
CGFloat iconY = kPadding;
_icon = CGRectMake(iconX, iconY, kIconWidth, kIconHeight);

// 昵称
CGFloat screenNameX = 2* iconX + kIconWidth;
CGFloat screenNameY = iconY;
CGFloat screenNameWidth = winWidth - screenNameX - kPadding;
CGFloat screenNameHeight = [UIFont systemFontOfSize:kScreenNameSize].lineHeight;
_screenName = CGRectMake(screenNameX, screenNameY, screenNameWidth, screenNameHeight);

// 微博
CGFloat statusX = screenNameX;
CGFloat statusY = screenNameY + screenNameHeight + kPadding;
CGFloat statusWidth = screenNameWidth;
CGSize statusSize = [statusContent.text sizeWithFont:[UIFont systemFontOfSize:kStatusSize] constrainedToSize:CGSizeMake(statusWidth, 1000) lineBreakMode:NSLineBreakByWordWrapping];
CGFloat statusHeiht = statusSize.height;
_status = CGRectMake(statusX, statusY, statusWidth, statusHeiht);

_cellHeight = statusY + statusHeiht + kPadding;

// 如果有微博配图
if (statusContent.thumbnailPic) {
CGFloat imageX = statusX;
CGFloat imageY = statusY + statusHeiht + kPadding;
_image = CGRectMake(imageX, imageY, kImageWidth, kImageHeight);

_cellHeight = imageY + kImageHeight + kPadding;
}

// 如果有转发微博
if (statusContent.retweetedStatus) {
// 被转发的微博整体框架
CGFloat retweetX = statusX;
CGFloat retweetY = statusY + statusHeiht + kPadding;
CGFloat retweetWidth = statusWidth;
CGFloat retweetHeight;

// 被转发微博的昵称
CGFloat retweetScreenNameX = kPadding;
CGFloat retweetScreenNameY = kPadding;
CGFloat retweetScreenNameWidth = retweetWidth- retweetScreenNameX - kPadding;
CGFloat retweetScreenNameHeight = [UIFont systemFontOfSize:kStatusSize].lineHeight;
_retweetScreenName = CGRectMake(retweetScreenNameX, retweetScreenNameY, retweetScreenNameWidth, retweetScreenNameHeight);

// 被转发微博的内容
CGFloat retweetStatusX = retweetScreenNameX;
CGFloat retweetStatusY = retweetScreenNameY + retweetScreenNameHeight + kPadding;
CGFloat retweetStatusWidth = retweetScreenNameWidth;

CGSize retweetStatusSize = [statusContent.retweetedStatus.text sizeWithFont:[UIFont systemFontOfSize:kStatusSize] constrainedToSize:CGSizeMake(retweetStatusWidth, 1000) lineBreakMode:NSLineBreakByWordWrapping];
CGFloat retweetStatusHeight = retweetStatusSize.height;
_retweetStatus = CGRectMake(retweetStatusX, retweetStatusY, retweetStatusWidth, retweetStatusHeight);

retweetHeight = retweetStatusY + retweetStatusHeight + kPadding;

// 被转发的微博有配图
if (statusContent.retweetedStatus.thumbnailPic) {
CGFloat retweetImageX = retweetStatusX;
CGFloat retweetImageY = retweetStatusY + retweetStatusHeight + kPadding;
_retweetImage = CGRectMake(retweetImageX, retweetImageY, kImageWidth, kImageHeight);


retweetHeight = retweetImageY + kImageHeight + kPadding;
}

_retweet = CGRectMake(retweetX, retweetY, retweetWidth, retweetHeight);

_cellHeight = retweetY + retweetHeight + kPadding;
}
}
return self;
}
@end

==================================================================

#import "StatusCell.h"
#import "Status.h"
#import "User.h"
#import "StatusFrame.h"
// 昵称的字体大小
#define kScreenNameSize 12
// 微博内容大小
#define kStatusSize 15

@interface StatusCell() {
// 用户昵称
UILabel *_screenName;
// 微博内容
UILabel *_status;
// 被转发的微博
UIImageView *_retweetView;
// 被转发微博的用户昵称
UILabel *_retweetScreenName;
// 被转发微博的内容
UILabel *_retweetStatus;
}
@end

@implementation StatusCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// 设置cell的背景色
UIView *bg = [[[UIView alloc] init] autorelease];
bg.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
self.backgroundView = bg;

// 选中的背景
UIView *selectdBg = [[[UIView alloc] init] autorelease];
selectdBg.backgroundColor = [UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1];
self.selectedBackgroundView = selectdBg;

// 用户头像
UIImageView *icon = [[[UIImageView alloc] init] autorelease];
[self.contentView addSubview:icon];
_icon = icon;

// 用户昵称
UILabel *screenName = [[[UILabel alloc] init] autorelease];
screenName.backgroundColor = [UIColor clearColor];
screenName.textColor = [UIColor blackColor];
screenName.font = [UIFont systemFontOfSize:kScreenNameSize];
[self.contentView addSubview:screenName];
_screenName = screenName;

// 微博内容
UILabel *status = [[[UILabel alloc] init] autorelease];
status.font = [UIFont systemFontOfSize:kStatusSize];
// numberOfLines = 0代表自动换行
status.numberOfLines = 0;
status.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:status];
_status = status;

// 微博配图
UIImageView *image = [[[UIImageView alloc] init] autorelease];
// 图片居中,而且限制在ImageView的bounds中
image.contentMode = UIViewContentModeScaleAspectFit;
[self.contentView addSubview:image];
_image = image;

// 被转发的微博
UIImageView *retweetView = [[[UIImageView alloc] init] autorelease];
retweetView.image = [UIImage resizeImage:@"retweet_background.png"];
[self.contentView addSubview:retweetView];
_retweetView = retweetView;

// 被转发微博的用户昵称
UILabel *retweetScreenName = [[[UILabel alloc] init] autorelease];
retweetScreenName.textColor = [UIColor blueColor];
retweetScreenName.backgroundColor = [UIColor clearColor];
retweetScreenName.font = [UIFont systemFontOfSize:kStatusSize];
[retweetView addSubview:retweetScreenName];
_retweetScreenName = retweetScreenName;

// 被转发微博的内容
UILabel *retweetStatus = [[[UILabel alloc] init] autorelease];
retweetStatus.backgroundColor = [UIColor clearColor];
retweetStatus.font = [UIFont systemFontOfSize:kStatusSize];
retweetStatus.numberOfLines = 0;
[retweetView addSubview:retweetStatus];
_retweetStatus = retweetStatus;

// 被转发微博的配图
UIImageView *retweetImage = [[[UIImageView alloc] init] autorelease];
// 图片居中,而且限制在ImageView的bounds中
retweetImage.contentMode = UIViewContentModeScaleAspectFit;
[retweetView addSubview:retweetImage];
_retweetImage = retweetImage;
}
return self;
}

- (void)setStatusContent:(Status *)statusContent {
if (_statusContent != statusContent) {
[_statusContent release];
_statusContent = [statusContent retain];


// 用户昵称
_screenName.text = statusContent.user.screenName;
// 微博内容
_status.text = statusContent.text;

if (statusContent.thumbnailPic) {
_image.hidden = NO;
} else {
_image.hidden = YES;
}

Status *retweet = statusContent.retweetedStatus;
if (retweet) {
_retweetView.hidden = NO;
_retweetScreenName.text = [NSString stringWithFormat:@"@%@", retweet.user.screenName];
_retweetStatus.text = retweet.text;

if (retweet.thumbnailPic) {
_retweetImage.hidden = NO;
} else {
_retweetImage.hidden = YES;
}
} else {
_retweetView.hidden = YES;
}
}
}

#pragma mark 重新布局所有的子控件
- (void)layoutSubviews {
[super layoutSubviews];

_icon.frame = _statusContent.frame.icon;
_screenName.frame = _statusContent.frame.screenName;
_status.frame = _statusContent.frame.status;
_image.frame = _statusContent.frame.image;

_retweetView.frame = _statusContent.frame.retweet;
_retweetScreenName.frame = _statusContent.frame.retweetScreenName;
_retweetStatus.frame = _statusContent.frame.retweetStatus;
_retweetImage.frame = _statusContent.frame.retweetImage;
}

- (void)dealloc {
[_statusContent release];
[super dealloc];
}
@end

原文地址:https://www.cnblogs.com/gcb999/p/3149265.html