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
...
@@ -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
)
}
}
...
...
ShorthandMaster/Input/ViewModel/SHInputViewModel.swift
View file @
15d02a8a
...
@@ -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
>
()
...
...
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