// // SignatureViewController.m // RedAnt ERP Mobile // // Created by Ray on 14-8-7. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved. // #define INTNUMBERS @"0123456789\n" #import "SignatureViewController.h" #import "RAUtils.h" //#import "const.h" @interface SignatureViewController () @end @implementation SignatureViewController + (instancetype)ra_signatureViewControllerWithCompletion:(void (^)(UIImage *))blk { SignatureViewController * vc =[ [UIStoryboard storyboardWithName:@"signature" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"SignatureViewController"]; vc.onReturnImg = blk; return vc; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } -(BOOL) shouldAutorotate { return false; } - (void)onBackClick:(UIButton *)sender { [self.navigationController popViewControllerAnimated:FALSE]; } - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title = self.title; // ========================================== // 1. 導航欄外觀修復 (防止變黑/透明) // ========================================== if (@available(iOS 15.0, *)) { UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init]; [appearance configureWithOpaqueBackground]; // 強制不透明 appearance.backgroundColor = [UIColor whiteColor]; // 背景白 appearance.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor blackColor]}; self.navigationController.navigationBar.standardAppearance = appearance; self.navigationController.navigationBar.scrollEdgeAppearance = appearance; } else { self.navigationController.navigationBar.barTintColor = [UIColor whiteColor]; self.navigationController.navigationBar.translucent = NO; } // ========================================== // 2. 視圖佈局修復 (防止黑邊) // ========================================== // 禁止頂部延伸(避開導航欄),允許其他方向延伸(鋪滿底部) self.edgesForExtendedLayout = UIRectEdgeAll & ~UIRectEdgeTop; // 設置底色為白,防止加載時透出黑色 self.view.backgroundColor = [UIColor whiteColor]; // ========================================== // 3. 初始化畫板 (使用 Auto Layout) // ========================================== if(self.linewidth == 0) self.linewidth = 5; EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; // 【修改重點】初始化傳 CGRectZero,並關閉 Autoresizing 轉換 self.signatureView = [[SignatureView alloc] initWithFrame:CGRectZero context:context]; self.signatureView.translatesAutoresizingMaskIntoConstraints = NO; self.signatureView.backgroundColor = [UIColor whiteColor]; // 設置邊框樣式 self.signatureView.layer.borderColor = [UIColor darkGrayColor].CGColor; self.signatureView.layer.borderWidth = 1.0; [self.view addSubview:self.signatureView]; // 【修改重點】添加約束,將畫板強行釘死在父視圖的四個邊緣 // 這樣無論屏幕多長,它都會自動拉伸填滿,徹底消除底部黑邊 [NSLayoutConstraint activateConstraints:@[ [self.signatureView.topAnchor constraintEqualToAnchor:self.view.topAnchor], [self.signatureView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], [self.signatureView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], [self.signatureView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor] ]]; // ========================================== // 4. 導航欄按鈕設置 (保持原有邏輯) // ========================================== UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAutomatic] style:UIBarButtonItemStylePlain target:self action:@selector(onBackClick:)]; self.navigationItem.leftBarButtonItem = closeButton; UIBarButtonItem *clearBtn = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"clear"] imageWithRenderingMode:UIImageRenderingModeAutomatic] style:UIBarButtonItemStylePlain target:self action:@selector(onClear:)]; UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"save"] imageWithRenderingMode:UIImageRenderingModeAutomatic] style:UIBarButtonItemStylePlain target:self action:@selector(onDone:)]; UIBarButtonItem *settingBtn = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"signature_setting"] imageWithRenderingMode:UIImageRenderingModeAutomatic] style:UIBarButtonItemStylePlain target:self action:@selector(onSetting:)]; if (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPhone) { self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:doneBtn, clearBtn, nil]; } else { self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:doneBtn, clearBtn, settingBtn, nil]; } [self.navigationController.navigationBar layoutIfNeeded]; // 設置畫筆參數 [self.signatureView setLineWidth:self.linewidth]; self.signatureView.foregroundLineColor = [UIColor colorWithRed:0.204 green:0.596 blue:0.859 alpha:1.000]; } - (UIImage *)addImage:(UIImage *)image size:(CGSize )size { UIGraphicsBeginImageContext(size); UIImage *cleanImage = UIGraphicsGetImageFromCurrentImageContext(); // Draw image1 [image drawInRect:CGRectMake((size.width-image.size.width)/2,(size.height-image.size.height)/2, image.size.width, image.size.height)]; // Draw image2 [cleanImage drawInRect:CGRectMake(0, 0, cleanImage.size.width, cleanImage.size.height)]; UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return resultingImage; } - (void)onSetting:(UIButton *)sender { UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Change line width" message:nil preferredStyle:UIAlertControllerStyleAlert]; //block代码块取代了delegate [alertControl addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.text = [NSString stringWithFormat:@"%d",self.linewidth]; textField.keyboardType=UIKeyboardTypeNumberPad; textField.delegate = self; }]; UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { if(self.linewidth>16||self.linewidth<1) { int newlinewidth = 0; if(self.linewidth>16) { self.linewidth = 16; newlinewidth=16; } else if(self.linewidth<1) { self.linewidth = 1; newlinewidth=1; } [RAUtils message_box:@"Line width should between 1 and 16" message:[NSString stringWithFormat:@"Line width has been set to %d.",newlinewidth] completion:nil]; } [self.signatureView setLineWidth:self.linewidth]; }]; UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { // DebugLog(@"Cancel"); }]; [alertControl addAction:actionOne]; [alertControl addAction:alertthree]; //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField) [self presentViewController:alertControl animated:YES completion:nil]; } - (void)onDone:(UIButton *)sender { DebugLog(@"onDone"); UIImage* img = [self.signatureView signatureImage]; // return ; // CGSize imgsize = img.size; NSData* data = [self.signatureView signatureData]; if (self.Signaturedelegate && [self.Signaturedelegate respondsToSelector:@selector(SignatureVCReturnImage:indexPath:)]) { [self.Signaturedelegate SignatureVCReturnImage:img indexPath:self.indexPath]; } if (self.Signaturedelegate && [self.Signaturedelegate respondsToSelector:@selector(SignatureVCReturnData:indexPath:)]) { [self.Signaturedelegate SignatureVCReturnData:data indexPath:self.indexPath]; } [self.navigationController popViewControllerAnimated:true]; if(self.onReturnImg) self.onReturnImg(img); // [self.aBNewPersonNav dismissViewControllerAnimated:true completion:^{ // ; // }]; } - (void)onClear:(UIButton *)sender { [self.signatureView clear]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark textField delegate -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ NSCharacterSet *cs; cs = [[NSCharacterSet characterSetWithCharactersInString:INTNUMBERS]invertedSet]; NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; BOOL canChange = [string isEqualToString:filtered]; return canChange; } - (void)textFieldDidEndEditing:(UITextField *)textField { self.linewidth = [textField.text intValue]; } //- (void)textFieldDidBeginEditing:(UITextField *)textField //{ // DebugLog(@"textField shouldChangeCharactersInRange"); // // self.lastedit = textField; // UITableViewCell *cell = (UITableViewCell *) textField.superview.superview; // self.lastedit_from = [self.editorTable indexPathForCell:cell]; //} /* #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. } */ //- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { // self.signatureView.frame = self.view.bounds; //} - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator { [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; [coordinator animateAlongsideTransition:^(id _Nonnull context) { // what ever you want to prepare } completion:^(id _Nonnull context) { self.signatureView.frame = self.view.bounds; }]; } @end