CommodityTitleView.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. //
  2. // CommodityTitleView.m
  3. // yunbaolive
  4. //
  5. // Created by ybRRR on 2020/2/24.
  6. // Copyright © 2020 cat. All rights reserved.
  7. //
  8. #import "CommodityTitleView.h"
  9. #import "YBImageView.h"
  10. #import "ShowDetailVC.h"
  11. @implementation CommodityTitleView
  12. -(instancetype)initWithFrame:(CGRect)frame
  13. {
  14. self = [super initWithFrame:frame];
  15. if (self) {
  16. self.backgroundColor = UIColor.whiteColor;
  17. imageArray = [NSMutableArray array];
  18. _newvideoPath = @"";
  19. [self createUI];
  20. }
  21. return self;
  22. }
  23. -(void)createUI{
  24. UILabel*title = [[UILabel alloc]init];
  25. title.frame = CGRectMake(12, 10, 180, 20);
  26. title.font = [UIFont boldSystemFontOfSize:14];
  27. title.text = YZMsg(@"商品标题");
  28. title.textColor = [UIColor blackColor];
  29. [self addSubview:title];
  30. self.titleText = [[UITextField alloc]init];
  31. self.titleText.frame = CGRectMake(12, title.bottom+10, _window_width-24, 20);
  32. self.titleText.font = [UIFont systemFontOfSize:14];
  33. self.titleText.textColor = [UIColor blackColor];
  34. self.titleText.backgroundColor = [UIColor clearColor];
  35. self.titleText.returnKeyType = UIReturnKeyDone;
  36. self.titleText.delegate = self;
  37. [self.titleText addTarget:self action:@selector(titleTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  38. NSMutableAttributedString*holderString = [[NSMutableAttributedString alloc] initWithString:YZMsg(@"建议填写产品词、属性词、营销词等,不超过15个字") attributes:@{NSForegroundColorAttributeName:[UIColor grayColor] }];
  39. self.titleText.attributedPlaceholder = holderString;
  40. [self addSubview:self.titleText];
  41. imgWidth = (_window_width - 75)/4;
  42. videoBtn = [UIButton buttonWithType:0];
  43. videoBtn.frame = CGRectMake(15, self.titleText.bottom+15, imgWidth, imgWidth);
  44. videoBtn.backgroundColor = RGB(245, 245, 245);// RGB_COLOR(@"#302D41", 1);
  45. [videoBtn addTarget:self action:@selector(imageBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  46. videoBtn.tag = 1000;
  47. [videoBtn setImage:[UIImage imageNamed:@"addComm"] forState:0];
  48. [videoBtn setTitle:YZMsg(@"商品视频") forState:0];
  49. [videoBtn setTitleColor:[UIColor grayColor] forState:0];
  50. videoBtn.titleLabel.font = [UIFont systemFontOfSize:10];
  51. videoBtn = [PublicObj setUpImgDownText:videoBtn];
  52. [self addSubview:videoBtn];
  53. imgeBtn = [UIButton buttonWithType:0];
  54. imgeBtn.frame = CGRectMake(15+(1%4)*(imgWidth + 15), self.titleText.bottom+15, imgWidth, imgWidth);
  55. imgeBtn.backgroundColor = RGB(245, 245, 245);// RGB_COLOR(@"#302D41", 1);
  56. [imgeBtn addTarget:self action:@selector(imageBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  57. [imgeBtn setImage:[UIImage imageNamed:@"addComm"] forState:0];
  58. [imgeBtn setTitle:YZMsg(@"商品图") forState:0];
  59. [imgeBtn setTitleColor:[UIColor grayColor] forState:0];
  60. imgeBtn.titleLabel.font = [UIFont systemFontOfSize:10];
  61. imgeBtn = [PublicObj setUpImgDownText:imgeBtn];
  62. imgeBtn.tag = 1001;
  63. [self addSubview:imgeBtn];
  64. }
  65. - (void)imageBtnClick:(UIButton *)sender{
  66. if (sender.tag == 1001) {
  67. TZImagePickerController *imagePC = [[TZImagePickerController alloc]initWithMaxImagesCount:9-imageArray.count delegate:self];
  68. imagePC.preferredLanguage = [lagType isEqual:ZH_CN] ? @"zh-Hans":@"en";
  69. imagePC.modalPresentationStyle = 0;
  70. imagePC.showSelectBtn = YES;
  71. imagePC.allowCrop = NO;
  72. imagePC.allowPickingOriginalPhoto = NO;
  73. imagePC.oKButtonTitleColorNormal = Pink_Cor;
  74. imagePC.allowTakePicture = YES;
  75. imagePC.allowTakeVideo = NO;
  76. imagePC.allowPickingVideo = NO;
  77. imagePC.allowPickingMultipleVideo = NO;
  78. [[[YBBaseAppDelegate sharedAppDelegate] topViewController]presentViewController:imagePC animated:YES completion:nil];
  79. }else if (sender.tag == 1000){
  80. BOOL isShowLive = [[NSUserDefaults standardUserDefaults]boolForKey:@"isShowChatLive"];
  81. if (isShowLive) {
  82. [MBProgressHUD showError:YZMsg(@"直播间开启期间不可使用该操作")];
  83. return;
  84. }
  85. TZImagePickerController *imagePC = [[TZImagePickerController alloc]initWithMaxImagesCount:1 delegate:self];
  86. imagePC.preferredLanguage = [lagType isEqual:ZH_CN] ? @"zh-Hans":@"en";
  87. imagePC.modalPresentationStyle = 0;
  88. imagePC.showSelectBtn = NO;
  89. imagePC.allowCrop = NO;
  90. imagePC.allowPickingOriginalPhoto = NO;
  91. imagePC.oKButtonTitleColorNormal = Pink_Cor;
  92. imagePC.allowPickingImage = NO;
  93. imagePC.allowTakePicture = NO;
  94. imagePC.allowTakeVideo = YES;
  95. imagePC.allowPickingVideo = YES;
  96. imagePC.allowPickingMultipleVideo = NO;
  97. imagePC.videoMaximumDuration = live_MAX_RECORD_TIME;
  98. [[[YBBaseAppDelegate sharedAppDelegate] topViewController]presentViewController:imagePC animated:YES completion:nil];
  99. }
  100. }
  101. -(BOOL)textFieldShouldReturn:(UITextField *)textField
  102. {
  103. [self.titleText resignFirstResponder];
  104. return YES;
  105. }
  106. #pragma mark - UITextViewDelegate
  107. -(void)titleTextFieldDidChange:(UITextField *)textField
  108. {
  109. NSString *nameString;
  110. if (textField == self.titleText) {
  111. nameString = self.titleText.text;
  112. }
  113. NSString *lang = [[[UITextInputMode activeInputModes]firstObject] primaryLanguage]; // 键盘输入模式
  114. if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写
  115. UITextRange *selectedRange = [self.titleText markedTextRange];//获取高亮部分
  116. UITextPosition *position = [self.titleText positionFromPosition:selectedRange.start offset:0];
  117. //没有高亮选择的字,则对已输入的文字进行字数统计和限制
  118. if (!position) {
  119. if (nameString.length > 15) {
  120. self.titleText.text = [nameString substringToIndex:15];
  121. }
  122. }else{
  123. //有高亮选择的字符串,则暂不对文字进行统计和限制
  124. }
  125. }else{
  126. // 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
  127. if (nameString.length > 15) {
  128. self.titleText.text = [nameString substringToIndex:15];
  129. }
  130. }
  131. }
  132. #pragma mark - TZImagePickerControllerDelegate
  133. - (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingVideo:(UIImage *)coverImage sourceAssets:(PHAsset *)asset {
  134. [MBProgressHUD showMessage:@""];
  135. [[TZImageManager manager] getVideoOutputPathWithAsset:asset presetName:AVAssetExportPresetMediumQuality success:^(NSString *outputPath) {
  136. NSLog(@"视频导出到本地完成,沙盒路径为:%@",outputPath);
  137. if (outputPath) {
  138. [MBProgressHUD hideHUD];
  139. self.videoPath = outputPath;
  140. self.newvideoPath = outputPath;
  141. self.videoCoverImage = coverImage;
  142. [self addVideoimage];
  143. }else{
  144. [MBProgressHUD hideHUD];
  145. [MBProgressHUD showError:YZMsg(@"请重新选择(iCloud视频请先在本地相册下载后上传)")];
  146. }
  147. } failure:^(NSString *errorMessage, NSError *error) {
  148. [MBProgressHUD hideHUD];
  149. [MBProgressHUD showError:errorMessage];
  150. NSLog(@"视频导出失败:%@,error:%@",errorMessage, error);
  151. }];
  152. }
  153. - (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto{
  154. NSLog(@"------多选择图片--:%@",photos);
  155. [imageArray addObjectsFromArray:photos];
  156. [self.delegate setCommodityImg:imageArray];
  157. [self setImageFrame];
  158. }
  159. #pragma mark-----添加视频------------
  160. -(void)addVideoimage{
  161. subVideoBtn = [UIButton buttonWithType:0];
  162. subVideoBtn.frame = CGRectMake(0, 0, videoBtn.width, videoBtn.height);
  163. if ([self.fromStr isEqual:@"seller"]) {
  164. [subVideoBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:self.goodsModel.video_thumb_format] forState:0];
  165. }else{
  166. [subVideoBtn setBackgroundImage:self.videoCoverImage forState:0];
  167. }
  168. subVideoBtn.contentMode = UIViewContentModeScaleAspectFill;
  169. subVideoBtn.clipsToBounds = YES;
  170. [subVideoBtn setImage:[UIImage imageNamed:@"evaluate_播放"] forState:0];
  171. [subVideoBtn addTarget:self action:@selector(showVideo) forControlEvents:UIControlEventTouchUpInside];
  172. [videoBtn addSubview:subVideoBtn];
  173. UIButton *delBtn = [UIButton buttonWithType:0];
  174. delBtn.frame = CGRectMake(subVideoBtn.width-25, 5, 20, 20);
  175. [delBtn setBackgroundImage:[UIImage imageNamed:@"comm_del"] forState:0];
  176. [delBtn addTarget:self action:@selector(delVideoClick) forControlEvents:UIControlEventTouchUpInside];
  177. [subVideoBtn addSubview:delBtn];
  178. }
  179. #pragma mark-------预览图片------------
  180. -(void)showImage:(UIButton *)sender{
  181. NSInteger index = sender.tag-3000-1;
  182. YBImageView *imgView = [[YBImageView alloc] initWithImageArray:imageArray andIndex:index andMine:NO isDtCell:NO andBlock:^(NSArray * _Nonnull array) {
  183. }];
  184. [imgView hideDelete];
  185. [[UIApplication sharedApplication].keyWindow addSubview:imgView];
  186. }
  187. #pragma mark-------预览视频------------
  188. -(void)showVideo{
  189. ShowDetailVC *detail = [[ShowDetailVC alloc]init];
  190. detail.videoPath =self.videoPath;
  191. detail.deleteEvent = ^(NSString *type) {
  192. };
  193. [[YBBaseAppDelegate sharedAppDelegate]pushViewController:detail animated:NO];
  194. }
  195. #pragma mark-------删除视频------------
  196. -(void)delVideoClick{
  197. self.videoPath = @"";
  198. [subVideoBtn removeFromSuperview];
  199. subVideoBtn = nil;
  200. }
  201. #pragma mark-------删除图片------------
  202. -(void)delImageClick:(UIButton *)sender{
  203. NSInteger index = sender.tag-4000-1;
  204. [imageArray removeObjectAtIndex:index];
  205. [self.delegate setCommodityImg:imageArray];
  206. [self setImageFrame];
  207. }
  208. #pragma mark-------重新赋值图片坐标----------
  209. -(void)setImageFrame{
  210. for (UIButton *btn in self.subviews) {
  211. if (btn.tag >=3000) {
  212. [btn removeFromSuperview];
  213. }
  214. }
  215. if (imageArray.count > 0) {
  216. if (imageArray.count < 3) {
  217. self.heightEvent(170);
  218. }else if (imageArray.count>=3 && imageArray.count< 7){
  219. if (self.heightEvent) {
  220. self.heightEvent(170+imgWidth+15);
  221. }
  222. }else if(imageArray.count >= 7){
  223. if (self.heightEvent) {
  224. self.heightEvent(170+imgWidth*2+30);
  225. }
  226. }
  227. for (int i = 1; i <= imageArray.count ; i ++) {
  228. UIButton *btn = [UIButton buttonWithType:0];
  229. btn.imageView.contentMode = UIViewContentModeScaleAspectFill;
  230. btn.imageView.clipsToBounds = YES;
  231. btn.backgroundColor = RGB(250, 250, 250);
  232. if (i < 4) {
  233. btn.frame = CGRectMake(15+(i%4)*(imgWidth + 15), self.titleText.bottom+15, imgWidth, imgWidth);
  234. }else if (i >= 4 && i < 8){
  235. btn.frame = CGRectMake(15+(i%4)*(imgWidth + 15), self.titleText.bottom+15+(imgWidth+15), imgWidth, imgWidth);
  236. }else{
  237. btn.frame = CGRectMake(15+(i%4)*(imgWidth + 15), self.titleText.bottom+15+imgWidth*2+30, imgWidth, imgWidth);
  238. }
  239. btn.tag = 3000+i;
  240. //如果是编辑图片需要赋值判断:判断图片类型:UIImage或网络图片
  241. if ([self.fromStr isEqual:@"seller"]) {
  242. if ([imageArray[i-1] isKindOfClass:[UIImage class]]) {
  243. [btn setImage:imageArray[i-1] forState:0];
  244. }else{
  245. [btn sd_setImageWithURL:[NSURL URLWithString:imageArray[i-1]] forState:0];
  246. }
  247. }else{
  248. [btn setImage:imageArray[i-1] forState:0];
  249. }
  250. [btn addTarget:self action:@selector(showImage:) forControlEvents:UIControlEventTouchUpInside];
  251. [self addSubview:btn];
  252. UIButton *delBtn = [UIButton buttonWithType:0];
  253. delBtn.frame = CGRectMake(imgWidth-25, 5, 20, 20);
  254. [delBtn setBackgroundImage:[UIImage imageNamed:@"comm_del"] forState:0];
  255. delBtn.tag = 4000+i;
  256. [delBtn addTarget:self action:@selector(delImageClick:) forControlEvents:UIControlEventTouchUpInside];
  257. [btn addSubview:delBtn];
  258. }
  259. }else{
  260. self.heightEvent(170);
  261. imgeBtn.frame = CGRectMake(15+(1%4)*(imgWidth + 15), self.titleText.bottom+15, imgWidth, imgWidth);
  262. [imgeBtn setTitle:YZMsg(@"商品图") forState:0];
  263. return;
  264. }
  265. if (imageArray.count == 9) {
  266. imgeBtn.hidden = YES;
  267. }else{
  268. imgeBtn.hidden = NO;
  269. imgeBtn.frame = CGRectMake(15+((imageArray.count+1)%4)*(imgWidth + 15), self.titleText.bottom+((imageArray.count+1)/4)*(imgWidth + 15)+15, imgWidth, imgWidth);
  270. [imgeBtn setTitle:[NSString stringWithFormat:@"%ld/9",imageArray.count] forState:0];
  271. imgeBtn = [PublicObj setUpImgDownText:imgeBtn];
  272. }
  273. }
  274. #pragma mark--------编辑商品赋值-----------------
  275. -(void)setDetaiModelData:(CommodityDetailModel *)detailModel fromWhere:(NSString *)from
  276. {
  277. _goodsModel = detailModel;
  278. self.fromStr = from;
  279. //有视频
  280. if (detailModel.video_thumb_format.length > 5) {
  281. [self addVideoimage];
  282. self.videoPath = detailModel.video_url_format;
  283. }
  284. //有图片
  285. if (detailModel.thumbs_format.count > 0) {
  286. [imageArray addObjectsFromArray:detailModel.thumbs_format];
  287. [self setImageFrame];
  288. [self.delegate setCommodityImg:imageArray];
  289. }
  290. }
  291. @end