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
Apply color on image template #105
Comments
@aleufms thanks for your report! Would you be interested in submitting a pull request to fix it? Writing a unit test that demonstrates the failure is a good first step, although in this case, determining that the resulting string contains tinted images could be tricky. Let me know if you'd like to try - I'm more than happy to help out if you'd like. |
Ok, sorry for the delay. I've been upload an example on https://github.com/aleufms/BonMotImageExample |
TL;DR you found a bug or secret feature in UIKit. Scroll to the bottom for my recommendation of what you should do. OK, so, you've stumbled upon what is, as far as I can tell, either a bug or an undocumented feature of let attachment = NSTextAttachment()
// image must be set to Template rendering mode
attachment.image = UIImage(named: "ic_navigate_next")
// Need to use a space, or this bug/feature isn't activated
let attributedString = NSMutableAttributedString(string: " ")
let attachmentString = NSAttributedString(attachment: attachment)
attributedString.appendAttributedString(attachmentString)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.orangeColor(), range: NSMakeRange(0, 2))
label.attributedText = attributedString This will result in a colored image: (The original image was black.) If you do just an image, without a space, you don't get color: let attachment = NSTextAttachment()
attachment.image = UIImage(named: "ic_navigate_next")
let attachmentString = NSAttributedString(attachment: attachment).mutableCopy() as! NSMutableAttributedString
attachmentString.addAttribute(NSForegroundColorAttributeName, value: UIColor.orangeColor(), range: NSMakeRange(0, 1))
label.attributedText = attachmentString I didn't know about this behavior until you posted this bug, and it's not supported in BonMot - see #21. I wonder if there's a way, in BonMot, to prevent it, so people are not confused in the future? For example, we could force the image to always render in Original mode in - (NSAttributedString *)attributedStringLastConcatenant:(BOOL)lastConcatenant
{
NSMutableAttributedString *mutableAttributedString = nil;
if (self.image) {
NSAssert(!self.string, @"If self.image is non-nil, self.string must be nil");
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [self.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; @aleufms for your use case, I recommend pre-tinting your image using |
Acceptance criteria for BonMot 4.0:
|
While commenting the method, I thought that we might want to support background color. Does that make sense to you? |
Background color for the image? Wouldn't NSBackgroundColorAttributeName take care of that? |
Yea, not sure what I was thinking there. Carry on! On Sunday, October 2, 2016, Zev Eisenberg notifications@github.com wrote:
|
In the upcoming 4.0 release, images set to "automatic" or "always template" will now be tinted if they are styled with a color. |
The red color only is applied on the lastimage. The first image on the chain remains black.
The text was updated successfully, but these errors were encountered: