Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in / Register
Toggle navigation
S
ShorthandMaster
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lmj
ShorthandMaster
Commits
15d02a8a
Commit
15d02a8a
authored
Sep 27, 2020
by
zhangzhe
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
no message
parent
7eca89d2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
124 additions
and
65 deletions
+124
-65
SHInputController.swift
ShorthandMaster/Input/Controller/SHInputController.swift
+62
-56
SHInputViewModel.swift
ShorthandMaster/Input/ViewModel/SHInputViewModel.swift
+62
-9
No files found.
ShorthandMaster/Input/Controller/SHInputController.swift
View file @
15d02a8a
...
...
@@ -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
)
}
...
...
ShorthandMaster/Input/ViewModel/SHInputViewModel.swift
View file @
15d02a8a
...
...
@@ -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
>
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment