Moved hasAttachmentImage to a function.

This commit is contained in:
Kevin G Christiano 2019-09-03 10:07:15 -04:00
parent 38912a4f85
commit 62418f5132

View File

@ -19,9 +19,6 @@ public typealias ActionBlock = () -> ()
public var makeWholeViewClickable = false public var makeWholeViewClickable = false
/// Indication that attributed text attachment will affect range location of attributed text.
public var hasAttachmentImageInsideText = false
/// Set this property if you want updateView to update the font based on this standard and the size passed in. /// Set this property if you want updateView to update the font based on this standard and the size passed in.
public var standardFontSize: CGFloat = 0.0 public var standardFontSize: CGFloat = 0.0
@ -249,6 +246,7 @@ public typealias ActionBlock = () -> ()
case "strikethrough": case "strikethrough":
attributedString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.thick.rawValue, range: range) attributedString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.thick.rawValue, range: range)
attributedString.addAttribute(.baselineOffset, value: 0, range: range) attributedString.addAttribute(.baselineOffset, value: 0, range: range)
case "color": case "color":
if let colorHex = attribute.optionalStringForKey(KeyTextColor), !colorHex.isEmpty { if let colorHex = attribute.optionalStringForKey(KeyTextColor), !colorHex.isEmpty {
attributedString.removeAttribute(.foregroundColor, range: range) attributedString.removeAttribute(.foregroundColor, range: range)
@ -267,23 +265,9 @@ public typealias ActionBlock = () -> ()
} }
let mutableString = NSMutableAttributedString() let mutableString = NSMutableAttributedString()
let space = NSAttributedString(string: " ") mutableString.append(NSAttributedString(attachment: imageAttachment))
let attachment = NSAttributedString(attachment: imageAttachment)
if location == 0 {
mutableString.append(attachment)
mutableString.append(space)
} else {
mutableString.append(space)
mutableString.append(attachment)
}
attributedString.insert(mutableString, at: location) attributedString.insert(mutableString, at: location)
if location < attributedString.length {
(label as? Label)?.hasAttachmentImageInsideText = true
}
case "font": case "font":
if let fontStyle = attribute.optionalStringForKey("style") { if let fontStyle = attribute.optionalStringForKey("style") {
let styles = MFStyler.styleGetAttributedString("0", withStyle: fontStyle) let styles = MFStyler.styleGetAttributedString("0", withStyle: fontStyle)
@ -415,8 +399,8 @@ public typealias ActionBlock = () -> ()
} }
/** /**
Appends an external link image to the end of the attributed string. Appends an external link image to the end of the attributed string.
Will provide one whitespace to the left of the icon Will provide one whitespace to the left of the icon
*/ */
@objc public func appendExternalLinkIcon() { @objc public func appendExternalLinkIcon() {
@ -433,7 +417,7 @@ public typealias ActionBlock = () -> ()
Insert external link icon anywhere within text of Label. Insert external link icon anywhere within text of Label.
- Note: Each icon insertion adds 2 additional characters to the overall text length. - Note: Each icon insertion adds 2 additional characters to the overall text length.
This means that you MUST insert icons and links in the order they would appear. This means that you MUST insert icons and links in the order they would appear.
- parameter index: Location within the associated text to insert an external Link Icon - parameter index: Location within the associated text to insert an external Link Icon
*/ */
public func insertExternalLinkIcon(at index: Int) { public func insertExternalLinkIcon(at index: Int) {
@ -473,9 +457,9 @@ public typealias ActionBlock = () -> ()
DispatchQueue.global(qos: .default).async { DispatchQueue.global(qos: .default).async {
guard let url = URL(string: url), guard let url = URL(string: url),
let data = try? Data(contentsOf: url) let data = try? Data(contentsOf: url)
else { return } else { return }
imageAttachment.image = UIImage(data: data) imageAttachment.image = UIImage(data: data)
DispatchQueue.main.sync { DispatchQueue.main.sync {
@ -485,6 +469,23 @@ public typealias ActionBlock = () -> ()
return imageAttachment return imageAttachment
} }
/// Call to detect in the attributedText contains an NSTextAttachment.
func textContainsTextAttachment() -> Bool {
guard let attributedText = attributedText else { return false }
var containsAttachment = false
attributedText.enumerateAttribute(.attachment, in: NSRange(location: 0, length: attributedText.length), options: []) { value, range, stop in
if value is NSTextAttachment {
containsAttachment = true
return
}
}
return containsAttachment
}
} }
// MARK: - Atomization // MARK: - Atomization
@ -495,7 +496,6 @@ extension Label {
attributedText = nil attributedText = nil
textAlignment = .left textAlignment = .left
originalAttributedString = nil originalAttributedString = nil
hasAttachmentImageInsideText = false
styleB2(true) styleB2(true)
} }