|
//
// creatTopicController.m
// ThePaperHD
//
// Created by scar1900 on 15/4/28.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "creatTopicController.h"
#import "AsyncImageView.h"
#import "TPCustomButton.h"
#import "AsyncImageView.h"
#import "UIImage-Extensions.h"
#import "MLImageCrop.h"
#define CATEBTNTAG 2500
#define SELFINTROTAG 2520
#define TOPICFIELDTAG 2521
#define DESCRIBETEXTVIEWTAG 2522
#define LEAVEMESSAGETEXTVIEWTAG 2523
@interface cateBtn : UIButton
- (void)setCateBtnInterface:(NSString*)title;
@end
@implementation cateBtn
- (id)init {
self = [super init];
if (self) {
self.clipsToBounds = YES;
}
return self;
}
- (void)setCateBtnInterface:(NSString*)title{
[self setTitle:title forState:UIControlStateNormal];
[self setTitleColor:[UIColor colorWithHexString:TextGray] forState:UIControlStateNormal];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[self setBackgroundImage:imageWithUIColor([UIColor whiteColor])
forState:UIControlStateNormal];
[self setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:BLUECOLOR])
forState:UIControlStateHighlighted];
[self setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:BLUECOLOR])
forState:UIControlStateSelected];
self.titleLabel.font = appFont(15, NO);
self.layer.cornerRadius = 2;
self.layer.borderColor = [UIColor colorWithHexString:TextGray].CGColor;
self.layer.borderWidth = 1;
}
- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
if (selected) {
self.layer.borderColor = [UIColor clearColor].CGColor;
self.layer.borderWidth = 0;
}else {
self.layer.borderColor = [UIColor colorWithHexString:TextGray].CGColor;
self.layer.borderWidth = 1;
}
}
@end
@interface creatTopicController ()<UIScrollViewDelegate,UIActionSheetDelegate,UIPopoverControllerDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate,MLImageCropDelegate,UITextFieldDelegate,UITextViewDelegate,AsyncImageViewDelegate> {
CGFloat topPadding;
CGFloat cateSelectHeight;
CGFloat lineNum;
NSInteger pickerIndex;
CGFloat responderHeight;
CGFloat keyboardOffset;
}
@property(nonatomic, strong)TopicInfoBO *topicInfo;
@property(nonatomic, strong)UIScrollView *backScrollView;
@property(nonatomic, strong)AsyncImageView *headImageView;
@property(nonatomic, strong)UIImageView *vImg;
@property(nonatomic, strong)UILabel *userNameLabel;
@property(nonatomic, strong)UIImageView *sexImg;
@property(nonatomic, strong)NSMutableArray *cateList;
@property(nonatomic, strong)UIView *categorySelectView;
@property(nonatomic, strong)NSMutableArray *cateBtnList;
@property(nonatomic, strong)UIView *topicTitleTextView; //话题介绍板块
@property(nonatomic, strong)UITextField *selfIntroductionField; //自我介绍
//@property(nonatomic, strong)UITextField *topicField1; //话题简介1
@property(nonatomic, strong)UITextField *topicField2; //话题简介2
@property(nonatomic, strong)UITextView *topicDescribeView;
@property(nonatomic, strong)UILabel *topicDescribePlaceHolder;
@property(nonatomic, strong)UITextView *leaveMessageView;
@property(nonatomic, strong)UILabel *leaveMessagePlaceHolder;
@property(nonatomic, strong)UIButton *imageSelectBtn; //选择封面按钮
@property(nonatomic, strong)UILabel *imageSelectLabel;
@property(nonatomic, strong)UIImage *topicBackImg;
@property(nonatomic, strong)AsyncImageView *topicBackImageView;
@property(nonatomic, strong)UIButton *deleteImageBtn;
@property(nonatomic, strong)TPCustomButton *confirmBtn;
@property(nonatomic, strong)UIActionSheet *actionSheet;
@property(nonatomic, strong)UIPopoverController *popController;
@property(nonatomic, strong)UILabel *actionLabel;
@end
@implementation creatTopicController
@synthesize cateList,cateBtnList;
@synthesize popController,actionSheet;
@synthesize topicInfo = _topicInfo;
@synthesize topicBO;
@synthesize tag = _tag;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[MobClick event:@"42"];
if (!self.topicInfo) {
self.topicInfo = TopicInfoBO.new;
}
if (isIOS7) {
topPadding = 64;
}else topPadding = 44;
[self leftIconButton:@"Button/backBarBtn.png" highlited:nil selector:@selector(backButtonHandler:)];
[self.view addSubview:self.backScrollView];
UITapGestureRecognizer *closeGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapToCloseKeyboard:)];
[self.backScrollView addGestureRecognizer:closeGes];
[self setUserInfo];
//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
[self askCategoryList];
//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
-(void)setTag:(NSString *)tagIndex{
_tag = tagIndex;
}
- (void)setTopicInfo:(TopicInfoBO *)topic {
_topicInfo = topic;
[self.cateList enumerateObjectsUsingBlock:^(categoryBO* obj, NSUInteger idx, BOOL *stop) {
if ([obj.category isEqualToString:topic.category]) {
UIButton *btn = self.cateBtnList[idx-1];
btn.selected = YES;
}
}];
self.topicDescribeView.text = topic.desc;
if (isBlankString(topic.desc)) {
self.topicDescribePlaceHolder.hidden = NO;
}else self.topicDescribePlaceHolder.hidden = YES;
self.leaveMessageView.text = topic.message;
if (isBlankString(topic.message)) {
self.leaveMessagePlaceHolder.hidden = NO;
}else self.leaveMessagePlaceHolder.hidden = YES;
NSArray *array = [topic.title componentsSeparatedByString:@","];
if (array.count > 0) {
self.selfIntroductionField.text = array[0];
__block NSString *titleStr = @"";
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if (idx != 0 && idx != array.count-1) {
titleStr = [titleStr stringByAppendingString:obj];
}
}];
self.topicField2.text = titleStr;
}
if (!isBlankString(topic.pic)) {
self.topicBackImageView.delegate = self;
[self.backScrollView addSubview:self.topicBackImageView];
self.topicBackImageView.imageUrl = topic.pic;
[self.backScrollView addSubview:self.deleteImageBtn];
[self viewWillLayoutSubviews];
}
}
- (void)askCategoryList {
[Remote doJsonAction:0 requestUrl:getTopicCategorysURL parameter:nil delegate:self];
}
- (void)setUserInfo {
self.headImageView.imageUrl = [TPUserDefault instance].userBO.pic;
if ([[TPUserDefault instance].userBO.isAuth intValue] == 1) {
self.vImg.hidden = NO;
}else {
self.vImg.hidden = YES;
}
self.userNameLabel.text = [TPUserDefault instance].userBO.sname;
if ([[TPUserDefault instance].userBO.sex integerValue] ==0) {
self.sexImg.image = Image(@"topic/maleSex.png");
}else{
self.sexImg.image = Image(@"topic/femaleSex.png");
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.title = @"创建话题";
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
if (self.view.bounds.size.width > self.view.bounds.size.height) {
self.view.frame =CGRectMake(0, 0, self.view.bounds.size.height, self.view.bounds.size.width);
}
}else {
if (self.view.bounds.size.width < self.view.bounds.size.height) {
self.view.frame =CGRectMake(0, 0, self.view.bounds.size.height, self.view.bounds.size.width);
}
}
self.navigationController.navigationBarHidden = NO;
if (isIOS7) {
self.automaticallyAdjustsScrollViewInsets = NO;
}
self.navigationController.navigationBar.translucent = YES;
//【需求】视觉优化(bug:4569)
NSString *barColor;
for (int i = 0 ; i < [TPUserDefault instance].channldataList.count; i++) {
if ([[TPUserDefault instance].channldataList[i][@"nodeId"] isEqualToString:@"-3"]) {
barColor =[TPUserDefault instance].channldataList[i][@"color"];
}
}
if (isIOS7) {
self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:barColor];
}else {
self.navigationController.navigationBar.tintColor = [UIColor colorWithHexString:barColor];
}
// if (isIOS7) {
// self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"0xa89271"];
// }else {
// self.navigationController.navigationBar.tintColor = [UIColor colorWithHexString:@"0xa89271"];
// }
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
CGFloat contentHight = 0;
self.headImageView.frame = CGRectMake(CGRectGetWidth(self.backScrollView.bounds)/2-30, 20+topPadding, 60, 60);
contentHight = contentHight + 20+topPadding+60;
self.vImg.frame = CGRectMake(CGRectGetMaxX(self.headImageView.frame)-16,
CGRectGetMaxY(self.headImageView.frame)-20,
20,
20);
CGFloat userNameWidth = widthForString([TPUserDefault instance].userBO.sname,
self.userNameLabel.font, 19, self.userNameLabel.lineBreakMode);
self.userNameLabel.frame = CGRectMake(CGRectGetWidth(self.view.bounds)/2-userNameWidth/2,
CGRectGetMaxY(self.headImageView.frame)+12,
userNameWidth,
19);
contentHight = contentHight + 12+19;
self.sexImg.frame = CGRectMake(CGRectGetMaxX(self.userNameLabel.frame)+5, CGRectGetMinY(self.userNameLabel.frame)+5, 12, 12);
CGFloat cateHeight = 32 + (25+8)*lineNum;
if (lineNum > 0) {
self.categorySelectView.frame = CGRectMake(CGRectGetWidth(self.backScrollView.bounds)/2-740/2,
CGRectGetMaxY(self.userNameLabel.frame)+25,
740,
cateHeight);
contentHight = contentHight + 25+cateHeight;
}
self.topicTitleTextView.frame = CGRectMake(CGRectGetWidth(self.backScrollView.bounds)/2-740/2,
CGRectGetMaxY(self.userNameLabel.frame)+cateHeight,
740,
105);
contentHight = contentHight + 105;
self.topicDescribeView.frame = CGRectMake(CGRectGetMinX(self.topicTitleTextView.frame),
CGRectGetMaxY(self.topicTitleTextView.frame)+15,
740,
206/2);
contentHight = contentHight + 103+15;
self.leaveMessageView.frame = CGRectMake(CGRectGetMinX(self.topicTitleTextView.frame),
CGRectGetMaxY(self.topicDescribeView.frame)+20,
740,
180/2);
contentHight = contentHight + 103+20;
self.imageSelectBtn.frame = CGRectMake(CGRectGetMinX(self.topicTitleTextView.frame),
CGRectGetMaxY(self.leaveMessageView.frame)+20,
60,
60);
contentHight = contentHight + 80;
self.imageSelectLabel.frame = CGRectMake(CGRectGetMaxX(self.imageSelectBtn.frame)+18,
CGRectGetMinY(self.imageSelectBtn.frame),
150,
CGRectGetHeight(self.imageSelectBtn.frame));
if (_topicBackImageView) {
self.topicBackImageView.frame = CGRectMake(CGRectGetWidth(self.view.bounds)/2-300,
CGRectGetMaxY(self.imageSelectBtn.frame)+25,
600,
450);
contentHight = contentHight+25+450;
self.actionLabel.frame = CGRectMake(CGRectGetWidth(self.view.bounds)/2-200,
CGRectGetMaxY(self.topicBackImageView.frame)+20,
400,
15);
self.confirmBtn.frame = CGRectMake(CGRectGetWidth(self.view.bounds)/2-200,
CGRectGetMaxY(self.actionLabel.frame)+15,
400,
40);
contentHight = contentHight + 25 + 40 + 30;
self.deleteImageBtn.frame = CGRectMake(CGRectGetMaxX(self.topicBackImageView.frame)-41/4,
CGRectGetMinY(self.topicBackImageView.frame)-41/4,
41/2,
41/2);
}else {
self.actionLabel.frame = CGRectMake(CGRectGetWidth(self.view.bounds)/2-200,
CGRectGetMaxY(self.imageSelectBtn.frame)+20,
400,
15);
self.confirmBtn.frame = CGRectMake(CGRectGetWidth(self.view.bounds)/2-200,
CGRectGetMaxY(self.actionLabel.frame)+15,
400,
40);
contentHight = contentHight + 25 + 40 + 30;
}
[self.backScrollView setContentSize:CGSizeMake(0, contentHight)];
if (self.popController) {
[self showPopOver];
}
}
- (UIScrollView*)backScrollView {
if (!_backScrollView) {
_backScrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
_backScrollView.directionalLockEnabled = YES;
_backScrollView.alwaysBounceVertical = YES;
_backScrollView.showsVerticalScrollIndicator = YES;
_backScrollView.autoresizingMask = AutoresizingFull;
_backScrollView.backgroundColor = [UIColor clearColor];
_backScrollView.delegate = self;
}
return _backScrollView;
}
- (AsyncImageView*)headImageView {
if (!_headImageView) {
_headImageView = [[AsyncImageView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.bounds)/2-30, 20, 60, 60)];
_headImageView.layer.cornerRadius = 30;
_headImageView.clipsToBounds = YES;
_headImageView.isHaveWaterPrint = NO;
}
return _headImageView;
}
-(UIImageView *)vImg{
if (!_vImg) {
_vImg = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.headImageView.frame)-16,
CGRectGetMaxY(self.headImageView.frame)-20,
20,
20)];
_vImg.image = Image(@"topic/vipIcon.png");
}
return _vImg;
}
- (UILabel*)userNameLabel {
if (!_userNameLabel) {
_userNameLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_userNameLabel.font = appFont(17, NO);
_userNameLabel.textColor = [UIColor colorWithHexString:TextBlack];
_userNameLabel.textAlignment = NSTextAlignmentLeft;
_userNameLabel.backgroundColor = [UIColor clearColor];
}
return _userNameLabel;
}
-(UIImageView *)sexImg{
if (!_sexImg) {
_sexImg = [[UIImageView alloc] initWithFrame:CGRectZero];
}
return _sexImg;
}
- (UIView*)categorySelectView {
if (!_categorySelectView) {
_categorySelectView = [[UIView alloc]initWithFrame:CGRectZero];
_categorySelectView.backgroundColor = [UIColor clearColor];
UILabel *cateLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 195/2, 25)];
cateLabel.text = @"请选择分类";
cateLabel.font = appFont(17, NO);
cateLabel.backgroundColor = [UIColor clearColor];
cateLabel.textColor = [UIColor colorWithHexString:TextBlack];
[_categorySelectView addSubview:cateLabel];
CGFloat num = self.cateList.count/7;
CGFloat remind = self.cateList.count%7;
if (remind == 0) {
lineNum = (int)num;
}else {
lineNum = (int)num + 1;
}
self.cateBtnList = [NSMutableArray array];
[self.cateList enumerateObjectsUsingBlock:^(categoryBO* obj, NSUInteger idx, BOOL *stop) {
cateBtn *cateButton = [[cateBtn alloc]init];
NSInteger index = idx-1;
if (obj.category && !isBlankString(obj.category)) {
[cateButton setCateBtnInterface:obj.name];
cateButton.tag = CATEBTNTAG + idx;
[cateButton addTarget:self action:@selector(selectOneCate:) forControlEvents:UIControlEventTouchUpInside];
CGFloat lineF = index/6;
int line = (int)lineF;
CGFloat row = 0;
if (line == 0) {
row = index;
}else {
row = index - 6;
}
cateButton.frame = CGRectMake(230/2+(75+65/2)*row, line*(25+8), 75, 25);
[_categorySelectView addSubview:cateButton];
[self.cateBtnList addObject:cateButton];
}
}];
}
return _categorySelectView;
}
- (UIView *)topicTitleTextView {
if (!_topicTitleTextView) {
_topicTitleTextView = [[UIView alloc]initWithFrame:CGRectZero];
_topicTitleTextView.backgroundColor = [UIColor clearColor];
self.selfIntroductionField.frame = CGRectMake(0, 0, 800/2, 29);
UIView *introductionLine = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.selfIntroductionField.frame)-1, CGRectGetWidth(self.selfIntroductionField.frame), 1)];
introductionLine.backgroundColor = [UIColor colorWithHexString:TextGray];
[self.selfIntroductionField addSubview:introductionLine];
[_topicTitleTextView addSubview:self.selfIntroductionField];
UILabel *paddingLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.selfIntroductionField.frame),
CGRectGetHeight(self.selfIntroductionField.frame)-20,
10,
20)];
paddingLabel.font = appFont(17, NO);
paddingLabel.textColor = [UIColor colorWithHexString:TextBlack];
paddingLabel.textAlignment = NSTextAlignmentCenter;
paddingLabel.backgroundColor = [UIColor clearColor];
paddingLabel.text = @",";
[_topicTitleTextView addSubview:paddingLabel];
// self.topicField1.frame = CGRectMake(CGRectGetMaxX(paddingLabel.frame),
// CGRectGetMinY(self.selfIntroductionField.frame), 700/2, 29);
// UIView *topicFieldLine1 = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.topicField1.frame)-1, CGRectGetWidth(self.topicField1.frame), 1)];
// topicFieldLine1.backgroundColor = [UIColor colorWithHexString:TextGray];
// [self.topicField1 addSubview:topicFieldLine1];
// [_topicTitleTextView addSubview:self.topicField1];
self.topicField2.frame = CGRectMake(0, CGRectGetMaxY(self.selfIntroductionField.frame)+35-29, 1320/2, 29);
UIView *topicFieldLine2 = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.topicField2.frame)-1, CGRectGetWidth(self.topicField2.frame), 1)];
topicFieldLine2.backgroundColor = [UIColor colorWithHexString:TextGray];
[self.topicField2 addSubview:topicFieldLine2];
[_topicTitleTextView addSubview:self.topicField2];
UILabel *lastLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.topicField2.frame),
CGRectGetMinY(self.topicField2.frame)+CGRectGetHeight(self.topicField2.frame)-20,
85,
20)];
lastLabel.font = appFont(17, NO);
lastLabel.textColor = [UIColor colorWithHexString:TextBlack];
lastLabel.textAlignment = NSTextAlignmentLeft;
lastLabel.backgroundColor = [UIColor clearColor];
lastLabel.text = @",问我吧!";
[_topicTitleTextView addSubview:lastLabel];
UILabel *exLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,
CGRectGetMaxY(self.topicField2.frame)+15,
740,
25)];
exLabel.textColor = [UIColor colorWithHexString:TextGray];
exLabel.font = appFont(17, NO);
exLabel.backgroundColor = [UIColor clearColor];
exLabel.textAlignment = NSTextAlignmentLeft;
exLabel.numberOfLines = 0;
exLabel.text = @"例如:我是澎湃记者李磊,关于暗访的问题,问我吧!(不超过40个字)";
[_topicTitleTextView addSubview:exLabel];
}
return _topicTitleTextView;
}
- (UITextField*)selfIntroductionField {
if (!_selfIntroductionField) {
_selfIntroductionField = [[UITextField alloc]initWithFrame:CGRectZero];
_selfIntroductionField.font = appFont(17, NO);
_selfIntroductionField.textColor = [UIColor colorWithHexString:TextBlack];
_selfIntroductionField.backgroundColor = [UIColor clearColor];
_selfIntroductionField.textAlignment = NSTextAlignmentLeft;
_selfIntroductionField.text = @"我是";
_selfIntroductionField.delegate = self;
_selfIntroductionField.tag = SELFINTROTAG;
_selfIntroductionField.keyboardAppearance = UIKeyboardAppearanceDefault;
if ([[TPUserDefault instance].isNightMode intValue] > 0) {
_selfIntroductionField.keyboardAppearance = UIKeyboardAppearanceDark;
}
// [_selfIntroductionField addTarget:self action:@selector(selfIntroDidChange:) forControlEvents:UIControlEventEditingChanged];
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(selfIntroDidChange:)
name:UITextFieldTextDidChangeNotification
object:_selfIntroductionField];
}
return _selfIntroductionField;
}
//- (UITextField*)topicField1 {
// if (!_topicField1) {
// _topicField1 = [[UITextField alloc]initWithFrame:CGRectZero];
// _topicField1.font = appFont(17, NO);
// _topicField1.textColor = [UIColor colorWithHexString:TextBlack];
// _topicField1.backgroundColor = [UIColor clearColor];
// _topicField1.textAlignment = NSTextAlignmentLeft;
// _topicField1.delegate = self;
// }
// return _topicField1;
//}
- (UITextField*)topicField2 {
if (!_topicField2) {
_topicField2 = [[UITextField alloc]initWithFrame:CGRectZero];
_topicField2.font = appFont(17, NO);
_topicField2.textColor = [UIColor colorWithHexString:TextBlack];
_topicField2.backgroundColor = [UIColor clearColor];
_topicField2.textAlignment = NSTextAlignmentLeft;
NSAttributedString* atrString = [[NSAttributedString alloc] initWithString:@"你想要说的话题" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:LINECOLOR]}];
_topicField2.attributedPlaceholder = atrString;
_topicField2.delegate = self;
_topicField2.tag = TOPICFIELDTAG;
_topicField2.keyboardAppearance = UIKeyboardAppearanceDefault;
if ([[TPUserDefault instance].isNightMode intValue] > 0) {
_topicField2.keyboardAppearance = UIKeyboardAppearanceDark;
}
// [_topicField2 addTarget:self action:@selector(topicFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(topicFieldDidChange:)
name:UITextFieldTextDidChangeNotification
object:_topicField2];
}
return _topicField2;
}
- (UITextView *)topicDescribeView {
if (!_topicDescribeView) {
_topicDescribeView = [[UITextView alloc]initWithFrame:CGRectZero];
_topicDescribeView.backgroundColor = [UIColor colorWithHexString:@"0xeff0f1"];
_topicDescribeView.scrollEnabled = YES;
_topicDescribeView.delegate =self;
_topicDescribeView.textAlignment = NSTextAlignmentLeft;
_topicDescribeView.textColor = [UIColor colorWithHexString:TextBlack];
_topicDescribeView.font = appFont(14, NO);
_topicDescribeView.layer.borderColor = [UIColor colorWithHexString:LINECOLOR].CGColor;
_topicDescribeView.layer.borderWidth = 1;
_topicDescribeView.tag = DESCRIBETEXTVIEWTAG;
_topicDescribeView.layer.cornerRadius = 2;
_topicDescribeView.keyboardAppearance = UIKeyboardAppearanceDefault;
if ([[TPUserDefault instance].isNightMode intValue] > 0) {
_topicDescribeView.keyboardAppearance = UIKeyboardAppearanceDark;
}
[_topicDescribeView addSubview:self.topicDescribePlaceHolder];
self.topicDescribePlaceHolder.frame = CGRectMake(15,
25/2,
700,
35/2);
}
return _topicDescribeView;
}
- (UILabel*)topicDescribePlaceHolder {
if (!_topicDescribePlaceHolder) {
_topicDescribePlaceHolder = [[UILabel alloc]initWithFrame:CGRectZero];
_topicDescribePlaceHolder.text = @"请详细描述你的话题(内容为4-800个字)";
_topicDescribePlaceHolder.textColor = [UIColor colorWithHexString:TextGray];
_topicDescribePlaceHolder.font = appFont(14, NO);
_topicDescribePlaceHolder.textAlignment = NSTextAlignmentLeft;
_topicDescribePlaceHolder.hidden = NO;
_topicDescribePlaceHolder.backgroundColor = [UIColor clearColor];
}
return _topicDescribePlaceHolder;
}
- (UITextView *)leaveMessageView {
if (!_leaveMessageView) {
_leaveMessageView = [[UITextView alloc]initWithFrame:CGRectZero];
_leaveMessageView.backgroundColor = [UIColor colorWithHexString:@"0xeff0f1"];
_leaveMessageView.scrollEnabled = YES;
_leaveMessageView.delegate =self;
_leaveMessageView.textAlignment = NSTextAlignmentLeft;
_leaveMessageView.textColor = [UIColor colorWithHexString:TextBlack];
_leaveMessageView.font = appFont(14, NO);
_leaveMessageView.layer.borderColor = [UIColor colorWithHexString:LINECOLOR].CGColor;
_leaveMessageView.layer.borderWidth = 1;
_leaveMessageView.layer.cornerRadius = 2;
_leaveMessageView.tag = LEAVEMESSAGETEXTVIEWTAG;
_leaveMessageView.keyboardAppearance = UIKeyboardAppearanceDefault;
if ([[TPUserDefault instance].isNightMode intValue] > 0) {
_leaveMessageView.keyboardAppearance = UIKeyboardAppearanceDark;
}
[_leaveMessageView addSubview:self.leaveMessagePlaceHolder];
self.leaveMessagePlaceHolder.frame = CGRectMake(15,
25/2,
700,
35);
}
return _leaveMessageView;
}
- (UILabel*)leaveMessagePlaceHolder {
if (!_leaveMessagePlaceHolder) {
_leaveMessagePlaceHolder = [[UILabel alloc]initWithFrame:CGRectZero];
//【需求变更】创建话题额外提示(bug:5491)
_leaveMessagePlaceHolder.text = @"(可选)还可以给小编留言哦,详细描述您的通讯方式和作答时间等信息,可以帮助更快审核通过 \n(不超过800个字,仅小编可见)";
_leaveMessagePlaceHolder.textColor = [UIColor colorWithHexString:TextGray];
_leaveMessagePlaceHolder.font = appFont(14, NO);
_leaveMessagePlaceHolder.textAlignment = NSTextAlignmentLeft;
_leaveMessagePlaceHolder.hidden = NO;
_leaveMessagePlaceHolder.numberOfLines = 2;
_leaveMessagePlaceHolder.lineBreakMode = NSLineBreakByWordWrapping;
_leaveMessagePlaceHolder.backgroundColor = [UIColor clearColor];
}
return _leaveMessagePlaceHolder;
}
- (UIButton*)imageSelectBtn {
if (!_imageSelectBtn) {
_imageSelectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_imageSelectBtn.backgroundColor = [UIColor clearColor];
[_imageSelectBtn setImage:Image(@"topic/topicBackSelect.png") forState:UIControlStateNormal];
[_imageSelectBtn addTarget:self action:@selector(selectBackImg:) forControlEvents:UIControlEventTouchUpInside];
}
return _imageSelectBtn;
}
- (UILabel *)imageSelectLabel {
if (!_imageSelectLabel) {
_imageSelectLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_imageSelectLabel.text = @"设置封面(可选)";
_imageSelectLabel.textColor = [UIColor colorWithHexString:TextGray];
_imageSelectLabel.font = appFont(17, NO);
_imageSelectLabel.backgroundColor = [UIColor clearColor];
_imageSelectLabel.textAlignment = NSTextAlignmentLeft;
}
return _imageSelectLabel;
}
-(UILabel *)actionLabel{
if (!_actionLabel) {
_actionLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_actionLabel.text = @"您的话题问答授权澎湃新闻使用";
_actionLabel.textColor = [UIColor colorWithHexString:TextGray];
_actionLabel.font = appFont(14, NO);
_actionLabel.textAlignment = NSTextAlignmentCenter;
}
return _actionLabel;
}
- (TPCustomButton*)confirmBtn {
if (!_confirmBtn) {
_confirmBtn = [[TPCustomButton alloc]initWithFrame:CGRectZero];
_confirmBtn.title = @"提 交";
[_confirmBtn addTarget:self action:@selector(confirmEvent:) forControlEvents:UIControlEventTouchUpInside];
}
return _confirmBtn;
}
- (AsyncImageView*)topicBackImageView {
if (!_topicBackImageView) {
_topicBackImageView = [[AsyncImageView alloc]initWithFrame:CGRectZero];
_topicBackImageView.isHaveWaterPrint = NO;
}
return _topicBackImageView;
}
- (UIButton*)deleteImageBtn {
if (!_deleteImageBtn) {
_deleteImageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_deleteImageBtn.frame = CGRectZero;
[_deleteImageBtn setImage:Image(@"Button/delete.png") forState:UIControlStateNormal];
[_deleteImageBtn setImage:Image(@"Button/delete_s.png") forState:UIControlStateHighlighted];
[_deleteImageBtn addTarget:self action:@selector(deleteImage:) forControlEvents:UIControlEventTouchUpInside];
}
return _deleteImageBtn;
}
#pragma mark - add subViews
- (void)addSubviewsWithNoneTopic {
CGFloat num = self.cateList.count/7;
CGFloat remind = self.cateList.count%7;
if (remind == 0) {
lineNum = (int)num;
}else {
lineNum = (int)num + 1;
}
[self.backScrollView addSubview:self.headImageView];
[self.backScrollView addSubview:self.vImg];
[self.backScrollView addSubview:self.userNameLabel];
[self.backScrollView addSubview:self.sexImg];
[self.backScrollView addSubview:self.categorySelectView];
[self.backScrollView addSubview:self.topicTitleTextView];
[self.backScrollView addSubview:self.topicDescribeView];
[self.backScrollView addSubview:self.leaveMessageView];
[self.backScrollView addSubview:self.imageSelectBtn];
[self.backScrollView addSubview:self.imageSelectLabel];
[self.backScrollView addSubview:self.actionLabel];
[self.backScrollView addSubview:self.confirmBtn];
[self viewWillLayoutSubviews];
}
#pragma mark - scrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[self closeKeyBoard];
}
#pragma mark - textField and textView Delegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([self isContainsEmoji:text]) {
return NO;
}else {
return YES;
};
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([self isContainsEmoji:string]) {
return NO;
}else {
return YES;
}
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
CGRect textRect = textView.frame;
responderHeight = textRect.origin.y+textRect.size.height;
return YES;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
CGRect textRect = textField.frame;
responderHeight = textRect.origin.y+textRect.size.height;
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[self closeKeyBoard];
return YES;
}
- (void)textViewDidChange:(UITextView *)textView {
if (textView.tag == DESCRIBETEXTVIEWTAG) {
self.topicInfo.desc = self.topicDescribeView.text;
if (textView.text.length > 0) {
self.topicDescribePlaceHolder.hidden = YES;
if (textView.text.length >= 800) {
textView.text = [textView.text substringToIndex:800];
}
}else self.topicDescribePlaceHolder.hidden = NO;
}else {
self.topicInfo.message = self.leaveMessageView.text;
if (textView.text.length > 0) {
self.leaveMessagePlaceHolder.hidden = YES;
if (textView.text.length >= 800) {
textView.text = [textView.text substringToIndex:800];
}
}else self.leaveMessagePlaceHolder.hidden = NO;
}
}
- (void)selfIntroDidChange:(NSNotification*)obj {//我的话题,创建话题界面,input1和input2出现逻辑错误(bug:4808)
UITextField *textField = (UITextField *)obj.object;
if (textField.tag == SELFINTROTAG) {
// NSString *toBeString = textField.text;
NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写
UITextRange *selectedRange = [textField markedTextRange];
//获取高亮部分
UITextPosition *position;
if (selectedRange) {
position = [textField positionFromPosition:selectedRange.start offset:0];
}else{
position = nil;
}
if (!position) {
if (textField.text.length > 20) {
textField.text = [textField.text substringToIndex:20];
}
}
}else{// 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
if(textField.text.length >20){
textField.text = [textField.text substringToIndex:20];
}
}
}
NSString *intro = self.selfIntroductionField.text;
NSString *topicTit = self.topicField2.text;
NSString *title = [NSString stringWithFormat:@"%@,%@,问我吧!",intro,topicTit];
self.topicInfo.title = title;
}
- (void)topicFieldDidChange:(NSNotification*)obj {//我的话题,创建话题界面,input1和input2出现逻辑错误(bug:4808)
UITextField *field = (UITextField *)obj.object;
if (field.tag == TOPICFIELDTAG) {
NSString *toBeString = field.text;
NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
if ([lang isEqualToString:@"zh-Hans"] || [lang isEqualToString:@"en-US"] || [lang isEqualToString:@"zh-Hant"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写
UITextRange *selectedRange = [field markedTextRange];
//获取高亮部分
UITextPosition *position;
if (selectedRange) {
position = [field positionFromPosition:selectedRange.start offset:0];
}else{
position = nil;
}
// 没有高亮选择的字,则对已输入的文字进行字数统计和限制
if (!position) {
if (toBeString.length > 35) {
field.text = [field.text substringToIndex:35];
}
}else{// 有高亮选择的字符串,则暂不对文字进行统计和限制
}
}else{ // 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
if (toBeString.length > 35) {
field.text = [field.text substringToIndex:35];
}
}
}
NSString *intro = self.selfIntroductionField.text;
NSString *topicTit = self.topicField2.text;
NSString *title = [NSString stringWithFormat:@"%@,%@,问我吧!",intro,topicTit];
self.topicInfo.title = title;
}
//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
if (responderHeight < CGRectGetMaxY(self.topicTitleTextView.frame)) {
return;
}
//获取键盘的高度
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
CGFloat keyboardHeight = keyboardRect.size.height;
//创建话题页,横屏选中给小编留言,弹出键盘,竖屏,上方出现大量空白(bug:4744)
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
//TODO
}else {
if (isNotIOS8) {
keyboardHeight = keyboardRect.size.width;
}
}
CGFloat offset = CGRectGetHeight(self.backScrollView.frame)-keyboardHeight;
if (responderHeight > offset) {
keyboardOffset = responderHeight - offset+50;
[self.backScrollView setContentOffset:CGPointMake(0, keyboardOffset)];
}else {
CGFloat naviHeight = 169;
if (isNotIOS7) {
naviHeight = 169-20;
}
if (responderHeight <= naviHeight) {
keyboardOffset = responderHeight - naviHeight;
[self.backScrollView setContentOffset:CGPointMake(0, keyboardOffset)];
}
}
}
//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification
{
CGFloat maxHeight = CGRectGetMaxY(self.topicTitleTextView.frame);
if (responderHeight < maxHeight) {
return;
}
CGPoint currentOffset = self.backScrollView.contentOffset;
currentOffset.y = currentOffset.y - keyboardOffset;
[self.backScrollView setContentOffset:currentOffset];
keyboardOffset = 0;
}
#pragma mark - is emoji
- (BOOL)isContainsEmoji:(NSString *)string {
__block BOOL isEomji = NO;
[string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
const unichar hs = [substring characterAtIndex:0];
// surrogate pair
if (0xd800 <= hs && hs <= 0xdbff) {
if (substring.length > 1) {
const unichar ls = [substring characterAtIndex:1];
const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
if (0x1d000 <= uc && uc <= 0x1f77f) {
isEomji = YES;
}
}
} else {
// non surrogate
if (0x2100 <= hs && hs <= 0x27ff && hs != 0x263b) {
isEomji = YES;
} else if (0x2B05 <= hs && hs <= 0x2b07) {
isEomji = YES;
} else if (0x2934 <= hs && hs <= 0x2935) {
isEomji = YES;
} else if (0x3297 <= hs && hs <= 0x3299) {
isEomji = YES;
} else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50|| hs == 0x231a ) {
isEomji = YES;
}
if (!isEomji && substring.length > 1) {
const unichar ls = [substring characterAtIndex:1];
if (ls == 0x20e3) {
isEomji = YES;
}
}
}
}];
return isEomji;
}
#pragma mark - handler
- (void)closeKeyBoard {
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}
- (void)tapToCloseKeyboard:(id)sender {
[self closeKeyBoard];
}
- (void)backButtonHandler:(id)sender {
[self closeKeyBoard];
if (!topicBO) {
if (isBlankString(self.topicInfo.category)
&&isBlankString(self.topicInfo.title)
&&isBlankString(self.topicInfo.desc)
&&isBlankString(self.topicInfo.message)
&&isBlankString(self.topicInfo.imageId)) {
[self.navigationController popViewControllerAnimated:YES];
}else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"是否放弃已输入内容?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
alert.tag = 1003;
[alert show];
}
}else {
if ([self.topicInfo.category isEqualToString:self.topicBO.category]
&& [self.topicInfo.title isEqualToString:self.topicBO.title]
&& [self.topicInfo.desc isEqualToString:self.topicBO.desc]
&& [self.topicInfo.message isEqualToString:self.topicBO.message]) {
if (!self.topicInfo.imageId) {
[self.navigationController popViewControllerAnimated:YES];
}else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"是否放弃已输入内容?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
alert.tag = 1004;
[alert show];
}
}else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"是否放弃已输入内容?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
alert.tag = 1001;
[alert show];
}
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != 0) {
if (alertView.tag == 1001 || alertView.tag == 1003 || alertView.tag == 1004){
[alertView dismissWithClickedButtonIndex:-1 animated:YES];
[self.navigationController popViewControllerAnimated:YES];
}
}else{
if (alertView.tag == 1002) {
NSMutableDictionary *dic = setDataModelToDic(self.topicInfo, [TopicInfoBO class]);
[dic setValue:self.topicInfo.desc forKey:@"description"];
[dic setValue:self.topicInfo.topicId forKey:@"topicId"];
__weak typeof(self) Self = self;
if (self.topicBackImg && isBlankString(self.topicInfo.imageId)) {
[self uploadImage:self.topicBackImg success:^(NSString *imageID) {
[self stopHud];
[dic setValue:imageID forKey:@"imageId"];
[Remote doJsonActionWithBlock:0 requestUrl:saveTopicURL parameter:dic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
[MobClick event:@"47"];
[Self.navigationController popViewControllerAnimated:YES];
[[NSNotificationCenter defaultCenter]postNotificationName:REFRESHAFTERCREATTOPIC object:nil];
}else {
ShowTextMessage(message);
}
}];
}];
}else {
[dic setValue:self.topicInfo.imageId forKey:@"imageId"];
[Remote doJsonActionWithBlock:0 requestUrl:saveTopicURL parameter:dic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
[MobClick event:@"47"];
[Self.navigationController popViewControllerAnimated:YES];
[[NSNotificationCenter defaultCenter]postNotificationName:REFRESHAFTERCREATTOPIC object:nil];
}else {
ShowTextMessage(message);
}
}];
}
}else{
}
}
}
- (void)selectOneCate:(UIButton*)btn {
if (btn.selected) {
return;
}
btn.selected = !btn.selected;
[self.cateBtnList enumerateObjectsUsingBlock:^(cateBtn* obj, NSUInteger idx, BOOL *stop) {
if (obj.tag != btn.tag) {
obj.selected = NO;
}
}];
categoryBO *cateBO = self.cateList[btn.tag - CATEBTNTAG];
self.topicInfo.category = cateBO.category;
self.topicInfo.categoryName = cateBO.name;
}
- (void)confirmEvent:(UIButton*)btn {
[self closeKeyBoard];
if (isBlankString(self.topicInfo.category)) {
ShowTextMessage(@"必填项不能为空");
return;
}
if (self.selfIntroductionField.text.length + self.topicField2.text.length >35) {
ShowTextMessage(@"话题标题过长,请重新输入!");
return;
}
NSString *intro = self.selfIntroductionField.text;
NSString *topicTit = self.topicField2.text;
if (isBlankString(intro) || [intro isEqualToString:@"我是"] || isBlankString(topicTit)) {
ShowTextMessage(@"必填项不能为空");
return;
}
NSString *title = [NSString stringWithFormat:@"%@,%@,问我吧!",intro,topicTit];
self.topicInfo.title = title;
if (isBlankString(self.topicDescribeView.text)) {
ShowTextMessage(@"必填项不能为空");
return;
}
if (self.topicDescribeView.text.length < 4) {
ShowTextMessage(@"话题描述少于4个字!");
return;
}
if (self.topicDescribeView.text.length > 800) {
ShowTextMessage(@"话题描述大于800个字!");
return;
}
self.topicInfo.desc = self.topicDescribeView.text;
self.topicInfo.message = self.leaveMessageView.text;
if (!_tag ||[_tag isEqualToString:@"0"]) {
NSMutableDictionary *dic = setDataModelToDic(self.topicInfo, [TopicInfoBO class]);
[dic setValue:self.topicInfo.desc forKey:@"description"];
__weak typeof(self) Self = self;
if (self.topicBackImg && isBlankString(self.topicInfo.imageId)) {
[self uploadImage:self.topicBackImg success:^(NSString *imageID) {
[self stopHud];
[dic setValue:imageID forKey:@"imageId"];
[Remote doJsonActionWithBlock:0 requestUrl:saveTopicURL parameter:dic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
[MobClick event:@"35"];
[Self.navigationController popViewControllerAnimated:YES];
[[NSNotificationCenter defaultCenter]postNotificationName:REFRESHAFTERCREATTOPIC object:nil];
}else {
ShowTextMessage(message);
}
}];
}];
}else {
[dic setValue:self.topicInfo.imageId forKey:@"imageId"];
[Remote doJsonActionWithBlock:0 requestUrl:saveTopicURL parameter:dic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
[MobClick event:@"35"];
[Self.navigationController popViewControllerAnimated:YES];
[[NSNotificationCenter defaultCenter]postNotificationName:REFRESHAFTERCREATTOPIC object:nil];
}else {
ShowTextMessage(message);
}
}];
}
}else{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"是否确认提交话题修改?" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:@"放弃",nil];
alert.tag = 1002;
[alert show];
}
}
- (void)selectBackImg:(UIButton*)btn {
[self showActionSheet];
}
- (void)showActionSheet {
if (self.actionSheet) {
self.actionSheet = nil;
}
[self closeKeyBoard];
self.actionSheet = [[UIActionSheet alloc]initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:@"相册选择",@"拍照",nil];
self.actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
self.actionSheet.center = self.view.center;
[self.actionSheet showInView:self.view];
}
- (void)deleteImage:(UIButton*)btn {
if (self.topicBackImg) {
[UIView animateWithDuration:0.3 animations:^{
[self.topicBackImageView removeFromSuperview];
[self.deleteImageBtn removeFromSuperview];
_deleteImageBtn = nil;
_topicBackImageView = nil;
[self viewWillLayoutSubviews];
} completion:^(BOOL finished) {
self.topicBackImg = nil;
if (!isBlankString(self.topicInfo.pic)) {
self.topicInfo.imageId = @"-1";
}
}];
}
}
#pragma mark - actionsheet delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
pickerIndex = buttonIndex;
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
// picker.navigationController.navigationBar.barTintColor = [UIColor blackColor];
// picker.navigationController.navigationBar.tintColor = [UIColor whiteColor];
// [picker.navigationController.navigationBar setTitleTextAttributes:
// @{NSForegroundColorAttributeName:[UIColor whiteColor]}];
picker.delegate = self;
picker.allowsEditing = NO;
if (buttonIndex == 0) {
if (popController) {
self.popController = nil;
}
self.popController = [[UIPopoverController alloc]initWithContentViewController:picker];
self.popController.delegate = self;
// self.popController.popoverContentSize = CGSizeMake(300, 500);
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self showPopOver];
}else if (buttonIndex == 1) {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.mediaTypes = [NSArray arrayWithObject:@"public.image"];
[self presentViewController:picker animated:YES completion:nil];
}
self.actionSheet = nil;
}
- (void)showPopOver {
CGRect rect = self.imageSelectBtn.frame;
rect.origin.y = rect.origin.y - self.backScrollView.contentOffset.y;
[self.popController presentPopoverFromRect:rect
inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft
animated:YES];
}
#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
if (pickerIndex == 0) {
[self.popController dismissPopoverAnimated:YES];
self.popController = nil;
}else if (pickerIndex == 1){
[picker dismissViewControllerAnimated:YES completion:nil];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)infos {
@try {
if (pickerIndex == 0) {
[self.popController dismissPopoverAnimated:YES];
self.popController = nil;
[self editImage:infos];
}else if (pickerIndex == 1){
[picker dismissViewControllerAnimated:YES completion:^{
[self editImage:infos];
}];
}
}
@catch (NSException * e) {
TPLOG(@"%@",[e reason]);
}
}
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
}
- (void)editImage:(NSDictionary *)infos{
NSString *mediaType = [infos objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.image"]){
UIImage *image = [infos objectForKey:@"UIImagePickerControllerOriginalImage"];
MLImageCrop *imageCrop = [[MLImageCrop alloc]init];
imageCrop.delegate = self;
imageCrop.ratioOfWidthAndHeight = 600.0f/450.0f;
imageCrop.image = image;
[imageCrop showWithAnimation:YES];
}
}
#pragma mark - crop delegate
- (void)cropImage:(UIImage*)croppedImage forOriginalImage:(UIImage*)originalImage
{
croppedImage = [croppedImage imageByScalingProportionallyToMinimumSize:CGSizeMake(600, 450)];
self.topicBackImg = croppedImage;
if (!_topicBackImageView) {
[self.backScrollView addSubview:self.topicBackImageView];
self.topicBackImageView.imageView.image = self.topicBackImg;
self.topicInfo.imageId = @"";
[self.backScrollView addSubview:self.deleteImageBtn];
[self viewWillLayoutSubviews];
}else {
self.topicBackImageView.imageView.image = self.topicBackImg;
}
self.topicInfo.imageId = @"";
}
- (void)getImageSuccess:(UIImage *)image {
self.topicBackImg = image;
}
#pragma mark - remote delegate
- (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responsData {
if (actionTag == 0) {
NSArray *tempCataList = responsData[@"dataList"];
self.cateList = [NSMutableArray array];
categoryBO *allCata = categoryBO.new;
allCata.name = @"全部";
allCata.enname = @"ALL";
allCata.category = @"";
[self.cateList addObject:allCata];
[tempCataList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
categoryBO *cate = setJsonDicToDataModel(obj, [categoryBO class]);
[self.cateList addObject:cate];
}];
[self addSubviewsWithNoneTopic];
if (!self.topicBO) {
self.topicInfo = TopicInfoBO.new;
[self stopHud];
}else {
NSDictionary *dic = @{@"topicId":self.topicBO.topicId,@"forwordType":@"7"};
[Remote doJsonAction:1 requestUrl:topicInfoURL parameter:dic delegate:self];
}
}else {
self.topicBO = setJsonDicToDataModel(responsData[@"topicInfo"], [TopicInfoBO class]);
self.topicBO.desc = responsData[@"topicInfo"][@"description"];
NSString *newNums = responsData[@"topicInfo"][@"newNums"];
if (newNums) {
self.topicBO.unNums = newNums;
}
TopicInfoBO *temp = setJsonDicToDataModel(responsData[@"topicInfo"], [TopicInfoBO class]);
temp.desc = responsData[@"topicInfo"][@"description"];
if (newNums) {
temp.unNums = newNums;
}
self.topicInfo = temp;
[self stopHud];
}
}
- (void)remoteResponsFailed:(int)actionTag withMessage:(NSString *)message resultCode:(NSString *)code {
ShowTextMessage(message);
[self stopHud];
}
#pragma mark - orentation
- (void)statusBarOrientationChange:(NSNotification *)notification {
[self closeKeyBoard];
if (self.actionSheet && self.actionSheet.isVisible) {
[self.actionSheet dismissWithClickedButtonIndex:-1 animated:YES];
[self showActionSheet];
}else if (self.popController && self.popController.isPopoverVisible) {
[self.popController dismissPopoverAnimated:NO];
}
}
- (void)dealloc {
//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)startHud {
if (!self.hud) {
self.hud = [MBProgressHUD showHUDAddedTo:KEY_WINDOW animated:YES];
self.hud.mode = MBProgressHUDModeIndeterminate;
self.hud.userInteractionEnabled = YES;
self.hud.dimBackground = NO;
UIColor *hudColor = [UIColor colorWithRed:76.5f/255.f green:76.5f/255.f blue:76.5f/255.f alpha:0.9f];
self.hud.color = hudColor;
self.hud.cornerRadius = 4;
self.hud.removeFromSuperViewOnHide = YES;
}
}
- (void)stopHud {
if (self.hud) {
[self.hud hide:YES];
self.hud = nil;
}
}
- (void)uploadImage:(UIImage *)image success:(void(^)(NSString *imageID))successful{
NSData *imgData = UIImageJPEGRepresentation(image, 0.1f);
CGFloat lengh = [imgData length];
// UIImage *temImg = [UIImage imageWithData:imgData];
// self.topicBackImg = temImg;
// self.topicBackImageView.imageView.image = temImg;
// return;
// if (lengh/1024 > 100) {
// imgData = UIImageJPEGRepresentation(image, 0.5f);
// lengh = [imgData length];
// }
NSDictionary *dic = @{@"C_TYPE":@"jpg",@"C_LEN":[NSString stringWithFormat:@"%.f",lengh],@"O_TYPE":@"1",@"O_ID":self.topicInfo.topicId?self.topicInfo.topicId:@""};
__weak typeof(self) Self = self;
[self startHud];
[Remote upLoadImageAsyncWithBlock:getUploadURL actionTag:0 parameter:dic image:nil completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
NSString *status = responseData[@"STATUS"];
if ([status intValue] != 1 && [status intValue] != 13) {
ShowTextMessage(responseData[@"DESC"]);
[Self stopHud];
return;
}
NSString *url = responseData[@"URL"];
NSString *fileName = responseData[@"FILE_NAME"];
SaveFile(CachePath(fileName), imgData);
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:responseData];
[dic removeObjectForKey:@"URL"];
[dic setValue:@"0" forKey:@"P_START"];
[dic setValue:dic[@"C_LEN"] forKey:@"P_END"];
[dic removeObjectForKey:@"PARAM"];
[dic removeObjectForKey:@"STATUS"];
[dic removeObjectForKey:@"RESULT"];
[Remote upLoadImageAsyncWithBlock:url actionTag:1 parameter:dic image:CachePath(fileName) completion:^(BOOL success, NSString *message, id responseData) {
[Self stopHud];
if (success) {
TPLOG (@"%@",responseData);
NSString *status = responseData[@"STATUS"];
if ([status intValue] == 13) {
// NSString *newImageURL = responseData[@"IMAGEURL"];
NSString *imageID = responseData[@"IMAGEID"];
if (successful) {
successful(imageID);
}
}else {
ShowTextMessage(message);
}
}else {
ShowTextMessage(message);
}
}];
}else {
ShowTextMessage(message);
[Self stopHud];
}
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
|