Commit 310abcf6 authored by lmj_521aiau@163.com's avatar lmj_521aiau@163.com

Merge branch 'branch_1.0.1' of gitlab.huolea.com:lmi/shorthandmaster into branch_1.0.1

parents ff4cdcd3 3a2189a1
...@@ -12,8 +12,19 @@ import Photos ...@@ -12,8 +12,19 @@ import Photos
class SHInputController: SHBaseViewController class SHInputController: SHBaseViewController
{ {
// MARK: - property // MARK: - property
private var viewModel : SHInputViewModel = SHInputViewModel() /// 文件夹model
private var shInputView : SHInputView! var folderModel : SHRecordFolderModel!
/// 保存成功回调
var saveSuccessCallBack:((SHRecordFolderModel)->Void)?
/// 存储数据的model
private var dataModel: SHRecordModel = SHRecordModel()
/// 未填写标题时的默认标题
private let defaultTitle: String = "新建笔记"
private var shInputView: SHInputView!
private let recorder_mp3: SHMp3RecordManager = SHMp3RecordManager.shared() private let recorder_mp3: SHMp3RecordManager = SHMp3RecordManager.shared()
...@@ -51,62 +62,67 @@ class SHInputController: SHBaseViewController ...@@ -51,62 +62,67 @@ class SHInputController: SHBaseViewController
self.shInputView.bottomView.goOnAudioButton.addTarget(self, action: #selector(didSelectedGoOnAudioButton(sender:)), for: UIControl.Event.touchUpInside) self.shInputView.bottomView.goOnAudioButton.addTarget(self, action: #selector(didSelectedGoOnAudioButton(sender:)), for: UIControl.Event.touchUpInside)
self.shInputView.bottomView.stopAudioButton.addTarget(self, action: #selector(didSelectedStopAudioButton(sender:)), for: UIControl.Event.touchUpInside) self.shInputView.bottomView.stopAudioButton.addTarget(self, action: #selector(didSelectedStopAudioButton(sender:)), for: UIControl.Event.touchUpInside)
self.shInputView.bottomView.keyboardButton.addTarget(self, action: #selector(didSelectedKeyboardButton(sender:)), for: UIControl.Event.touchUpInside) self.shInputView.bottomView.keyboardButton.addTarget(self, action: #selector(didSelectedKeyboardButton(sender:)), for: UIControl.Event.touchUpInside)
// 有记录显示记录 // TODO: 先做新增, 查看和编辑稍后再做
if UserDefaults.standard.value(forKey: "InputRecord") != nil // // 有记录显示记录
{ // if UserDefaults.standard.value(forKey: "InputRecord") != nil
self.shInputView.placeholder.isHidden = true // {
// self.shInputView.placeholder.isHidden = true
let historyDic : [String : Any] = UserDefaults.standard.value(forKey: "InputRecord") as! [String : Any] //
// let historyDic : [String : Any] = UserDefaults.standard.value(forKey: "InputRecord") as! [String : Any]
let attributeString : NSMutableAttributedString = NSMutableAttributedString() //
// let attributeString : NSMutableAttributedString = NSMutableAttributedString()
let titleString = NSAttributedString(string: historyDic["title"] as! String, attributes: (SHInputViewModel.titleAttributes() as! [NSAttributedString.Key : Any])) //
attributeString.append(titleString) // let titleString = NSAttributedString(string: historyDic["title"] as! String, attributes: (SHInputViewModel.titleAttributes() as! [NSAttributedString.Key : Any]))
self.viewModel.title = titleString.string // attributeString.append(titleString)
//// self.viewModel.title = titleString.string
let contentString = NSAttributedString(string: historyDic["content"] as! String, attributes: SHInputViewModel.contentAttributes() as? [NSAttributedString.Key : Any]) // self.dataModel.rename = titleString.string
attributeString.append(contentString) //
// let contentString = NSAttributedString(string: historyDic["content"] as! String, attributes: SHInputViewModel.contentAttributes() as? [NSAttributedString.Key : Any])
let imageArray : Array = historyDic["imageArray"] as! Array<[String : Any]> // attributeString.append(contentString)
//
for imageDictionary : [String : Any] in imageArray // let imageArray : Array = historyDic["imageArray"] as! Array<[String : Any]>
{ //
let imageDataString : String = imageDictionary["imageData"] as! String // for imageDictionary : [String : Any] in imageArray
// {
let imageData : Data = NSData(base64Encoded: imageDataString, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)! as Data // let imageDataString : String = imageDictionary["imageData"] as! String
let image : UIImage = UIImage(data: imageData)! //
// let imageData : Data = NSData(base64Encoded: imageDataString, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)! as Data
// 调整图片大小, 宽相同, 高随比例变化 // let image : UIImage = UIImage(data: imageData)!
let newWidth = kScreenWidth - 20 * 2 //
let newHeight = newWidth * image.size.height / image.size.width // // 调整图片大小, 宽相同, 高随比例变化
let newSize = CGSize(width: newWidth, height: newHeight) // let newWidth = kScreenWidth - 20 * 2
// let newHeight = newWidth * image.size.height / image.size.width
let imageLocation : Int = imageDictionary["imageLocation"] as! Int // let newSize = CGSize(width: newWidth, height: newHeight)
//
// let imageLocation : Int = imageDictionary["imageLocation"] as! Int
let attachment = NSTextAttachment.init() //
attachment.image = image //
attachment.bounds = CGRect(origin: CGPoint.zero, size: newSize) // let attachment = NSTextAttachment.init()
let imageString = NSMutableAttributedString(attachment: attachment) // attachment.image = image
// attachment.bounds = CGRect(origin: CGPoint.zero, size: newSize)
// 设置图片文字属性 // let imageString = NSMutableAttributedString(attachment: attachment)
imageString.addAttributes((SHInputViewModel.contentAttributes() as? [NSAttributedString.Key : Any])!, range: NSRange(location: 0, length: imageString.length)) //
// 设置图片间距 // // 设置图片文字属性
let paragraphStyle = NSMutableParagraphStyle() // imageString.addAttributes((SHInputViewModel.contentAttributes() as? [NSAttributedString.Key : Any])!, range: NSRange(location: 0, length: imageString.length))
paragraphStyle.lineSpacing = 18 // // 设置图片间距
imageString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: imageString.length)) // let paragraphStyle = NSMutableParagraphStyle()
// paragraphStyle.lineSpacing = 18
attributeString.insert(imageString, at: imageLocation) // imageString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: imageString.length))
} //
// attributeString.insert(imageString, at: imageLocation)
self.shInputView.textView.attributedText = attributeString // }
} //
else // self.shInputView.textView.attributedText = attributeString
{ // }
// 默认文字样式 // else
self.shInputView.textView.typingAttributes = SHInputViewModel.titleAttributes() as! [NSAttributedString.Key : Any] // {
} // // 默认文字样式
// self.shInputView.textView.typingAttributes = SHInputViewModel.titleAttributes() as! [NSAttributedString.Key : Any]
// }
// 默认文字样式
self.shInputView.textView.typingAttributes = SHInputViewModel.titleAttributes() as! [NSAttributedString.Key : Any]
} }
// MARK: 设置控件位置 // MARK: 设置控件位置
...@@ -131,50 +147,56 @@ class SHInputController: SHBaseViewController ...@@ -131,50 +147,56 @@ class SHInputController: SHBaseViewController
// MARK: 点击navigationBar右侧按钮 // MARK: 点击navigationBar右侧按钮
@objc func didSelectedNavRightButton() @objc func didSelectedNavRightButton()
{ {
self.viewModel.imageArray = SHInputViewModel.getImageArrayWithTextView(textView: self.shInputView.textView) self.dataModel.recordingType = 1
self.dataModel.createDate = Date()
self.dataModel.modifyDate = Date()
let imageArray = SHInputViewModel.setUpImageArrayWithTextView(textView: self.shInputView.textView)
self.dataModel.imagesPath = imageArray.imagesPathArray
self.dataModel.imagesIndex = imageArray.imagesIndexArray
if self.dataModel.rename.length == 0
{
self.dataModel.rename = defaultTitle
}
self.dataModel.txt = self.shInputView.textView.text.substring(self.dataModel.rename.length, length: self.shInputView.textView.text.length - self.dataModel.rename.length)!
// 保存到沙盒
var recordList = CRUserDefaults.recordList
var i = 0;
// var folderModel = SHRecordFolderModel()
self.viewModel.content = self.shInputView.textView.text.substring(self.viewModel.title.length, length: self.shInputView.textView.text.length - self.viewModel.title.length)! for dict in recordList!
{
var model = SHRecordFolderModel()
model = getDataDictWith(dict: dict)
if model.id == self.folderModel.id
{
model.dataSources.append(self.dataModel)
self.folderModel = model
break
}
i += 1
}
var dic : [String : Any] = Dictionary() recordList![i] = getDictWith(obj: self.folderModel!)
dic.updateValue(self.viewModel.title, forKey: "title")
dic.updateValue(self.viewModel.content, forKey: "content")
dic.updateValue(self.viewModel.imageArray, forKey: "imageArray")
UserDefaults.standard.setValue(dic, forKey: "InputRecord") CRUserDefaults.recordList = recordList
UserDefaults.standard.synchronize()
self.saveSuccessCallBack?(self.folderModel)
self.navigationController?.popViewController(animated: true) self.navigationController?.popViewController(animated: true)
} }
// MARK: 获取textView中的图片数据, 保存成一个数组
// func getImageArrayWithTextView(textView : UITextView) -> Array<Any>
// {
// var imageArray = Array<Any>()
//
// let attributedString = textView.attributedText
//
// attributedString?.enumerateAttribute(NSAttributedString.Key.attachment, in: NSRange(location: 0, length: attributedString!.length), options: NSAttributedString.EnumerationOptions.longestEffectiveRangeNotRequired, using: { (attributes, range, stop) in
//
// var imageDictionary : Dictionary<String, Any> = Dictionary()
//
// if attributes != nil
// {
// let imageAttachment = attributes as! NSTextAttachment
//
// if imageAttachment.image != nil
// {
// imageDictionary.updateValue(UIImage.jpegData(imageAttachment.image!), forKey: "imageData")
// imageDictionary.updateValue(range.location, forKey: "imageLocation")
//
// imageArray.append(imageDictionary)
// }
// }
//
// })
//
// return imageArray
// }
// MARK: 点击键盘按钮 // MARK: 点击键盘按钮
@objc func didSelectedKeyboardButton(sender : UIButton) @objc func didSelectedKeyboardButton(sender : UIButton)
{ {
...@@ -203,7 +225,7 @@ extension SHInputController: UITextViewDelegate, SHInputTextViewDelegate ...@@ -203,7 +225,7 @@ extension SHInputController: UITextViewDelegate, SHInputTextViewDelegate
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool
{ {
// 获取标题文字位置 // 获取标题文字位置
let titleRange = NSRange(location: 0, length: self.viewModel.title.length) let titleRange = NSRange(location: 0, length: self.dataModel.rename.length)
// 在标题位置输入的内容为标题 // 在标题位置输入的内容为标题
if range.location <= titleRange.location + titleRange.length if range.location <= titleRange.location + titleRange.length
...@@ -224,7 +246,7 @@ extension SHInputController: UITextViewDelegate, SHInputTextViewDelegate ...@@ -224,7 +246,7 @@ extension SHInputController: UITextViewDelegate, SHInputTextViewDelegate
textViewString.insert(text, at: range.location) textViewString.insert(text, at: range.location)
// 保存标题文字 // 保存标题文字
self.viewModel.title = textViewString.substring(with: NSRange(location: 0, length: range.location + text.length)) self.dataModel.rename = textViewString.substring(with: NSRange(location: 0, length: range.location + text.length))
} }
} }
else else
...@@ -317,18 +339,21 @@ extension SHInputController: UIImagePickerControllerDelegate, UINavigationContro ...@@ -317,18 +339,21 @@ extension SHInputController: UIImagePickerControllerDelegate, UINavigationContro
let newSize = CGSize(width: newWidth, height: newHeight) let newSize = CGSize(width: newWidth, height: newHeight)
// 如果没有标题添加"未命名"作为标题 // 如果没有标题添加默认标题
if self.viewModel.title.length == 0 if self.dataModel.rename.length == 0
{ {
// 标题 // 标题
let titleString = NSMutableAttributedString(string: "未命名", attributes: (SHInputViewModel.titleAttributes() as! [NSAttributedString.Key : Any])) let titleString = NSMutableAttributedString(string: self.defaultTitle, attributes: (SHInputViewModel.titleAttributes() as! [NSAttributedString.Key : Any]))
self.shInputView.textView.attributedText = titleString self.shInputView.textView.attributedText = titleString
self.shInputView.placeholder.isHidden = true self.shInputView.placeholder.isHidden = true
self.viewModel.title = titleString.string self.dataModel.rename = titleString.string
} }
// 如果占位文字未隐藏, 隐藏占位文字
self.shInputView.placeholder.isHidden = true
// 前文 // 前文
let textString = NSMutableAttributedString(attributedString: self.shInputView.textView.attributedText) let textString = NSMutableAttributedString(attributedString: self.shInputView.textView.attributedText)
......
...@@ -11,12 +11,15 @@ import UIKit ...@@ -11,12 +11,15 @@ import UIKit
class SHInputViewModel: NSObject { class SHInputViewModel: NSObject {
// MARK: - property // MARK: - property
/// 标题文字 // /// 标题文字
var title : String = "" // var title : String = ""
/// 内容文字 // /// 内容文字
var content : String = "" // var content : String = ""
/// 图片数组 // /// 图片数组
var imageArray : Array = Array<Any>() // var imageArray : Array = Array<Any>()
// var recordModel : SHRecordModel = SHRecordModel()
...@@ -50,34 +53,79 @@ class SHInputViewModel: NSObject { ...@@ -50,34 +53,79 @@ class SHInputViewModel: NSObject {
return titleAttributes return titleAttributes
} }
// MARK: 获取图片数组 // MARK: 获取textView中的图片, 保存到本地, 并生成数组
class func getImageArrayWithTextView(textView : UITextView) -> Array<Any> class func setUpImageArrayWithTextView(textView: UITextView) -> (imagesPathArray: Array<String>, imagesIndexArray: Array<Int>)
{ {
var imageArray = Array<Any>() /// 图片路径数组
var imagesPathArray = Array<String>()
/// 图片位置数组
var imagesIndexArray = Array<Int>()
// 创建文件夹
/// 文件夹路径
let subFile = Date().format("yyyyMMdd")
let time = Date.init().timeStamp
let filePath = DocumentPath + "/" + subFile + time
do
{
try FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: true, attributes: nil)
}
catch let error
{
print(error)
return (imagesPathArray, imagesIndexArray)
}
// 遍历textView文字中的NSTextAttachment
let attributedString = textView.attributedText let attributedString = textView.attributedText
attributedString?.enumerateAttribute(NSAttributedString.Key.attachment, in: NSRange(location: 0, length: attributedString!.length), options: NSAttributedString.EnumerationOptions.longestEffectiveRangeNotRequired, using: { (attributes, range, stop) in attributedString?.enumerateAttribute(NSAttributedString.Key.attachment, in: NSRange(location: 0, length: attributedString!.length), options: NSAttributedString.EnumerationOptions.longestEffectiveRangeNotRequired, using: { (attributes, range, stop) in
var imageDictionary : Dictionary<String, Any> = Dictionary()
if attributes != nil if attributes != nil
{ {
let imageAttachment = attributes as! NSTextAttachment let imageAttachment = attributes as! NSTextAttachment
// 找到图片
if imageAttachment.image != nil if imageAttachment.image != nil
{ {
// 将图片保存在本地
// 把图片转成Data类型
let imageData : Data = (imageAttachment.image?.jpegData(compressionQuality: 1.0))! let imageData : Data = (imageAttachment.image?.jpegData(compressionQuality: 1.0))!
imageDictionary.updateValue(imageData.base64EncodedString(options: Data.Base64EncodingOptions.lineLength64Characters), forKey: "imageData") // 保存的本地路径
imageDictionary.updateValue(range.location, forKey: "imageLocation") let imagePath = filePath + "/" + "image.jpeg" + imagesPathArray.count.string
let imageUrl : URL = URL(fileURLWithPath: imagePath)
var saveSucceed : Bool = Bool(true)
// 将图片保存到沙盒
do
{
try imageData.write(to: imageUrl)
}
catch let error
{
saveSucceed = false
print(error)
}
imageArray.append(imageDictionary) if saveSucceed
{
// 保存成功后存储保存的路径
imagesPathArray.append(imagePath)
// 存储图片位置
imagesIndexArray.append(range.location)
}
} }
} }
}) })
return imageArray return (imagesPathArray, imagesIndexArray)
} }
} }
...@@ -346,7 +346,14 @@ extension SHRecordDetailsVC { ...@@ -346,7 +346,14 @@ extension SHRecordDetailsVC {
switch sender.tag { switch sender.tag {
case 200: case 200:
let input = SHInputController.init() let input = SHInputController.init()
input.folderModel = currentModel
self.navigationController?.pushViewController(input, animated: true) self.navigationController?.pushViewController(input, animated: true)
input.saveSuccessCallBack = { model in
self.currentModel = model
self.updateocloudUI()
}
break break
default: default:
let record = UIStoryboard.init(name: "Record", bundle: nil).instantiateViewController(withIdentifier: "SHRecordViewController") as! SHRecordViewController let record = UIStoryboard.init(name: "Record", bundle: nil).instantiateViewController(withIdentifier: "SHRecordViewController") as! SHRecordViewController
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment