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)
@ -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)
} }