Latest changes to account for variable icon locations and text alignments.

This commit is contained in:
Kevin G Christiano 2019-08-14 12:58:46 -04:00
parent 7c93506d87
commit abde89f06f

View File

@ -20,7 +20,7 @@ public typealias ActionBlock = () -> ()
public var makeWholeViewClickable = false
/// Indication that attributed text attachment will affect range location of attributed text.
fileprivate var hasAttachmentImageInsideText = 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.
public var standardFontSize: CGFloat = 0.0
@ -126,6 +126,7 @@ public typealias ActionBlock = () -> ()
return label
}
/// H32 -> Legal
@objc public static func commonLabelH32(_ scale: Bool) -> Label {
let label = Label.label()
label.styleH32(scale)
@ -153,12 +154,14 @@ public typealias ActionBlock = () -> ()
return label
}
/// B20 -> Legal
@objc public static func commonLabelB20(_ scale: Bool) -> Label {
let label = Label.label()
label.styleB20(scale)
return label
}
/// Default
@objc open class func label() -> Label {
return Label(frame: .zero)
}
@ -556,35 +559,11 @@ extension Label {
@objc private func textLinkTapped(_ gesture: UITapGestureRecognizer) {
var offset = 0
var attachmentLocations: [Int] = []
var attachmentIndex = 0
// Used to identify all image attachments in the label.
if hasAttachmentImageInsideText {
attributedText?.enumerateAttribute(.attachment, in: NSRange(location: 0, length: attributedText!.length), options: []) { value, range, stop in
guard value is NSTextAttachment else { return }
attachmentLocations.append(range.location)
if range.location == 0 {
offset = 2
}
}
}
for clause in clauses {
guard let range = clause.range else { return }
// Must increment an offset becuase every image added to text interior increases text length by 2.
if hasAttachmentImageInsideText {
if range.location >= attachmentLocations[attachmentIndex] - range.length {
offset += 2
attachmentIndex += 1
}
}
// This determines if we tapped on the desired range of text.
if gesture.didTapAttributedTextInLabel(self, inRange: range, offset: offset) {
if gesture.didTapAttributedTextInLabel(self, inRange: range) {
clause.performAction()
return
}
@ -595,7 +574,7 @@ extension Label {
// MARK: -
extension UITapGestureRecognizer {
func didTapAttributedTextInLabel(_ label: Label, inRange targetRange: NSRange, offset: Int) -> Bool {
func didTapAttributedTextInLabel(_ label: Label, inRange targetRange: NSRange) -> Bool {
// There would only ever be one clause to act on.
if label.makeWholeViewClickable {
@ -603,8 +582,14 @@ extension UITapGestureRecognizer {
}
guard let attributedText = label.attributedText else { return false }
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = label.textAlignment
let newAttributedString = NSMutableAttributedString(attributedString: attributedText)
newAttributedString.addAttributes([NSAttributedString.Key.paragraphStyle: paragraph], range: NSRange(location: 0, length: attributedText.string.count))
let textStorage = NSTextStorage(attributedString: attributedText)
let textStorage = NSTextStorage(attributedString: newAttributedString)
let layoutManager = NSLayoutManager()
let textContainer = NSTextContainer(size: .zero)
@ -618,12 +603,6 @@ extension UITapGestureRecognizer {
let indexOfGlyph = layoutManager.glyphIndex(for: location(in: label), in: textContainer)
// If text attachment was inserted then extra indices must be accounted for.
if label.hasAttachmentImageInsideText {
let location = targetRange.location + offset
return NSLocationInRange(indexOfGlyph, NSRange(location: location, length: targetRange.length))
}
return NSLocationInRange(indexOfGlyph, targetRange)
}
}