仿QQ布局实例

仿QQ布局实例

- (UIImageView*)iconImageView

{
   
if ( _iconImageView == nil ) {
       
_iconImageView = [[ UIImageView alloc ] init ];
        [
self . contentView addSubview : _iconImageView ];
    }
   
return _iconImageView ;
}

- (
UILabel *)timerLabel
{
   
if ( _timerLabel == nil ) {
       
_timerLabel = [[ UILabel alloc ] init ];
       
_timerLabel . textAlignment = NSTextAlignmentCenter ;
       
_timerLabel . font = [ UIFont systemFontOfSize : 13.0 ];
        [
self . contentView addSubview : _timerLabel ];
    }
   
return _timerLabel ;
}

- (
UIButton *)textBtn
{
   
if ( _textBtn == nil ) {
       
_textBtn = [[ UIButton alloc ] init ];
       
_textBtn . titleLabel . numberOfLines = 0 ;
       
_textBtn . titleLabel . font = [ UIFont systemFontOfSize : 13.0 ];
        [
self . contentView addSubview : _textBtn ];
    }
   
return _textBtn ;
}

- (
void )setQqInfo:( SUNQQInfo *)qqInfo
{
   
_qqInfo = qqInfo;
   
   
// 设置时间
   
self . timerLabel . text = qqInfo. time ;
   
CGFloat timerW = [ UIScreen mainScreen ]. bounds . size . width ;
   
self . timerLabel . frame = CGRectMake ( 0 , 0 , timerW, 35 );
   
   
// 设置头像
   
if (qqInfo. type ) {
       
CGFloat imageX = kMargin ;
       
CGFloat imageY = CGRectGetMaxY ( self . timerLabel . frame ) + kMargin ;
       
self . iconImageView . frame = CGRectMake (imageX, imageY, kIconW , kIconH );
       
self . iconImageView . image = [ UIImage imageNamed : @"other" ];
    }
else {
       
CGFloat X = [ UIScreen mainScreen ]. bounds . size . width - kMargin - kIconW ;
       
CGFloat Y = CGRectGetMaxY ( self . timerLabel . frame ) + 10 ;
       
self . iconImageView . frame = CGRectMake (X, Y, kIconW , kIconH );
       
self . iconImageView . image = [ UIImage imageNamed : @"me" ];
       
    }

   
// 设置文字
   
NSDictionary *dict = @{ NSFontAttributeName : [ UIFont systemFontOfSize : 13.0 ] } ;
   
CGRect frameText = [qqInfo. text boundingRectWithSize : CGSizeMake ( 150 , MAXFLOAT ) options : NSStringDrawingUsesLineFragmentOrigin attributes :dict context : nil ];
    [
self . textBtn setTitle :qqInfo. text forState : UIControlStateNormal ];
   
self . textBtn . contentEdgeInsets = UIEdgeInsetsMake ( 20 , 20 , 20 , 20 );
   
CGFloat textY = CGRectGetMaxY ( self . timerLabel . frame ) + 10 ;
   
CGFloat textW = frameText. size . width + 40 ;
   
CGFloat textH = frameText. size . height + 40 ;
   
CGFloat textX;
   
if (qqInfo. type ) {
        textX =
CGRectGetMaxX ( self . iconImageView . frame ) + 10 ;
    }
else {
        textX = [
UIScreen mainScreen ]. bounds . size . width - 2 * kMargin - kIconW - textW;
    }
   
self . textBtn . frame = CGRectMake (textX, textY, textW, textH);
   
   
// 设置行高
   
CGFloat maxH = MAX ( CGRectGetMaxY ( self . iconImageView . frame ), CGRectGetMaxY ( self . textBtn . frame ));
   
self . cellHight = maxH + kMargin ;
   
   
   
// 设置聊天背景
   
if (qqInfo. type ) {
       
UIImage *image = [ UIImage imageNamed : @"chat_recive_nor" ];
       
UIImage *image1 = [image stretchableImageWithLeftCapWidth : 30   topCapHeight : 30 ];
        [
self . textBtn setBackgroundImage :image1 forState : UIControlStateNormal ];
        [
self . textBtn setTitleColor :[ UIColor blackColor ] forState : UIControlStateNormal ];
    }
else {
       
UIImage *image = [ UIImage imageNamed : @"chat_send_nor" ];
       
UIImage *image1 = [image stretchableImageWithLeftCapWidth : 30 topCapHeight : 30 ];
        [
self . textBtn setBackgroundImage :image1 forState : UIControlStateNormal ];
        [
self . textBtn setTitleColor :[ UIColor whiteColor ] forState : UIControlStateNormal ];
    }
}

- ( void )viewDidLoad
{
    [
super viewDidLoad ];
NSLog ( @"%@" , self . qqs );
   
self . tableView . dataSource = self ;
   
self . tableView . delegate = self ;
   
// 去除分割线
   
self . tableView . separatorStyle = UITableViewCellSeparatorStyleNone ;
   
// 设置背景图片
   
self . tableView . backgroundColor = [ UIColor colorWithRed : 224 / 255.0 green : 224 / 255.0 blue : 224 / 255.0 alpha : 1.0 ];
   
// 界面不允许被点击
   
self . tableView . allowsSelection = NO ;
   
   
// 监听键盘的通知
    [[
NSNotificationCenter defaultCenter ] addObserver : self selector : @selector (keyboardWillChangeFrame:) name : UIKeyboardWillChangeFrameNotification object : nil ];
   
   
// 处理文本输入框
   
self . inputView . leftView = [[ UITextField alloc ] initWithFrame : CGRectMake ( 0 , 0 , 5 , 0 )];
   
self . inputView . leftViewMode = UITextFieldViewModeAlways ;
   
self . inputView . delegate = self ;
}

- (
void )keyboardWillChangeFrame:( NSNotification *)noti
{
   
/**
     UIKeyboardFrameBeginUserInfoKey = NSRect: {{0, 480}, {320, 216}};
     UIKeyboardCenterBeginUserInfoKey = NSPoint: {160, 588};
     UIKeyboardFrameEndUserInfoKey = NSRect: {{0, 264}, {320, 216}};
     UIKeyboardCenterEndUserInfoKey = NSPoint: {160, 372};
     UIKeyboardAnimationDurationUserInfoKey = 0.25;
     UIKeyboardFrameChangedByUserInteraction = 0;
     UIKeyboardBoundsUserInfoKey = NSRect: {{0, 0}, {320, 216}};
     UIKeyboardAnimationCurveUserInfoKey = 7;
     }
     */

   
   
NSLog ( @"%@" ,noti. userInfo );
   
// 设置窗口的颜色
   
self . view . window . backgroundColor = self . tableView . backgroundColor ;
   
   
// 取出键盘动画的时间
   
CGFloat duration= [noti. userInfo [ UIKeyboardAnimationDurationUserInfoKey ] doubleValue ];
   
   
// 取得键盘最后的 frame
   
CGRect keyboardFrame = [noti. userInfo [ UIKeyboardFrameEndUserInfoKey ] CGRectValue ];
   
   
// 计算控制器移动的距离
   
CGFloat tansformY = keyboardFrame. origin . y - self . view . frame . size . height ;
   
   
// 执行动画
    [
UIView animateWithDuration :duration animations :^{
       
self . view . transform = CGAffineTransformMakeTranslation ( 0 , tansformY);
    }];
}

- (
void )scrollViewWillBeginDecelerating:( UIScrollView *)scrollView
{
   
// 关闭键盘
    [
self . view endEditing : YES ];
}

- (
void )dealloc
{
    [[
NSNotificationCenter defaultCenter ] removeObserver : self ];
}

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

// 设置状态栏的可见性
- (
BOOL )prefersStatusBarHidden
{
   
return YES ;
}

#pragma mark - 实现代理的方法
- ( CGFloat )tableView:( UITableView *)tableView heightForRowAtIndexPath:( NSIndexPath *)indexPath
{
   
SUNQQView *qqView = [[ SUNQQView alloc ] init ];
    qqView.
qqInfo self . qqs [indexPath. row ];
   
return qqView. cellHight ;
}

- (
UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath
{
   
static NSString *ID = @"cell" ;
   
SUNQQView *cell = [tableView dequeueReusableCellWithIdentifier :ID];
   
if (cell == nil ) {
        cell = [[
SUNQQView alloc ] initWithStyle : UITableViewCellStyleDefault reuseIdentifier :ID];
    }
   
// 取出数据
    cell.
qqInfo = self . qqs [indexPath. row ];
    cell.
backgroundColor = [ UIColor clearColor ];
   
   
return cell;
}

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

- (
BOOL )textFieldShouldReturn:( UITextField *)textField
{
   
NSLog ( @"%@" ,textField);
   
// 添加模型
   
SUNQQInfo *qqInfo = [[ SUNQQInfo alloc ] init ];
    qqInfo.
type = 0 ;
    qqInfo.
time = @"11:23" ;
    qqInfo.
text = textField. text ;
    [
self . qqs addObject :qqInfo];
   
   
// 刷新表格
    [
self . tableView reloadData ];
   
   
// 自动滚动到最后一行
   
NSIndexPath *path = [ NSIndexPath indexPathForRow : self . qqs . count - 1 inSection : 0 ];
    [
self . tableView scrollToRowAtIndexPath :path atScrollPosition : UITableViewScrollPositionBottom animated : YES ];
   
   
// 清除文本框上的文字
   
self . inputView . text = nil ;
   
return YES ;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值