Commit 15d02a8a authored by zhangzhe's avatar zhangzhe

no message

parent 7eca89d2
...@@ -54,63 +54,67 @@ class SHInputController: SHBaseViewController ...@@ -54,63 +54,67 @@ class SHInputController: SHBaseViewController
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.dataModel.rename = titleString.string //// 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 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]> //
// let imageArray : Array = historyDic["imageArray"] as! Array<[String : Any]>
for imageDictionary : [String : Any] in imageArray //
{ // for imageDictionary : [String : Any] in imageArray
let imageDataString : String = imageDictionary["imageData"] as! String // {
// 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 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 newWidth = kScreenWidth - 20 * 2
let newSize = CGSize(width: newWidth, height: newHeight) // let newHeight = newWidth * image.size.height / image.size.width
// let newSize = CGSize(width: newWidth, height: newHeight)
let imageLocation : Int = imageDictionary["imageLocation"] as! Int //
// let imageLocation : Int = imageDictionary["imageLocation"] as! Int
//
let attachment = NSTextAttachment.init() //
attachment.image = image // let attachment = NSTextAttachment.init()
attachment.bounds = CGRect(origin: CGPoint.zero, size: newSize) // attachment.image = image
let imageString = NSMutableAttributedString(attachment: attachment) // 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)) // // 设置图片文字属性
// 设置图片间距 // imageString.addAttributes((SHInputViewModel.contentAttributes() as? [NSAttributedString.Key : Any])!, range: NSRange(location: 0, length: imageString.length))
let paragraphStyle = NSMutableParagraphStyle() // // 设置图片间距
paragraphStyle.lineSpacing = 18 // let paragraphStyle = NSMutableParagraphStyle()
imageString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: imageString.length)) // paragraphStyle.lineSpacing = 18
// imageString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: imageString.length))
attributeString.insert(imageString, at: imageLocation) //
} // 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] self.shInputView.textView.typingAttributes = SHInputViewModel.titleAttributes() as! [NSAttributedString.Key : Any]
} }
}
// MARK: 设置控件位置 // MARK: 设置控件位置
func setUpSubviewsLocation() func setUpSubviewsLocation()
...@@ -151,6 +155,8 @@ class SHInputController: SHBaseViewController ...@@ -151,6 +155,8 @@ class SHInputController: SHBaseViewController
CRUserDefaults.recordList = [modelDictionary] CRUserDefaults.recordList = [modelDictionary]
self.navigationController?.popViewController(animated: true) self.navigationController?.popViewController(animated: true)
} }
......
...@@ -11,14 +11,14 @@ import UIKit ...@@ -11,14 +11,14 @@ 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() // var recordModel : SHRecordModel = SHRecordModel()
...@@ -53,8 +53,61 @@ class SHInputViewModel: NSObject { ...@@ -53,8 +53,61 @@ class SHInputViewModel: NSObject {
return titleAttributes return titleAttributes
} }
// MARK: 获取图片数组 // MARK: 获取textView中的图片, 保存到本地, 并生成数组
class func getImageArrayWithTextView(textView : UITextView) -> Array<Any> 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>() 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