博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
新浪微博客户端(38)-显示键盘上的工具条
阅读量:4877 次
发布时间:2019-06-11

本文共 6613 字,大约阅读时间需要 22 分钟。

 

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() 
@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

 

最终效果:

 

转载于:https://www.cnblogs.com/yongdaimi/p/6102877.html

你可能感兴趣的文章
ER图是啥?
查看>>
too many include files depth = 1024错误原因
查看>>
HTTP协议详解(三)
查看>>
Android零基础入门第84节:引入Fragment原来是这么回事
查看>>
解析SQL Server之任务调度
查看>>
参考资料地址
查看>>
(转)为什么所有浏览器的userAgent都带Mozilla
查看>>
织梦字段属性筛选
查看>>
无法加载csopenglc.dll;找不到指定模块
查看>>
08.路由规则中定义参数
查看>>
Pandas截取列部分字符,并据此修改另一列的数据
查看>>
java.lang.IllegalArgumentException
查看>>
pytest
查看>>
python爬取某个网站的图片并保存到本地
查看>>
【Spark】编程实战之模拟SparkRPC原理实现自定义RPC
查看>>
关于Setup Factory 9的一些使用方法
查看>>
接口实现观察者模式
查看>>
网站Session 处理方式
查看>>
记开发个人图书收藏清单小程序开发(九)Web开发——新增图书信息
查看>>
四则运算完结篇
查看>>