Moved hasAttachmentImage to a function.
This commit is contained in:
parent
38912a4f85
commit
62418f5132
@ -16,11 +16,8 @@ public typealias ActionBlock = () -> ()
|
|||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
// MARK: - General Properties
|
// MARK: - General Properties
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
|
|
||||||
public var makeWholeViewClickable = false
|
|
||||||
|
|
||||||
/// Indication that attributed text attachment will affect range location of attributed text.
|
public var makeWholeViewClickable = false
|
||||||
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
|
||||||
@ -241,7 +238,7 @@ public typealias ActionBlock = () -> ()
|
|||||||
else { continue }
|
else { continue }
|
||||||
|
|
||||||
let range = NSRange(location: location, length: length)
|
let range = NSRange(location: location, length: length)
|
||||||
|
|
||||||
switch attributeType {
|
switch attributeType {
|
||||||
case "underline":
|
case "underline":
|
||||||
attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: range)
|
attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: range)
|
||||||
@ -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)
|
||||||
@ -379,17 +363,17 @@ public typealias ActionBlock = () -> ()
|
|||||||
attributedString.addAttribute(.font, value: fontObj.withSize(stylerSize) as Any, range: range)
|
attributedString.addAttribute(.font, value: fontObj.withSize(stylerSize) as Any, range: range)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loop the original attributed string, resize the image attachments.
|
// Loop the original attributed string, resize the image attachments.
|
||||||
originalAttributedString.enumerateAttribute(.attachment, in: NSRange(location: 0, length: originalAttributedString.length), options: []) { value, range, stop in
|
originalAttributedString.enumerateAttribute(.attachment, in: NSRange(location: 0, length: originalAttributedString.length), options: []) { value, range, stop in
|
||||||
if let attachment = value as? NSTextAttachment,
|
if let attachment = value as? NSTextAttachment,
|
||||||
let stylerSize = MFStyler.sizeObjectGeneric(forCurrentDevice: attachment.bounds.width)?.getValueBased(onSize: size) {
|
let stylerSize = MFStyler.sizeObjectGeneric(forCurrentDevice: attachment.bounds.width)?.getValueBased(onSize: size) {
|
||||||
|
|
||||||
let dimension = round(stylerSize)
|
let dimension = round(stylerSize)
|
||||||
attachment.bounds = CGRect(x: 0, y: 0, width: dimension, height: dimension)
|
attachment.bounds = CGRect(x: 0, y: 0, width: dimension, height: dimension)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
attributedText = attributedString
|
attributedText = attributedString
|
||||||
} else if !MVMCoreGetterUtility.fequal(a: Float(standardFontSize), b: 0.0), let sizeObject = sizeObject ?? MFStyler.sizeObjectGeneric(forCurrentDevice: standardFontSize) {
|
} else if !MVMCoreGetterUtility.fequal(a: Float(standardFontSize), b: 0.0), let sizeObject = sizeObject ?? MFStyler.sizeObjectGeneric(forCurrentDevice: standardFontSize) {
|
||||||
font = font.withSize(sizeObject.getValueBased(onSize: size))
|
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.
|
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,13 +417,13 @@ 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) {
|
||||||
|
|
||||||
guard let attributedText = attributedText, index <= attributedText.string.count && index >= 0 else { return }
|
guard let attributedText = attributedText, index <= attributedText.string.count && index >= 0 else { return }
|
||||||
|
|
||||||
let mutableString = NSMutableAttributedString(attributedString: attributedText)
|
let mutableString = NSMutableAttributedString(attributedString: attributedText)
|
||||||
mutableString.insert(NSAttributedString(string: " "), at: index)
|
mutableString.insert(NSAttributedString(string: " "), at: index)
|
||||||
mutableString.insert(NSAttributedString(attachment: Label.getTextAttachmentImage(dimension: font.pointSize)), at: index + (index == 0 ? 0 : 1))
|
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 {
|
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 {
|
||||||
label.setNeedsDisplay()
|
label.setNeedsDisplay()
|
||||||
@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -625,7 +625,7 @@ extension UITapGestureRecognizer {
|
|||||||
|
|
||||||
let stagedAttributedString = NSMutableAttributedString(attributedString: attributedText)
|
let stagedAttributedString = NSMutableAttributedString(attributedString: attributedText)
|
||||||
stagedAttributedString.addAttributes([NSAttributedString.Key.paragraphStyle: paragraph], range: NSRange(location: 0, length: attributedText.string.count))
|
stagedAttributedString.addAttributes([NSAttributedString.Key.paragraphStyle: paragraph], range: NSRange(location: 0, length: attributedText.string.count))
|
||||||
|
|
||||||
let textStorage = NSTextStorage(attributedString: stagedAttributedString)
|
let textStorage = NSTextStorage(attributedString: stagedAttributedString)
|
||||||
let layoutManager = NSLayoutManager()
|
let layoutManager = NSLayoutManager()
|
||||||
let textContainer = NSTextContainer(size: .zero)
|
let textContainer = NSTextContainer(size: .zero)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user