ITeye博客

自制微信的ui

haoningabc · 2014-10-19 22:28 · ios · 移动开发 · iOS · Markdown

参考http://ios.9tech.cn/news/2013/1111/38520.html 修改

配置绑定


#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
    IBOutlet UITableView *tableViewList;
    IBOutlet UITextField *messageTxt;
}
@property (nonatomic, strong) NSMutableArray *resultArray;
@end


//
//  ViewController.m
//  BubbleDemo
//
//  Created by xiao7 on 14/10/19.
//  Copyright (c) 2014年 killinux. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
- (IBAction)sendBubbleMessage:(id)sender {
    NSString *thistext = messageTxt.text;
    NSLog(@"sendBubbleMessage:%@",thistext);
    messageTxt.text=nil;
     NSDictionary *dict8 = [nsdictionary dictionarywithobjectsandkeys:@"weixin",@"name",thistext,@"content", nil];
    [_resultarray addobject:dict8];
    [tableviewlist reloaddata];
}

- (void)viewDidLoad {
    [super viewdidload];
    NSDictionary *dict = [nsdictionary dictionarywithobjectsandkeys:@"weixin",@"name",@"这是一个测试",@"content", nil];
    NSDictionary *dict1 = [nsdictionary dictionarywithobjectsandkeys:@"haoning",@"name",@"hello",@"content", nil];

    NSDictionary *dict7 = [nsdictionary dictionarywithobjectsandkeys:@"weixin",@"name",@",长数据测试。",@"content", nil];
    
    _resultArray = [nsmutablearray arraywithobjects:dict,dict1, nil];
    [_resultarray addobject:dict7];

}

- (void)didReceiveMemoryWarning {
    [super didreceivememorywarning];
    // Dispose of any resources that can be recreated.
}
//泡泡文本
- (UIView *)bubbleView:(NSString *)text from:(BOOL)fromSelf withPosition:(int)position{
    
    //计算大小
    UIFont *font = [uifont systemfontofsize:14];
    CGSize size = [text sizewithfont:font constrainedtosize:cgsizemake(180.0f, 20000.0f) linebreakmode:nslinebreakbywordwrapping];
    
    // build single chat bubble cell with given text
    UIView *returnView = [[uiview alloc] initWithFrame:CGRectZero];
    returnView.backgroundColor = [uicolor clearcolor];
    
    //背影图片
    UIImage *bubble = [uiimage imagewithcontentsoffile:[[nsbundle mainbundle] pathForResource:fromSelf?@"SenderAppNodeBkg_HL":@"ReceiverTextNodeBkg" ofType:@"png"]];
    
    UIImageView *bubbleImageView = [[uiimageview alloc] initWithImage:[bubble stretchableimagewithleftcapwidth:floorf(bubble.size.width/2) topcapheight:floorf(bubble.size.height/2)]];
    NSLog(@"%f,%f",size.width,size.height);
    
    
    //添加文本信息
    UILabel *bubbleText = [[uilabel alloc] initWithFrame:CGRectMake(fromSelf?15.0f:22.0f, 20.0f, size.width+10, size.height+10)];
    bubbleText.backgroundColor = [uicolor clearcolor];
    bubbleText.font = font;
    bubbleText.numberOfLines = 0;
    bubbleText.lineBreakMode = NSLineBreakByWordWrapping;
    bubbleText.text = text;
    
    bubbleImageView.frame = CGRectMake(0.0f, 14.0f, bubbleText.frame.size.width+30.0f, bubbleText.frame.size.height+20.0f);
    
    if(fromSelf)
        returnView.frame = CGRectMake(320-position-(bubbleText.frame.size.width+30.0f), 0.0f, bubbleText.frame.size.width+30.0f, bubbleText.frame.size.height+30.0f);
    else
        returnView.frame = CGRectMake(position, 0.0f, bubbleText.frame.size.width+30.0f, bubbleText.frame.size.height+30.0f);
    
    [returnview addsubview:bubbleimageview];
    [returnview addsubview:bubbletext];
    
    return returnView;
}

//泡泡语音
- (UIView *)yuyinView:(NSInteger)logntime from:(BOOL)fromSelf withIndexRow:(NSInteger)indexRow  withPosition:(int)position{
    
    //根据语音长度
    int yuyinwidth = 66+fromSelf;
    
    UIButton *button = [uibutton buttonwithtype:uibuttontypecustom];
    button.tag = indexRow;
    if(fromSelf)
        button.frame =CGRectMake(320-position-yuyinwidth, 10, yuyinwidth, 54);
    else
        button.frame =CGRectMake(position, 10, yuyinwidth, 54);
    
    //image偏移量
    UIEdgeInsets imageInsert;
    imageInsert.top = -10;
    imageInsert.left = fromSelf?button.frame.size.width/3:-button.frame.size.width/3;
    button.imageEdgeInsets = imageInsert;
    
    [button setimage:[uiimage imagenamed:fromself?@"sendervoicenodeplaying":@"receivervoicenodeplaying"] forState:UIControlStateNormal];
    UIImage *backgroundImage = [uiimage imagenamed:fromself?@"sendervoicenodedownloading":@"receivervoicenodedownloading"];
    backgroundImage = [backgroundimage stretchableimagewithleftcapwidth:20 topcapheight:0];
    [button setbackgroundimage:backgroundimage forstate:uicontrolstatenormal];
    
    UILabel *label = [[uilabel alloc]initWithFrame:CGRectMake(fromSelf?-30:button.frame.size.width, 0, 30, button.frame.size.height)];
    label.text = [nsstring stringwithformat:@"%d''",logntime];
    label.textColor = [uicolor graycolor];
    label.font = [uifont systemfontofsize:13];
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [uicolor clearcolor];
    [button addsubview:label];
    
    return button;
}

#pragma UITableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

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

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = [_resultarray objectatindex:indexpath.row];
    UIFont *font = [uifont systemfontofsize:14];
    CGSize size = [[dict objectforkey:@"content"] sizeWithFont:font constrainedToSize:CGSizeMake(180.0f, 20000.0f) lineBreakMode:NSLineBreakByWordWrapping];
    
    return size.height+44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier];
    if (cell == nil) {
        cell = [[uitableviewcell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }else{
        for (UIView *cellView in cell.subviews){
            [cellview removefromsuperview];
        }
    }
    NSDictionary *dict = [_resultarray objectatindex:indexpath.row];
    
    //创建头像
    UIImageView *photo ;
    if ([[dict objectforkey:@"name"]isEqualToString:@"haoning"]) {
        photo = [[uiimageview alloc]initWithFrame:CGRectMake(320-60, 10, 50, 50)];
        [cell addsubview:photo];
        photo.image = [uiimage imagenamed:@"photo1"];
        
        if ([[dict objectforkey:@"content"] isEqualToString:@"0"]) {
            [cell addsubview:[self yuyinview:1 from:yes withindexrow:indexpath.row withposition:65]];
            
        }else{
            [cell addsubview:[self bubbleview:[dict objectforkey:@"content"] from:YES withPosition:65]];
        }
        
    }else{
        photo = [[uiimageview alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
        [cell addsubview:photo];
        photo.image = [uiimage imagenamed:@"photo"];
        
        if ([[dict objectforkey:@"content"] isEqualToString:@"0"]) {
            [cell addsubview:[self yuyinview:1 from:no withindexrow:indexpath.row withposition:65]];
        }else{
            [cell addsubview:[self bubbleview:[dict objectforkey:@"content"] from:NO withPosition:65]];
        }
    }
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

@end


评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。