New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MessageLabel enabled detectors attributes #429
Comments
Seems to be fixed on an up to date master branch |
Not quite fixed it seems. In for detector in detectors {
guard let rangeTuples = rangesForDetectors[detector] else { return } should be for detector in detectors {
guard let rangeTuples = rangesForDetectors[detector] else { continue } |
Even after changing that, custom hyperlinks text color won't work. Please consider subclassing Sources: fileprivate class MessageLayoutManager: NSLayoutManager {
override func showCGGlyphs(_ glyphs: UnsafePointer<CGGlyph>,
positions: UnsafePointer<CGPoint>,
count glyphCount: Int,
font: UIFont,
matrix textMatrix: CGAffineTransform,
attributes: [NSAttributedStringKey : Any] = [:], in graphicsContext: CGContext) {
// NSLayoutManager has a hard coded internal color for hyperlinks which ignores
// NSForegroundColorAttributeName. To get around this, we force the fill color
// in the current context to match NSForegroundColorAttributeName.
if let foregroundColor = attributes[.foregroundColor] as? UIColor {
graphicsContext.setFillColor(foregroundColor.cgColor)
}
super.showCGGlyphs(glyphs,
positions: positions,
count: glyphCount,
font: font,
matrix: textMatrix,
attributes: attributes,
in: graphicsContext)
}
} |
@antoinelamy Good catch on the bug. Care to submit a PR? |
@antoinelamy , Did you implement the |
@zhongwuzw yes @SD10 of course I can submit a PR for this. |
Cool @antoinelamy. About subclassing |
@SD10 ,we don't needs subclass @antoinelamy , |
@SD10 yes you have to subclass @zhongwuzw it doesn't matter that your are using |
@antoinelamy , We don't use any like If you think the problem still existed, hope your |
In my case, I supply an attributed string with a |
@zhongwuzw would you like me to add the required code to support private func parse(text: NSAttributedString) -> [NSTextCheckingResult] {
guard enabledDetectors.isEmpty == false else { return [] }
let checkingTypes = enabledDetectors.reduce(0) { $0 | $1.textCheckingType.rawValue }
let detector = try? NSDataDetector(types: checkingTypes)
let range = NSRange(location: 0, length: text.length)
var parseResults: [NSTextCheckingResult] = []
parseResults.append(contentsOf: detector?.matches(in: text.string, options: [], range: range) ?? [])
parseResults.append(contentsOf: existingLinkAttributesResults(text: text))
return parseResults
}
private func existingLinkAttributesResults(text: NSAttributedString) -> [NSTextCheckingResult] {
var parseResults: [NSTextCheckingResult] = []
text.enumerateAttributes(in: NSRange(location: 0, length: text.length), options: []) { (attributes, range, stop) in
if let linkAttributeValue = attributes[NSAttributedStringKey.link] {
if let url = linkAttributeValue as? URL {
parseResults.append(NSTextCheckingResult.linkCheckingResult(range: range, url: url))
} else if let urlString = linkAttributeValue as? String, let url = URL(string: urlString) {
parseResults.append(NSTextCheckingResult.linkCheckingResult(range: range, url: url))
}
}
}
return parseResults
} |
@antoinelamy Emm, If we add support to |
In my case it works wonderfully. |
Sorry for the slow response. I have quite the backlog of things to do here. I'm unfamiliar with @antoinelamy Can you just use |
I'm going to close this because the bug was resolved, feel free to open an issue for |
General Information
MessageKit Version:
master
iOS Version(s):
all
Swift Version:
all
Devices/Simulators:
all
Reproducible in ChatExample? (Yes/No):
Yes
What happened?
MessageLabel's
defaultAttributes
assigned toaddressAttributes
,dateAttributes
,phoneNumberAttributes
andurlAttributes
are assigned beforetextColor
is set so all enabled detectors are always displayed in black.What did you expect to happen?
Detected links, addresses, etc should display by default in the same color than the regular text. I could be fixed by assigning attributes in
textColor
didSet closure.The text was updated successfully, but these errors were encountered: