|
//
// writeCommentAndAskController.m
// ThePaperDemo
//
// Created by scar1900 on 14/12/25.
// Copyright (c) 2014年 scar1900. All rights reserved.
//
#import "writeCommentAndAskController.h"
//#import "LCAlertView.h"
#import "emojiView.h"
#import "emojiAttachment.h"
#import "UIControl+runTimeProtect.h"
#define writeContentHeight 185
#define textViewTopPadding 40
#define textViewHeight 100
#define emojiHeight 433/2
@interface writeCommentAndAskController ()<UITextViewDelegate,UIAlertViewDelegate,emojiViewDelegate,UIScrollViewDelegate> {
CGFloat keyboardOffset;
NSString *sourceString; //单纯字符串数据源
NSMutableAttributedString *textViewTrueString; //textview显示的真实字符串
}
@property(nonatomic, strong)UIView *writeContentView;
@property(nonatomic, strong)UITextView *writeTextView;
@property(nonatomic, assign)NSInteger limitTextNum;
@property(nonatomic, strong)UILabel *placeHolderLabel;
@property(nonatomic, strong)NSString *commentContent;
@property(nonatomic, strong)UIButton *backButton;
@property(nonatomic, strong)UIButton *cancelBtn;
@property(nonatomic, strong)UIButton *confirmBtn;
@property(nonatomic, strong)UIButton *switchEmojiBtn;
@property(nonatomic, strong)emojiView *emojiContent;
@end
@implementation writeCommentAndAskController
@synthesize type = _type;
@synthesize commentBO;
@synthesize limitTextNum;
@synthesize nodeId;
@synthesize delegate;
@synthesize commentContent;
@synthesize cancelBtn,confirmBtn;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.backButton];
// self.view.clipsToBounds = YES;
[self.view addSubview:self.writeContentView];
[self.writeContentView addSubview:self.writeTextView];
[self.writeContentView addSubview:self.placeHolderLabel];
cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// [cancelBtn setImage:Image(@"detailPage/commentCancel.png") forState:UIControlStateNormal];
// [cancelBtn setImage:Image(@"detailPage/commentCancel_s.png") forState:UIControlStateHighlighted];
[cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
[cancelBtn setTitleColor:[UIColor colorWithHexString:TextBlack] forState:UIControlStateNormal];
[cancelBtn setTitleColor:[UIColor colorWithHexString:TextLightGray] forState:UIControlStateHighlighted];
[cancelBtn setTitleColor:[UIColor colorWithHexString:LIGHTGRAY] forState:UIControlStateDisabled];
[cancelBtn addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
cancelBtn.titleLabel.font = appFont(TEXT_FOUR_LEVELSIZE, NO);
cancelBtn.frame = CGRectMake(0,
0,
50,
textViewTopPadding);
[self.writeContentView addSubview:cancelBtn];
confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// [confirmBtn setImage:Image(@"setting/offlineDownload_s.png") forState:UIControlStateNormal];
// [confirmBtn setImage:Image(@"detailPage/confirm_s.png") forState:UIControlStateHighlighted];
[confirmBtn setTitle:@"发表" forState:UIControlStateNormal];
[confirmBtn setTitleColor:[UIColor colorWithHexString:TextBlack] forState:UIControlStateNormal];
[confirmBtn setTitleColor:[UIColor colorWithHexString:TextLightGray] forState:UIControlStateHighlighted];
[confirmBtn addTarget:self action:@selector(confirmEvent:) forControlEvents:UIControlEventTouchUpInside];
[confirmBtn setTitleColor:[UIColor colorWithHexString:LIGHTGRAY] forState:UIControlStateDisabled];
confirmBtn.titleLabel.font = appFont(TEXT_FOUR_LEVELSIZE, NO);
confirmBtn.frame = CGRectMake(CGRectGetWidth(self.writeContentView.bounds)-50,
0,
50,
textViewTopPadding);
[self.writeContentView addSubview:confirmBtn];
[self setPlaceHolderOfTextView];
[self.writeContentView addSubview:self.switchEmojiBtn];
[self.writeTextView becomeFirstResponder];
//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
if (self.writeTextView.text.length == 0) confirmBtn.enabled = NO;
}
- (UIButton*)backButton {
if (!_backButton) {
_backButton = [UIButton buttonWithType:UIButtonTypeCustom];
_backButton.backgroundColor = [UIColor clearColor];
_backButton.frame = self.view.bounds;
[_backButton addTarget:self action:@selector(clickBack:) forControlEvents:UIControlEventTouchUpInside];
}
return _backButton;
}
- (UIView*)writeContentView {
if (!_writeContentView) {
_writeContentView = [[UIView alloc]initWithFrame:CGRectMake(0,
CGRectGetHeight(self.view.bounds)-writeContentHeight,
CGRectGetWidth(self.view.bounds),
writeContentHeight)];
_writeContentView.backgroundColor = [UIColor colorWithHexString:writeContentColor];
}
return _writeContentView;
}
- (UITextView*)writeTextView {
if (!_writeTextView) {
_writeTextView = [[UITextView alloc]initWithFrame:CGRectMake(10,
textViewTopPadding,
CGRectGetWidth(self.writeContentView.bounds)-20,
textViewHeight)];
_writeTextView.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
_writeTextView.scrollEnabled = YES;
_writeTextView.delegate =self;
_writeTextView.textAlignment = NSTextAlignmentLeft;
_writeTextView.textColor = [UIColor colorWithHexString:TextBlack];
_writeTextView.font = appFont(TEXT_FOUR_LEVELSIZE, NO);
// _writeTextView.layer.cornerRadius = 3.f;
_writeTextView.keyboardAppearance = UIKeyboardAppearanceDefault;
if ([[TPUserDefault instance].isNightMode intValue] > 0) {
_writeTextView.keyboardAppearance = UIKeyboardAppearanceDark;
}
_writeTextView.layer.borderColor = [UIColor colorWithHexString:LINECOLOR].CGColor;
_writeTextView.layer.borderWidth = 0.5f;
}
return _writeTextView;
}
- (UILabel*)placeHolderLabel {
if (!_placeHolderLabel) {
_placeHolderLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, textViewTopPadding+5, CGRectGetWidth(self.writeTextView.frame)-40, 50)];
_placeHolderLabel.textAlignment = NSTextAlignmentLeft;
_placeHolderLabel.lineBreakMode = NSLineBreakByCharWrapping;
_placeHolderLabel.numberOfLines = 0;
_placeHolderLabel.textColor = [UIColor colorWithHexString:LINECOLOR];
_placeHolderLabel.font = appFont(TEXT_FOUR_LEVELSIZE, NO);
}
return _placeHolderLabel;
}
- (UIButton*)switchEmojiBtn {
if (!_switchEmojiBtn) {
_switchEmojiBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_switchEmojiBtn.frame = CGRectMake(0, textViewTopPadding+textViewHeight, writeContentHeight-textViewTopPadding-textViewHeight, writeContentHeight-textViewTopPadding-textViewHeight);
CGFloat topPadding = (writeContentHeight-textViewTopPadding-textViewHeight)/2-25/2;
CGFloat leftPadding = 40/2-25/2;
[_switchEmojiBtn setImageEdgeInsets:UIEdgeInsetsMake(topPadding, leftPadding, topPadding, leftPadding)];
if ([[TPUserDefault instance].isNightMode intValue] == 1) {
[_switchEmojiBtn setImage:Image(@"detailPage/writeContentEmojiIcon_night.png") forState:UIControlStateNormal];
}else {
[_switchEmojiBtn setImage:Image(@"detailPage/writeContentEmojiIcon.png") forState:UIControlStateNormal];
}
/**
* bug:6023(夜间模式:输入框表情符号在夜间模式下不显示)
*/
[_switchEmojiBtn addTarget:self action:@selector(switchToEmoji:) forControlEvents:UIControlEventTouchUpInside];
_switchEmojiBtn.uxy_acceptEventInterval = 0.5;
}
return _switchEmojiBtn;
}
- (void)setPlaceHolderOfTextView {
NSString *placeHolder = @"";
if (!self.placeHoldText && isBlankString(self.placeHoldText)) {
userBO *user = setJsonDicToDataModel(self.commentBO.userInfo, [userBO class]);
if (self.type == commentType) {
placeHolder = [NSString stringWithFormat:@"评论(内容最多为%ld字)",(long)self.limitTextNum];
}else if (self.type == commentForUserType) {
placeHolder = [NSString stringWithFormat:@"回复%@(内容最多为%ld字)",user.sname,(long)self.limitTextNum];
}else if (self.type == askType) {
placeHolder = [NSString stringWithFormat:@"提问(内容最多为%ld字)",(long)self.limitTextNum];
}else if (self.type == askForUserType) {
placeHolder = [NSString stringWithFormat:@"回复%@(内容最多为%ld字)",user.sname,(long)self.limitTextNum];
}else if (self.type == aswerType) {//【需求】问答详情页(bug:4552) 回答改为回复
placeHolder = [NSString stringWithFormat:@"回复(内容最多为%ld字)",(long)self.limitTextNum];
}else if (self.type == interactionCommentType) {
placeHolder = @"每个问题背后都是一份翘首以待的热情,如何回复,你懂的!";
}else if (self.type == interactionAskType){
placeHolder = @"新颖、大胆、专业、有趣的好问题更有机会获得回复,开始提问吧!";
}
self.placeHolderLabel.text = placeHolder;
CGFloat height = [self.placeHolderLabel sizeThatFits:CGSizeMake(CGRectGetWidth(self.writeContentView.frame)-40, CGFLOAT_MAX)].height;
/**
* bug:5062(话题:提问的输入框中默认介绍不完整)
*/
self.placeHolderLabel.frame = CGRectMake(20, textViewTopPadding+8, CGRectGetWidth(self.writeContentView.frame)-40, height);
}else {
self.placeHolderLabel.text = self.placeHoldText;
}
}
- (void)setType:(commentAndAskType)coType {
_type = coType;
if (coType == commentType) {
self.limitTextNum = 800;
}else if (coType == commentForUserType) {
self.limitTextNum = 800;
}else if (coType == askType) {
self.limitTextNum = 140;
}else if (coType == askForUserType) {
self.limitTextNum = 800;
}else if (coType == aswerType) {
self.limitTextNum = 800;
}else if (coType == interactionAskType){
self.limitTextNum = 140;
}else if (coType == interactionCommentType) {
self.limitTextNum = 800;
}
}
#pragma mark - keyboard event
/**
* 触摸隐藏键盘
*
* @param sender 触摸手势
*/
- (void)clickBack:(UIButton*)sender {
[self closeKeyBoard];
if (_emojiContent) {
[UIView animateWithDuration:0.25 animations:^{
self.emojiContent.frame = CGRectMake(0,
CGRectGetHeight(self.view.bounds),
CGRectGetWidth(self.view.bounds),
emojiHeight);
self.writeContentView.frame = CGRectMake(0,
CGRectGetHeight(self.view.bounds)-writeContentHeight,
CGRectGetWidth(self.view.bounds),
writeContentHeight);
} completion:^(BOOL finished) {
[self.emojiContent removeFromSuperview];
self.emojiContent = nil;
}];
}else {
[self closeKeyBoard];
if (isBlankString(self.writeTextView.text)) {
if ([self.delegate respondsToSelector:@selector(dismissWriteContent:)]) {
[self.delegate dismissWriteContent:self.type];
}
}
}
if (isBlankString(self.writeTextView.text)) {
if ([self.delegate respondsToSelector:@selector(dismissWriteContent:)]) {
[self.delegate dismissWriteContent:self.type];
}
}
}
#pragma mark - keyBoard event
- (void)closeKeyBoard {
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}
//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
//获取键盘的高度
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
keyboardOffset = keyboardRect.size.height;
CGRect orginRect = CGRectMake(0,
CGRectGetHeight(self.view.bounds)-writeContentHeight,
CGRectGetWidth(self.view.bounds),
writeContentHeight);
orginRect.origin.y = orginRect.origin.y - keyboardOffset;
self.writeContentView.frame = orginRect;
if (_emojiContent) {
if ([[TPUserDefault instance].isNightMode intValue] == 1) {
[_switchEmojiBtn setImage:Image(@"detailPage/writeContentEmojiIcon_night.png") forState:UIControlStateNormal];
}else {
[_switchEmojiBtn setImage:Image(@"detailPage/writeContentEmojiIcon.png") forState:UIControlStateNormal];
}
[_switchEmojiBtn removeTarget:self action:@selector(switchToKeyboard:) forControlEvents:UIControlEventTouchUpInside];
[_switchEmojiBtn addTarget:self action:@selector(switchToEmoji:) forControlEvents:UIControlEventTouchUpInside];
_switchEmojiBtn.uxy_acceptEventInterval = 0.5;
self.emojiContent.frame = CGRectMake(0,
CGRectGetHeight(self.view.bounds),
CGRectGetWidth(self.view.bounds),
emojiHeight);
[self.emojiContent removeFromSuperview];
self.emojiContent = nil;
}else {
if ([[TPUserDefault instance].isNightMode intValue] == 1) {
[_switchEmojiBtn setImage:Image(@"detailPage/writeContentEmojiIcon_night.png") forState:UIControlStateNormal];
}else {
[_switchEmojiBtn setImage:Image(@"detailPage/writeContentEmojiIcon.png") forState:UIControlStateNormal];
}
[_switchEmojiBtn removeTarget:self action:@selector(switchToKeyboard:) forControlEvents:UIControlEventTouchUpInside];
[_switchEmojiBtn addTarget:self action:@selector(switchToEmoji:) forControlEvents:UIControlEventTouchUpInside];
_switchEmojiBtn.uxy_acceptEventInterval = 0.5;
}
/**
* bug:5932( 表情:当互动框可以输入表情时,多次点击“表情与文字”切换按钮后,无效)
*/
}
//当键盘出现或改变时调用
- (void)keyboardDidShow:(NSNotification *)aNotification
{
__block BOOL isHaveKeyboard = NO;
[_switchEmojiBtn.allTargets enumerateObjectsUsingBlock:^(id _Nonnull obj, BOOL * _Nonnull stop) {
NSArray *array = [_switchEmojiBtn actionsForTarget:obj forControlEvent:UIControlEventTouchUpInside];
[array enumerateObjectsUsingBlock:^(id _Nonnull item, NSUInteger index, BOOL * _Nonnull stop1) {
if ([item isMatchedByRegex:@"switchToKeyboard"]) {
isHaveKeyboard = YES;
*stop1 = YES;
*stop = YES;
}
}];
}];
if (isHaveKeyboard) {
if ([[TPUserDefault instance].isNightMode intValue] == 1) {
[_switchEmojiBtn setImage:Image(@"detailPage/writeContentEmojiIcon_night.png") forState:UIControlStateNormal];
}else {
[_switchEmojiBtn setImage:Image(@"detailPage/writeContentEmojiIcon.png") forState:UIControlStateNormal];
}
[_switchEmojiBtn removeTarget:self action:@selector(switchToKeyboard:) forControlEvents:UIControlEventTouchUpInside];
[_switchEmojiBtn addTarget:self action:@selector(switchToEmoji:) forControlEvents:UIControlEventTouchUpInside];
}
}
//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification
{
if (_emojiContent) {
self.writeContentView.frame = CGRectMake(0,
CGRectGetHeight(self.view.bounds)-writeContentHeight-emojiHeight,
CGRectGetWidth(self.view.bounds),
writeContentHeight);
}else {
self.writeContentView.frame = CGRectMake(0,
CGRectGetHeight(self.view.bounds)-writeContentHeight,
CGRectGetWidth(self.view.bounds),
writeContentHeight);
}
}
#pragma mark - text view delegate
- (void)textViewDidChange:(UITextView *)textView {
if (isBlankString(textView.text) || !textView.textStorage) {
self.placeHolderLabel.hidden = NO;
confirmBtn.enabled = NO;
}else {
self.placeHolderLabel.hidden = YES;
confirmBtn.enabled = YES;
}
if (isJustIos8) {
CGRect line = [textView caretRectForPosition:
textView.selectedTextRange.start];
CGFloat overflow = line.origin.y + line.size.height
- ( textView.contentOffset.y + textView.bounds.size.height
- textView.contentInset.bottom - textView.contentInset.top );
if ( overflow > 0 ) {
// We are at the bottom of the visible text and introduced a line feed, scroll down (iOS 7 does not do it)
// Scroll caret to visible area
CGPoint offset = textView.contentOffset;
offset.y += overflow + 7; // leave 7 pixels margin
// Cannot animate with setContentOffset:animated: or caret will not appear
[textView setContentOffset:offset animated:NO];
}
/**
* bug:6186(【适配性】输入框:ios8系统在输入框的最后一行,输入多点的拼音(能够显示到下一行),在点击对应的汉字输入上去,输入框显示未自动换行)
*/
}
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([textView isFirstResponder]) {
if ([[[textView textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textView textInputMode] primaryLanguage]) {
return NO;
}
}
return YES;
}
#pragma mark - button handler
- (void)confirmEvent:(UIButton*)btn {
[self closeKeyBoard];
self.commentContent = [self getPlainString:self.writeTextView.textStorage];
if ([self.commentContent isMatchedByRegex:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]"]) {
self.commentContent = disable_emoji(self.commentContent);
}
if (isBlankString(self.commentContent)) {
ShowTextMessageInPop((@"内容不能为空"), self.writeTextView);
return;
}
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
if (self.commentOT && !isBlankString(self.commentOT)) {
[dic setValue:self.commentOT forKey:@"ot"];
}else {
[dic setValue:@"1" forKey:@"ot"];
}
[dic setValue:self.nodeId forKey:@"c"];
// self.commentContent = [self.commentContent stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
// self.commentContent = [self.commentContent stringByReplacingOccurrencesOfString:@">" withString:@">"];
self.commentContent = disable_emoji(self.commentContent);
[dic setValue:self.commentContent forKey:@"content"];
if (self.type == commentType) {
[dic setValue:@"1" forKey:@"commentType"];
if(![dic[@"ot"] isEqualToString:@"3"]){
}
}else if (self.type == commentForUserType) {
if(![dic[@"ot"] isEqualToString:@"3"]){
}
[dic setValue:@"1" forKey:@"commentType"];
if (self.commentBO) {
[dic setValue:self.commentBO.commentId forKey:@"parentId"];
}
}else if (self.type == askType) {
if(![dic[@"ot"] isEqualToString:@"3"]){
}
[dic setValue:@"2" forKey:@"commentType"];
}else if (self.type == askForUserType) {
if(![dic[@"ot"] isEqualToString:@"3"]){
}
[dic setValue:@"3" forKey:@"commentType"];
if (self.commentBO) {
if (!isBlankString(self.commentBO.parentId)) {
[dic setValue:self.commentBO.parentId forKey:@"parentId"];
[dic setValue:self.commentBO.commentId forKey:@"quoteId"];
}else if(self.commentBO.parent){
[dic setValue:self.commentBO.parent[@"commentId"] forKey:@"parentId"];
[dic setValue:self.commentBO.commentId forKey:@"quoteId"];
}else if(self.commentBO.parentInfo){
[dic setValue:self.commentBO.parentInfo[@"commentId"] forKey:@"parentId"];
[dic setValue:self.commentBO.commentId forKey:@"quoteId"];
}
}
}else if (self.type == aswerType) {
if(![dic[@"ot"] isEqualToString:@"3"]){
}else {
}
[dic setValue:@"3" forKey:@"commentType"];
if (self.commentBO) {
if (self.commentBO.parent[@"commentId"]) {
[dic setValue:self.commentBO.parent[@"commentId"] forKey:@"parentId"];
}else{
[dic setValue:self.commentBO.commentId forKey:@"parentId"];
}
// [dic setValue:self.commentBO.commentId forKey:@"quoteId"];
// [dic setValue:self.commentBO.commentId forKey:@"parentId"];
}
}else if (self.type == interactionAskType){
if(![dic[@"ot"] isEqualToString:@"3"]){
}else {
}
[dic setValue:@"2" forKey:@"commentType"];
}else if (self.type == interactionCommentType){
if(![dic[@"ot"] isEqualToString:@"3"]){
}
[dic setValue:@"3" forKey:@"commentType"];
if (self.commentBO) {
[dic setValue:self.commentBO.commentId forKey:@"parentId"];
}
}
[Remote doJsonAction:0 requestUrl:commentURL parameter:dic delegate:self];
}
- (void)cancel:(UIButton*)btn {
self.commentContent = [self getPlainString:self.writeTextView.textStorage];
if (!isBlankString(self.commentContent)) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"是否放弃已输入的内容?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
[alert show];
return;
}
[self dismissContent];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != 0) {
[self dismissContent];
}
}
- (void)dismissContent {
[self closeKeyBoard];
if ([self.delegate respondsToSelector:@selector(dismissWriteContent:)]) {
[self.delegate dismissWriteContent:self.type];
}
}
- (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responsData {
[self closeKeyBoard];
NSString *message =responsData[@"resultMsg"];
// ShowTextMessage(message);
if ([message isMatchedByRegex:@"成功"]) {
if (self.isPush) {
if ([self.delegate respondsToSelector:@selector(commentSuccess:)]) {
ShowTextMessageWithDelay(message, 3);//话题详情页,提交一个问题,弹出的tips会闪一下(bug:4741)
[self.delegate commentSuccess:self.type];
}
}else {
if ([self.delegate respondsToSelector:@selector(commentSuccess:)]) {
ShowTextMessageWithDelay(message, 3);//话题详情页,提交一个问题,弹出的tips会闪一下(bug:4741)
[self.delegate commentSuccess:self.type];
}
}
}else {
ShowTextMessage(message);//话题详情页,提交一个问题,弹出的tips会闪一下(bug:4741)
dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, 1ull*NSEC_PER_SEC);
dispatch_after(delayTime, dispatch_get_main_queue(), ^{
if ([self.delegate respondsToSelector:@selector(commentSuccess:)]) {
[self.delegate commentSuccess:self.type];
}
});
}
}
- (void)remoteResponsFailed:(int)actionTag withMessage:(NSString *)message resultCode:(NSString *)code{
[self closeKeyBoard];
ShowTextMessage(message);
}
- (void)dealloc {
//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
if (_emojiContent) {
_emojiContent.delgate = nil;
_emojiContent = nil;
}
}
#pragma mark - emoji
- (emojiView*)emojiContent {
if (!_emojiContent) {
_emojiContent = [emojiView new];
_emojiContent.frame = CGRectMake(0,
CGRectGetHeight(self.view.bounds),
CGRectGetWidth(self.view.bounds),
emojiHeight);
_emojiContent.delgate = self;
}
return _emojiContent;
}
- (void)switchToEmoji:(UIButton*)btn {
[self.view addSubview:self.emojiContent];
[self closeKeyBoard];
[UIView animateWithDuration:0.25 animations:^{
CGRect orginRect = CGRectMake(0,
CGRectGetHeight(self.view.bounds)-writeContentHeight,
CGRectGetWidth(self.view.bounds),
writeContentHeight);
orginRect.origin.y = orginRect.origin.y - emojiHeight;
self.writeContentView.frame = orginRect;
/**
* bug:5935(评论,回复、提问等输入框:当输入框的键盘隐藏时,点击表情图标异常)
*/
self.emojiContent.frame = CGRectMake(0,
CGRectGetHeight(self.view.bounds)-emojiHeight,
CGRectGetWidth(self.view.bounds),
emojiHeight);
}completion:^(BOOL finished) {
if ([[TPUserDefault instance].isNightMode intValue] == 1) {
[_switchEmojiBtn setImage:Image(@"detailPage/writeContentKeyboardIcon_night.png") forState:UIControlStateNormal];
}else {
[_switchEmojiBtn setImage:Image(@"detailPage/writeContentKeyboardIcon.png") forState:UIControlStateNormal];
}
[_switchEmojiBtn removeTarget:self action:@selector(switchToEmoji:) forControlEvents:UIControlEventTouchUpInside];
[_switchEmojiBtn addTarget:self action:@selector(switchToKeyboard:) forControlEvents:UIControlEventTouchUpInside];
_switchEmojiBtn.uxy_acceptEventInterval = 0.5;
}];
// [UIView animateWithDuration:0.25 animations:^{
// self.writeContentView.frame = CGRectMake(0,
// CGRectGetHeight(self.view.bounds)-writeContentHeight-emojiHeight,
// CGRectGetWidth(self.view.bounds),
// writeContentHeight);
// } completion:^(BOOL finished) {
// [self.view addSubview:self.emojiContent];
//
// [UIView animateWithDuration:0.25 animations:^{
// self.emojiContent.frame = CGRectMake(0,
// CGRectGetHeight(self.view.bounds)-emojiHeight,
// CGRectGetWidth(self.view.bounds),
// emojiHeight);
// }completion:^(BOOL finished) {
// [_switchEmojiBtn setImage:Image(@"detailPage/writeContentKeyboardIcon.png") forState:UIControlStateNormal];
// [_switchEmojiBtn removeTarget:self action:@selector(switchToEmoji:) forControlEvents:UIControlEventTouchUpInside];
// [_switchEmojiBtn addTarget:self action:@selector(switchToKeyboard:) forControlEvents:UIControlEventTouchUpInside];
// }];
// }];
}
- (void)switchToKeyboard:(UIButton*)btn {
[self.writeTextView becomeFirstResponder];
}
#pragma mark - emoji delegate
- (void)selectEmoji:(NSString *)emojiString {
if ([emojiString isEqualToString:@"delete"]) {
NSRange range = self.writeTextView.selectedRange;
if (range.location == 0) {
return;
}
range.location = range.location-1;
range.length = 1;
[self.writeTextView.textStorage deleteCharactersInRange:range];
if (self.writeTextView.textStorage.length == 0) {
self.placeHolderLabel.hidden = NO;
confirmBtn.enabled = NO;
}
}else {
emojiAttachment *attachment = [[emojiAttachment alloc] init];
UIImage *image = Image([NSString stringWithFormat:@"face/%@.png",emojiString]);
attachment.image = image;
attachment.emojiTag = emojiString;
attachment.bounds = CGRectMake(0, 0, 16, 16);
[self.writeTextView.textStorage insertAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]
atIndex:self.writeTextView.selectedRange.location];
NSRange range = self.writeTextView.selectedRange;
range.location = range.location+1;
self.writeTextView.selectedRange = range;
self.placeHolderLabel.hidden = YES;
confirmBtn.enabled = YES;
[self resetTextStyle];
[self.writeTextView scrollRangeToVisible:NSMakeRange(self.writeTextView.selectedRange.location, 1)];
/**
* bug:6164(评论、提问、回答表情输入:在评论、提问、回答的输入框中一直输入表情待输入框满后不会自动换行显示)
*/
}
}
#pragma mark - string and attributeString method
- (void)stringToAttributeString {
textViewTrueString = [[NSMutableAttributedString alloc]initWithString:sourceString];
}
- (void)attributeStringToString {
sourceString = textViewTrueString.string;
}
- (NSMutableString *)getPlainString:(NSAttributedString*)attributeString{
NSMutableString *plainString = [NSMutableString stringWithString:attributeString.string];
__block NSUInteger base = 0;
[attributeString enumerateAttribute:NSAttachmentAttributeName inRange:NSMakeRange(0, attributeString.length)
options:0
usingBlock:^(id value, NSRange range, BOOL *stop) {
if (value && [value isKindOfClass:[emojiAttachment class]]) {
[plainString replaceCharactersInRange:NSMakeRange(range.location + base, range.length)
withString:[NSString stringWithFormat:@"[%@]",((emojiAttachment *) value).emojiTag]];
base += ((emojiAttachment *) value).emojiTag.length + 2-1;
}
}];
return plainString;
}
- (void)resetTextStyle {
//After changing text selection, should reset style.
NSRange wholeRange = NSMakeRange(0, self.writeTextView.textStorage.length);
[self.writeTextView.textStorage removeAttribute:NSFontAttributeName range:wholeRange];
[self.writeTextView.textStorage addAttribute:NSFontAttributeName value:appFont(TEXT_FOUR_LEVELSIZE, NO) range:wholeRange];
[self.writeTextView.textStorage addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:TextBlack] range:wholeRange];
/**
* bug:6159(评论、回复、提问等的输入框:在评论、回复、提问...的输入框中先输入表情在输入汉字,显示异常)
*/
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
|