IOS第九天(1:QQ聊天界面frame模型)

///  控制层

#import "HMViewController.h"
#import "HMMessageModel.h"
#import "HMMessageCell.h"
#import "HMMessageFrameModel.h"
@interface HMViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong)NSMutableArray *messages;

@end

@implementation HMViewController

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

}

- (NSMutableArray *)messages
{
    if (_messages == nil) {

        NSArray * array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"messages.plist" ofType:nil]];

        NSMutableArray *messageArr = [NSMutableArray array];
        for (NSDictionary *dict in array) {
            HMMessageModel *messga = [HMMessageModel messageWithDict:dict];

            HMMessageFrameModel *fm = [[HMMessageFrameModel alloc]init];
            fm.message = messga;

            [messageArr addObject:fm];
        }

        _messages = messageArr;
    }

    return _messages;
}
//隐藏状态栏
- (BOOL)prefersStatusBarHidden
{
    return YES;
}

#pragma mark tableview数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.messages.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HMMessageFrameModel *model = self.messages[indexPath.row];
    return model.cellH;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HMMessageCell *cell = [HMMessageCell messageCellWithTableView:tableView];
    HMMessageFrameModel *model = self.messages[indexPath.row];

    cell.frameMessage = model;

    return cell;
}

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

@end
#import <UIKit/UIKit.h>

@interface HMViewController : UIViewController

@end

///   model.h

#import <Foundation/Foundation.h>
typedef enum {
    HMMessageModelGatsby = 0,//Gatsby
    HMMessageModelJobs//Jobs
}HMMessageModelType;
@interface HMMessageModel : NSObject

//正文
@property (nonatomic, copy)NSString *text;

//时间
@property (nonatomic, copy)NSString *time;

//发送类型
@property (nonatomic, assign)HMMessageModelType type;

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

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

@end

*****model.m

#import "HMMessageModel.h"

@implementation HMMessageModel

- (instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }

    return self;
}

+ (instancetype)messageWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}

@end

******modelframe.h

#import <Foundation/Foundation.h>
@class HMMessageModel;
@interface HMMessageFrameModel : NSObject

//时间的frame
@property (nonatomic, assign,readonly)CGRect timeF;

//正文的frame
@property (nonatomic, assign,readonly)CGRect textViewF;

//图片
@property (nonatomic, assign,readonly)CGRect iconF;

//cell
@property (nonatomic, assign,readonly)CGFloat cellH;

//数据模型
@property (nonatomic, strong)HMMessageModel *message;
@end

******modelframe.m

#import "HMMessageFrameModel.h"
#import "Constant.h"
#import "HMMessageModel.h"
@implementation HMMessageFrameModel

- (void)setMessage:(HMMessageModel *)message
{
    _message = message;

    CGFloat padding = 10;
    //1. 时间
    CGFloat timeX = 0;
    CGFloat timeY = 0;
    CGFloat timeW = bScreenWidth;
    CGFloat timeH = bNormalH;

    _timeF = CGRectMake(timeX, timeY, timeW, timeH);

    //2.头像
    CGFloat iconX;
    CGFloat iconY = CGRectGetMaxY(_timeF);
    CGFloat iconW = bIconW;
    CGFloat iconH = bIconH;

    if (message.type == HMMessageModelGatsby) {//自己发的

        iconX = bScreenWidth - iconW - padding;

    }else{//别人发的
        iconX = padding;
    }

    _iconF =  CGRectMake(iconX, iconY, iconW, iconH);
    //3.正文

    CGFloat textX;
    CGFloat textY = iconY;

    CGSize textMaxSize = CGSizeMake(150, MAXFLOAT);
    CGSize textRealSize = [message.text boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:bBtnFont} context:nil].size;

    if (message.type == HMMessageModelGatsby) {
        textX = bScreenWidth - iconW - padding - textMaxSize.width;
    }else{
        textX = padding + iconW;
    }

//    _textViewF = CGRectMake(textX, textY, <#CGFloat width#>, <#CGFloat height#>)
    _textViewF = (CGRect){{textX,textY},textRealSize};

    //4.cell高度

    CGFloat iconMaxY = CGRectGetMaxY(_iconF);
    CGFloat textMaxY = CGRectGetMaxY(_textViewF);

    _cellH = MAX(iconMaxY, textMaxY);

}

@end

////****cell.h

#import <UIKit/UIKit.h>
@class HMMessageFrameModel;
@interface HMMessageCell : UITableViewCell

+ (instancetype)messageCellWithTableView:(UITableView *)tableview;

//frame 的模型
@property (nonatomic, strong)HMMessageFrameModel *frameMessage;

@end

////****cell.m

#import "HMMessageCell.h"
#import "HMMessageFrameModel.h"
#import "HMMessageModel.h"
#import "Constant.h"
@interface HMMessageCell()
//时间
@property (nonatomic, weak)UILabel *time;
//正文
@property (nonatomic, weak)UIButton *textView;
//用户头像
@property (nonatomic, weak)UIImageView *icon;

@end

@implementation HMMessageCell
+ (instancetype)messageCellWithTableView:(UITableView *)tableview
{
    static NSString *ID = @"messageCell";
    HMMessageCell *cell = [tableview dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[self alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }

    return cell;
}
// 初始化控件
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
       //1.时间
        UILabel *time = [[UILabel alloc]init];
        time.textAlignment = NSTextAlignmentCenter;
        time.font = [UIFont systemFontOfSize:13.0f];
        [self.contentView addSubview:time];
        self.time = time;

        //1.正文
        UIButton *textView = [[UIButton alloc]init];
        textView.backgroundColor = [UIColor grayColor];
        textView.titleLabel.font = bBtnFont;
        textView.titleLabel.numberOfLines = 0;//自动换行
        [self.contentView addSubview:textView];
        self.textView = textView;

        //1.头像
        UIImageView *icon = [[UIImageView alloc]init];
        [self.contentView addSubview:icon];
        self.icon = icon;

    }
    return self;
}
// 设置位置和值
- (void)setFrameMessage:(HMMessageFrameModel *)frameMessage
{
    _frameMessage = frameMessage;

    HMMessageModel *model = frameMessage.message;

    //1.时间
    self.time.frame = frameMessage.timeF;
    self.time.text = model.time;

    //2.头像
    self.icon.frame = frameMessage.iconF;
    if (model.type == HMMessageModelGatsby) {
        self.icon.image = [UIImage imageNamed:@"Gatsby"];
    }else{
        self.icon.image = [UIImage imageNamed:@"Jobs"];
    }

    //3.正文
    self.textView.frame = frameMessage.textViewF;
    [self.textView setTitle:model.text forState:UIControlStateNormal];

}
@end
时间: 2024-10-06 00:28:39

IOS第九天(1:QQ聊天界面frame模型)的相关文章

【iOS开发-65】QQ聊天界面案例:自定义cell、图片拉伸处理、NSNotification通知、键盘与视图移动以及输入框左边缩进处理

(1)案例 (2)源代码于素材下载 http://pan.baidu.com/s/1bnpiBCz (3)总结 --还是代码封装.控制器.视图.模型分别独立.里面还有很多代码可以独立出来整一个类. --如果某一个值只有特定的几个数字,那么可以用枚举来定义,注意命名规范 typedef enum{ WPMessageTypeMe=0, WPMessageTypeOther=1 }WPMessageType; --依然是计算一段文字所占据的宽和高 CGSize textMaxSize=CGSizeM

IOS第九天(2:QQ聊天界面键盘优化 和自动回复)

***********controller.m #import "HMViewController.h" #import "HMMessageModel.h" #import "HMMessageCell.h" #import "HMMessageFrameModel.h" @interface HMViewController ()<UITableViewDataSource,UITableViewDelegate,U

IOS第九天(3:QQ聊天界面通知的使用)

#import <Foundation/Foundation.h> #import "Person.h" #import "XQCompany.h" int main(int argc, const char * argv[]) { @autoreleasepool { //初始化两个机构 XQCompany *za = [[XQCompany alloc]init]; za.name = @"珍爱网"; XQCompany *sj

Objective-c——UI基础开发第八天(QQ聊天界面)

一.知识点: QQ聊天界面 双模型的使用(dataModel和frameModel) UITextField的使用 通知的使用 拉伸图片的两种方法(slicing/image对象的resizeableImageWithCapInsets属性) 枚举 方法的抽取(相同的拿出,不同的部分作为参数) 二.设置tableview的基本格式 1)定义tableview基本 numberOfSectionsInTableView:设置块 numberOfRowsInSection:设置每块对应的行数 cel

亲身体验用Java写的仿qq聊天界面

Java开发工具有许多种,新手用记事本写Java程序,有些人用NetBean,jbuilder,高手用eclipse,下面介绍用eclipse开发qq聊天界面. 代码如下: package Myjava_QQ; import java.awt.*; import javax.swing.*; import Myjava_QQ.truess; import java.awt.event.*; import java.applet.*; import java.io.BufferedReader;

IOS开发学习笔记043-QQ聊天界面实现

QQ聊天界面实现 效果如下: ? ?实现过程: ?1.首先实现基本界面 ? ? ? ? 头像使用 UIImageView : ? ? ? ? 文字消息使用 UIButton ? ? ? ? 标签使用 UILable :水平居中 ? ? ? ? 所有元素在一个cell中,在加载cell时进行判断显示和隐藏. ? ? ? ??合理设置各个控件之间的约束关系.主要是UIIimageVIew和UIButton顶部对齐,间距为10.UIButton的宽度设置一个约束范围,比如说 (>=60 &&

【3】QQ 聊天界面

1.说明 稍微修改了下QQ示例里面的聊天界面界面,然后把代码扣过来完成了QQ聊天界面部分,效果还可以. 2.代码部分 // QQTalk.h文件 #ifndef __QQ_TALK_H__ #define __QQ_TALK_H__ #include <DuiLib/DuiLibEnv.h> #include <DuiLib/UIlib.h> using namespace DuiLib; #define QQ_TALK_XML _T("chatbox.xml"

QQ聊天界面的输入法顶起界面底部输入框效果的实现

转载请注明:http://www.cnblogs.com/frank-zouxu/p/4127115.html 今天在公司做项目的时候遇到一个需求:需要做一个界面,效果类似QQ聊天界面,如图1,当我们点击内容输入框准备输入内容的时候,底部的表情框的那一栏会被输入法的软键盘给顶起来,默认状态下,输入法会覆盖掉我们的表情输入框.起初,百思不得解的我费尽了心思,未果,偶然看到此篇博客http://blog.csdn.net/twoicewoo/article/details/7384398.其实,欲达

简单模仿QQ聊天界面

首先看一下最终的效果,显示了消息时间,用户昵称,用户头像. 大致实现方法: 用最简单的ListView显示消息内容. 不同的用户使用不同的消息布局文件,例子有2个用户"Tony","Hill". 代码文件清单: 主布局文件activity_main.xml: 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools=&