Commit 15d02a8a authored by zhangzhe's avatar zhangzhe

no message

parent 7eca89d2
......@@ -54,63 +54,67 @@ class SHInputController: SHBaseViewController
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)
// 有记录显示记录
if UserDefaults.standard.value(forKey: "InputRecord") != nil
{
self.shInputView.placeholder.isHidden = true
let historyDic : [String : Any] = UserDefaults.standard.value(forKey: "InputRecord") as! [String : Any]
let attributeString : NSMutableAttributedString = NSMutableAttributedString()
let titleString = NSAttributedString(string: historyDic["title"] as! String, attributes: (SHInputViewModel.titleAttributes() as! [NSAttributedString.Key : Any]))
attributeString.append(titleString)
// self.viewModel.title = titleString.string
self.dataModel.rename = titleString.string
let contentString = NSAttributedString(string: historyDic["content"] as! String, attributes: SHInputViewModel.contentAttributes() as? [NSAttributedString.Key : Any])
attributeString.append(contentString)
let imageArray : Array = historyDic["imageArray"] as! Array<[String : Any]>
for imageDictionary : [String : Any] in imageArray
{
let imageDataString : String = imageDictionary["imageData"] as! String
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 imageLocation : Int = imageDictionary["imageLocation"] as! Int
let attachment = NSTextAttachment.init()
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()
paragraphStyle.lineSpacing = 18
imageString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: imageString.length))
attributeString.insert(imageString, at: imageLocation)
}
// TODO: 先做新增, 查看和编辑稍后再做
// // 有记录显示记录
// if UserDefaults.standard.value(forKey: "InputRecord") != nil
// {
// self.shInputView.placeholder.isHidden = true
//
// let historyDic : [String : Any] = UserDefaults.standard.value(forKey: "InputRecord") as! [String : Any]
//
// let attributeString : NSMutableAttributedString = NSMutableAttributedString()
//
// let titleString = NSAttributedString(string: historyDic["title"] as! String, attributes: (SHInputViewModel.titleAttributes() as! [NSAttributedString.Key : Any]))
// attributeString.append(titleString)
//// self.viewModel.title = titleString.string
// self.dataModel.rename = titleString.string
//
// let contentString = NSAttributedString(string: historyDic["content"] as! String, attributes: SHInputViewModel.contentAttributes() as? [NSAttributedString.Key : Any])
// attributeString.append(contentString)
//
// let imageArray : Array = historyDic["imageArray"] as! Array<[String : Any]>
//
// for imageDictionary : [String : Any] in imageArray
// {
// let imageDataString : String = imageDictionary["imageData"] as! String
//
// 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 imageLocation : Int = imageDictionary["imageLocation"] as! Int
//
//
// let attachment = NSTextAttachment.init()
// 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()
// paragraphStyle.lineSpacing = 18
// 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.typingAttributes = SHInputViewModel.titleAttributes() as! [NSAttributedString.Key : Any]
// }
self.shInputView.textView.attributedText = attributeString
}
else
{
// 默认文字样式
self.shInputView.textView.typingAttributes = SHInputViewModel.titleAttributes() as! [NSAttributedString.Key : Any]
}
}
// MARK: 设置控件位置
func setUpSubviewsLocation()
......@@ -151,6 +155,8 @@ class SHInputController: SHBaseViewController
CRUserDefaults.recordList = [modelDictionary]
self.navigationController?.popViewController(animated: true)
}
......
......@@ -11,14 +11,14 @@ import UIKit
class SHInputViewModel: NSObject {
// MARK: - property
/// 标题文字
var title : String = ""
/// 内容文字
var content : String = ""
/// 图片数组
var imageArray : Array = Array<Any>()
// /// 标题文字
// var title : String = ""
// /// 内容文字
// var content : String = ""
// /// 图片数组
// var imageArray : Array = Array<Any>()
var recordModel : SHRecordModel = SHRecordModel()
// var recordModel : SHRecordModel = SHRecordModel()
......@@ -53,8 +53,61 @@ class SHInputViewModel: NSObject {
return titleAttributes
}
// MARK: 获取图片数组
class func getImageArrayWithTextView(textView : UITextView) -> Array<Any>
// MARK: 获取textView中的图片, 保存到本地, 并生成数组
func setUpImageArrayWithTextView(textView: UITextView) -> (imagesPathArray: Array<String>, imagesIndexArray: Array<Int>)
{
/// 图片路径数组
var imagesPathArray = Array<String>()
/// 图片位置数组
var imagesIndexArray = Array<Int>()
/// 遍历textView文字中的NSTextAttachment
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
if attributes != nil
{
let imageAttachment = attributes as! NSTextAttachment
// 找到图片
if imageAttachment.image != nil
{
// 将图片保存在本地
// 把图片转成Data类型
let imageData : Data = (imageAttachment.image?.pngData())!
// 保存的本地路径
let subFile = Date().format("yyyyMMdd")
let time = Date.init().timeStamp
let imagePath = DocumentPath + "/" + subFile + time + "image.png"
let imageUrl : URL = URL(string: imagePath)!
// 将图片保存到沙盒
do
{
try imageData.write(to: imageUrl)
// 保存成功后存储保存的路径
imagesPathArray.append(imagePath)
// 存储图片位置
imagesIndexArray.append(range.location)
}
catch
{
}
}
}
})
return (imagesPathArray, imagesIndexArray)
}
class func setUpImageArrayWithTextView(textView : UITextView) -> Array<Any>
{
var imageArray = Array<Any>()
......
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