新浪微博客户端(38)-显示键盘上的工具条

DJComposeToolbar.m

#import "DJComposeToolbar.h"

@implementation DJComposeToolbar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"compose_toolbar_backgrounds"]];

        [self setupBtnWithImage:@"compose_camerabutton_background" hilightImage:@"compose_camerabutton_background_highlighted"];
        [self setupBtnWithImage:@"compose_toolbar_picture" hilightImage:@"compose_toolbar_picture_highlighted"];
        [self setupBtnWithImage:@"compose_mentionbutton_background" hilightImage:@"compose_mentionbutton_background_highlighted"];
        [self setupBtnWithImage:@"compose_trendbutton_background" hilightImage:@"compose_trendbutton_background_highlighted"];
        [self setupBtnWithImage:@"compose_emoticonbutton_background" hilightImage:@"compose_emoticonbutton_background_highlighted"];

    }
    return self;
}

- (void)setupBtnWithImage:(NSString *)image hilightImage:(NSString *)hilightImage {

    UIButton *btn = [[UIButton alloc] init];
    [btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:hilightImage] forState:UIControlStateHighlighted];

    [self addSubview:btn];

}

- (void)layoutSubviews {

    [super layoutSubviews];

    NSUInteger count = self.subviews.count;
    CGFloat btnY = 0;
    CGFloat btnW = self.width / count;
    CGFloat btnH = self.height;
    for (int i = 0; i < count; i++) {
        UIButton *btn = self.subviews[i];
        btn.x = i * btnW;
        btn.y = btnY;
        btn.width = btnW;
        btn.height = btnH;
    }

}

@end

DJComposeViewControll.m

#import "DJComposeViewController.h"
#import "DJAccountTool.h"
#import "DJTextView.h"
#import "AFHTTPSessionManager.h"
#import "MBProgressHUD+MJ.h"
#import "DJComposeToolbar.h"

@interface DJComposeViewController() <UITextViewDelegate>

@property (nonatomic,weak) DJTextView *textView;
@property (nonatomic,weak) DJComposeToolbar *toolbar;

@end

@implementation DJComposeViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    [self initNavigationView];
    [self initTextView];
    [self initComposeToolbar];
}

- (void)viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];
     self.navigationItem.rightBarButtonItem.enabled = NO;

}

/** 初始化工具条 */
- (void)initComposeToolbar {

    DJComposeToolbar *toolbar = [[DJComposeToolbar alloc] init];
    toolbar.width = self.view.width;
    toolbar.height = 44;
    toolbar.x = 0;
    toolbar.y = self.view.height - toolbar.height;
    [self.view addSubview:toolbar];
    self.toolbar = toolbar;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];

}

/** 初始化NavigationView */
- (void)initNavigationView {

    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(finish)];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发送" style:UIBarButtonItemStylePlain target:self action:@selector(send)];

    UILabel *titleView = [[UILabel alloc] init];
    titleView.width = 200;
    titleView.height = 44;
    titleView.numberOfLines = 0; // 设置titleView 为多行显示
    titleView.textAlignment = NSTextAlignmentCenter;

    DJAccount *account = [DJAccountTool account];
    NSString *nickName = account.screen_name;
    NSString *prefix = @"发微博";
    NSString *str = [NSString stringWithFormat:@"%@\n%@",prefix,nickName];
    NSRange nick_name_range = [str rangeOfString:nickName];
    NSRange prefix_range = [prefix rangeOfString:prefix];

    NSMutableAttributedString *titleStr = [[NSMutableAttributedString alloc] initWithString:str];
    [titleStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:nick_name_range];
    [titleStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:15] range:prefix_range];

    titleView.attributedText = titleStr;
    self.navigationItem.titleView = titleView;

}

/** 初始化输入区域 */
- (void)initTextView {

    DJTextView *textView = [[DJTextView alloc] init];
    textView.frame = self.view.bounds;
    textView.font = [UIFont systemFontOfSize:14];
    textView.placeholder = @"请输入微博内容";
    textView.placeholderColor = [UIColor grayColor];
    textView.alwaysBounceVertical = YES;
    [self.view addSubview:textView];
    self.textView = textView;
    textView.delegate = self; // 类似于android里面的setOnClickListener(this)方法

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textHasChange) name:UITextViewTextDidChangeNotification object:textView];

}

- (void)finish {

    [self dismissViewControllerAnimated:YES completion:nil];

}

/** 监听TextView文本改变 */
- (void)textHasChange {

    // 若用户已经为textView输入了文本,则发送按钮可点击
    self.navigationItem.rightBarButtonItem.enabled = self.textView.hasText;

}

/** 发微博 */
- (void)send {

    [self sendStatusRequest];

}

/** 发微博 */
- (void)sendStatusRequest {

    AFHTTPSessionManager *RequestManager = [AFHTTPSessionManager manager];
    NSString *urlString = @"https://api.weibo.com/2/statuses/update.json";

    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"access_token"] = [DJAccountTool account].access_token;
    params[@"status"] = self.textView.text;

    [RequestManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [MBProgressHUD showSuccess:@"发送成功"];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MBProgressHUD showError:@"发送失败"];
    }];
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 接收键盘frame改变通知(类似于android中的接收广播)
- (void)keyboardWillChangeFrameNotification:(NSNotification *)notification {
    NSDictionary *intent = notification.userInfo;
    // 键盘的frame
    CGRect keyboardF = [intent[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    // 动画的执行时间
    double duration = [intent[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    [UIView animateWithDuration:duration animations:^{
        self.toolbar.y = keyboardF.origin.y - self.toolbar.height;
    }];

}

#pragma mark - ScrollView 代理方法(当用户拖动TextView时使键盘消失)
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self.view endEditing:YES];

}

- (void)dealloc {

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

@end

最终效果:

时间: 2024-12-06 09:38:45

新浪微博客户端(38)-显示键盘上的工具条的相关文章

给键盘添加一个工具条

开发中经常遇到要给键盘添加一个工具条,工具条上有按钮,点击后可以隐藏键盘的情况.比如下面:需要弹出纯数字键盘,这个时候就需要通过工具条来隐藏键盘了. 场景:点击一个cell中的textfield弹出数字键盘,上面加上工具条 步骤1:在cellforrow中创建工具条. 步骤2:把textfield的inputAccessoryView 属性 = 你创建的工具条 步骤3:把textfield的keyboradType 属性 =  UIKeyboardTypeNumberPad 步骤4:实现工具条中

新浪微博客户端(45)-显示表情

DJEmotionListView.m - (void)setEmotions:(NSArray *)emotions { _emotions = emotions; // emotion 总个数 NSUInteger emotionCount = emotions.count; // 页码总个数 NSUInteger pageNums = (emotionCount + DJEmotionPageSize - 1) / DJEmotionPageSize; // 更新pageControl 显

iOS_22自定义键盘工具条

最终效果图: Main.storyboard KeyboardTool.xib KeyboardTool.h // KeyboardTool.h // 键盘处理 // Created by beyond on 14-8-24. // Copyright (c) 2014年 com.beyond. All rights reserved. #import <UIKit/UIKit.h> @protocol KeyboardToolDelegate; typedef enum { kKeyboar

CAD隐藏或显示工具条上的按钮(com接口VB语言)

主要用到函数说明: MxDrawXCustomFunction::Mx_HideToolBarControl 隐藏或显示工具条上的按钮.详细说明如下: 参数 说明 IN LPCTSTR pszToolBarName 工条名称 IN LPCTSTR pszControlName = NULL 按钮名称,多个按钮名称,可以用逗号分隔开,为空,隐藏或显示所有按钮 IN BOOL isHide = TRUE 是否隐藏按钮 IN BOOL isAutoRecalcLayout = TRUE 是否自动重新布

iOS开发项目篇—29自定义工具条

iOS开发项目篇—29自定义工具条 一.简单说明 1.实现效果: 2.实现思路: (1)尝试: 1 //添加子控件 2 -(void)setupTextView 3 { 4 //1.创建输入控件 5 YYTextView *textView=[[YYTextView alloc]init]; 6 //设置frame 7 textView.frame=self.view.bounds; 8 [self.view addSubview:textView]; 9 self.textView=textV

菜单与工具条的同步 APP_STANDARD.SYNCHRONIZE

初始情况下,菜单与工具条的状态是一致的,但程序中动态改变某一属性时,工具条并不能相应地改变,所以必须编写代码完成同步.通过调用以下函数来完成同步:APP_STANDARD.SYNCHRONIZE需要注意的是,当触发以下TRIGGER 以后,同步将自动完成,无需人工完成.WHEN–NEW–RECORD–INSTANCEWHEN–NEW–BLOCK–INSTANCEWHEN–NEW–ITEM–INSTANCE在FORM的开发过程中,我们在代码中设置了块的可修改.可删除属性时,但是却菜单栏上的工具条并

设置键盘顶部显示的工具条

- (void)createKeyboardTool { keyboardTool = [[UIToolbar alloc] initWithFrame: CGRectMake(kZero, kZero, kScreenW, 44.0f)]; NSMutableArray *myToolBarItems = [NSMutableArray array]; //创建键盘工具条上面的按钮,并设置点击事件 UIBarButtonItem *cancelBtn = [[UIBarButtonItem a

让UIWebView弹出键盘上的按钮显示中文

UIWebView是一个很常用的视图,一般用来加载网页,比如百度: 点击文本框输入框后,会弹出一个带有toolbar的键盘,toolbar中有3个辅助按钮 有了这3个按钮,是方便很多,但默认是英文的,有时我们想把按钮文字变为中文 其实办法很简单,只需要让你的应用程序支持中文本地化,意思是在项目中新建一个中文的本地化文件夹zh-Hans.lproj 如果还不太了解什么叫本地化,可以看看我的这篇文章<应用程序本地化> 下面简单演示下操作步骤: 1.添加中文本地化支持 2.选择要支持本地化的文件,至

android开发新浪微博客户端 完整攻略 [新手必读]

开始接触学习android已经有3个礼拜了,一直都是对着android的sdk文档写Tutorials从Hello World到Notepad Tutorial算是初步入门了吧,刚好最近对微博感兴趣就打算开发个android版本的新浪微博客户端作为练手项目,并且以随笔的方式详细的记录开发的全过程.本人对java语言以及eclipse Ide都是初次应用基本上属于边学边用,做移动设备上的东西也是第一次,总的来说属于无基础.无经验.无天赋的纯三无人员,还请广大同学们多多给予指点. 开发第一件事情,那