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

@ -16,11 +16,8 @@ public typealias ActionBlock = () -> ()
//------------------------------------------------------
// MARK: - General Properties
//------------------------------------------------------
public var makeWholeViewClickable = false
/// Indication that attributed text attachment will affect range location of attributed text.
public var hasAttachmentImageInsideText = false
public var makeWholeViewClickable = false
/// 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
@ -241,7 +238,7 @@ public typealias ActionBlock = () -> ()
else { continue }
let range = NSRange(location: location, length: length)
switch attributeType {
case "underline":
attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: range)
@ -249,6 +246,7 @@ public typealias ActionBlock = () -> ()
case "strikethrough":
attributedString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.thick.rawValue, range: range)
attributedString.addAttribute(.baselineOffset, value: 0, range: range)
case "color":
if let colorHex = attribute.optionalStringForKey(KeyTextColor), !colorHex.isEmpty {
attributedString.removeAttribute(.foregroundColor, range: range)
@ -267,23 +265,9 @@ public typealias ActionBlock = () -> ()
}
let mutableString = NSMutableAttributedString()
let space = NSAttributedString(string: " ")
let attachment = NSAttributedString(attachment: imageAttachment)
if location == 0 {
mutableString.append(attachment)
mutableString.append(space)
} else {
mutableString.append(space)
mutableString.append(attachment)
}
mutableString.append(NSAttributedString(attachment: imageAttachment))
attributedString.insert(mutableString, at: location)
if location < attributedString.length {
(label as? Label)?.hasAttachmentImageInsideText = true
}
case "font":
if let fontStyle = attribute.optionalStringForKey("style") {
let styles = MFStyler.styleGetAttributedString("0", withStyle: fontStyle)
@ -379,17 +363,17 @@ public typealias ActionBlock = () -> ()
attributedString.addAttribute(.font, value: fontObj.withSize(stylerSize) as Any, range: range)
}
}
// Loop the original attributed string, resize the image attachments.
originalAttributedString.enumerateAttribute(.attachment, in: NSRange(location: 0, length: originalAttributedString.length), options: []) { value, range, stop in
if let attachment = value as? NSTextAttachment,
let stylerSize = MFStyler.sizeObjectGeneric(forCurrentDevice: attachment.bounds.width)?.getValueBased(onSize: size) {
let dimension = round(stylerSize)
attachment.bounds = CGRect(x: 0, y: 0, width: dimension, height: dimension)
}
}
attributedText = attributedString
} else if !MVMCoreGetterUtility.fequal(a: Float(standardFontSize), b: 0.0), let sizeObject = sizeObject ?? MFStyler.sizeObjectGeneric(forCurrentDevice: standardFontSize) {
font = font.withSize(sizeObject.getValueBased(onSize: size))
@ -415,8 +399,8 @@ public typealias ActionBlock = () -> ()
}
/**
Appends an external link image to the end of the attributed string.
Will provide one whitespace to the left of the icon
Appends an external link image to the end of the attributed string.
Will provide one whitespace to the left of the icon
*/
@objc public func appendExternalLinkIcon() {
@ -433,13 +417,13 @@ public typealias ActionBlock = () -> ()
Insert external link icon anywhere within text of Label.
- 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
*/
public func insertExternalLinkIcon(at index: Int) {
guard let attributedText = attributedText, index <= attributedText.string.count && index >= 0 else { return }
let mutableString = NSMutableAttributedString(attributedString: attributedText)
mutableString.insert(NSAttributedString(string: " "), at: index)
mutableString.insert(NSAttributedString(attachment: Label.getTextAttachmentImage(dimension: font.pointSize)), at: index + (index == 0 ? 0 : 1))
@ -473,10 +457,10 @@ public typealias ActionBlock = () -> ()
DispatchQueue.global(qos: .default).async {
guard let url = URL(string: url),
let data = try? Data(contentsOf: url)
else { return }
guard let url = URL(string: url),
let data = try? Data(contentsOf: url)
else { return }
imageAttachment.image = UIImage(data: data)
DispatchQueue.main.sync {
label.setNeedsDisplay()
@ -485,6 +469,23 @@ public typealias ActionBlock = () -> ()
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
@ -495,7 +496,6 @@ extension Label {
attributedText = nil
textAlignment = .left
originalAttributedString = nil
hasAttachmentImageInsideText = false
styleB2(true)
}
@ -625,7 +625,7 @@ extension UITapGestureRecognizer {
let stagedAttributedString = NSMutableAttributedString(attributedString: attributedText)
stagedAttributedString.addAttributes([NSAttributedString.Key.paragraphStyle: paragraph], range: NSRange(location: 0, length: attributedText.string.count))
let textStorage = NSTextStorage(attributedString: stagedAttributedString)
let layoutManager = NSLayoutManager()
let textContainer = NSTextContainer(size: .zero)