苹果ios自定义表情输入_ios8自定义键盘开发
2015-08-01 16:34:51  By: dwtedx

本Demo是一个IOS的自定义 emoji表情键盘、运行效果非常好、自定义功能强大、主要实现了两大功能一是自定义 emoji表情键盘、二是可以信息输入框、也就是说可以输入表情和文字、界面也非常漂亮、下面是程序运行效果

ios 自定义键盘


核心代码

#import "DemoViewController.h"
#import "STInputBar.h"
 
@interface DemoViewController ()
 
@end
 
@implementation DemoViewController{
    STInputBar *_inputBar;
    UIScrollView *_scrollView;
    UILabel *_textWindow;
    NSArray *_names;
}
 
- (void)viewDidLoad {
    [super viewDidLoad];
     
    _inputBar = [STInputBar inputBar];
    _inputBar.center = CGPointMake(CGRectGetWidth(self.view.frame)/2, 
        CGRectGetHeight(self.view.bounds)-CGRectGetHeight(_inputBar.frame)
         CGRectGetHeight(_inputBar.frame)/2);
    [_inputBar setFitWhenKeyboardShowOrHide:YES];
    _inputBar.placeHolder = @"小明的故事...";
     
    _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    _scrollView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    _scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    _scrollView.alwaysBounceVertical = YES;
    _scrollView.contentInset = UIEdgeInsetsMake(-10, 0, 
        CGRectGetHeight(_inputBar.frame), 0);
    [self.view addSubview:_scrollView];
     
    _textWindow = [[UILabel alloc] initWithFrame:_scrollView.bounds];
    _textWindow.numberOfLines = 0;
    _textWindow.tag = 0;
    [_scrollView addSubview:_textWindow];
     
    [self.view addSubview:_inputBar];
     
    __weak typeof(self) weakSelf = self;
    [_inputBar setDidSendClicked:^(NSString *text) {
        [weakSelf updateMessage:text];
    }];
     
    _names = @[@"小明",@"老师"];
}
 
- (void)updateMessage:(NSString *)message{
     
    static NSDictionary *_txtAtt = nil;
    if (!_txtAtt){
        _txtAtt = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],
                    NSForegroundColorAttributeName:[UIColor darkTextColor]};
    }
    static NSDictionary *_timeAtt = nil;
    if (!_timeAtt){
        _timeAtt = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:13],
        NSForegroundColorAttributeName:[UIColor colorWithWhite:0.2 alpha:0.5]};
    }
     
    NSString *name = _names[_textWindow.tag];
    _inputBar.placeHolder = @"对话...";
    NSString *reply = [NSString stringWithFormat:@"回复 %@",_names[0]];
    if (1 == _textWindow.tag){
        name = [NSString stringWithFormat:@"%@ %@",name,reply];
    }
    else{
        _inputBar.placeHolder = reply;
    }
     
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] 
        initWithAttributedString:_textWindow.attributedText];
    [text appendAttributedString:[[NSAttributedString alloc] 
        initWithString:[NSString stringWithFormat:@"

--- %@ ---
%@
",
        [self time],name] attributes:_timeAtt]];
    [text appendAttributedString:[[NSAttributedString alloc] 
        initWithString:message attributes:_txtAtt]];
    [_textWindow setAttributedText:text];
    CGSize size = [_textWindow sizeThatFits:
        CGSizeMake(CGRectGetWidth(self.view.frame), 100000)];
    _textWindow.frame = CGRectMake(0, 0, size.width, size.height);
    _scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame), 
        CGRectGetHeight(_textWindow.frame));
     
    _textWindow.tag = (_textWindow.tag 1)%_names.count;
}
 
- (NSString *)time{
    static NSDateFormatter *_dateFormatter = nil;
    if (!_dateFormatter){
        _dateFormatter = [NSDateFormatter new];
        _dateFormatter.dateFormat = @"HH:mm:ss";
    }
    return [_dateFormatter stringFromDate:[NSDate date]];
}
 
@end


若资源对你有帮助、扫描下方的二维码、关注DD博客微信公众号(ddblogs)吧

最后给贴上Demo的源代码、希望对大家有用、有兴趣的哥们可以下载看看

源代码下载链接: http://dwtedx.com/download.html?bdkey=s/1pJ9TJxp 密码: ukh8

若资源对你有帮助、浏览后有很大收获、不妨小额打赏我一下、你的鼓励是维持我不断写博客最大动力

想获取DD博客最新代码、你可以扫描下方的二维码、关注DD博客微信公众号(ddblogs)

或者你也可以关注我的新浪微博、了解DD博客的最新动态:DD博客官方微博(dwtedx的微博)

如对资源有任何疑问或觉得仍然有很大的改善空间、可以对该博文进行评论、希望不吝赐教

为保证及时回复、可以使用博客留言板给我留言: DD博客留言板(dwtedx的留言板)

感谢你的访问、祝你生活愉快、工作顺心、欢迎常来逛逛


快速评论


技术评论

  • 该技术还没有评论、赶快抢沙发吧...
DD记账
top
+