Images being flipped when adding to NSAttributedString
I figured it out and it was so much simpler than I was making it.
Because the image was in a NSAttribuetdString being appended into a NSTextView I didn't need to resize each image in the NSAttributedString, rather I just had to set the attachment scaling inside the NSTextView with
markdown.layoutManager?.defaultAttachmentScaling = NSImageScaling.scaleProportionallyDown
One line is all it took
Vertical images being rotated horizontally when adding and saving to NSAttributedString in IOS?
figure out the rotation before I save the image
func rotateImage(image: UIImage) -> UIImage? {
if image.imageOrientation == UIImage.Orientation.up {
return image /// already upright, no need for changes
}
UIGraphicsBeginImageContext(image.size)
image.draw(in: CGRect(origin: CGPoint.zero, size: image.size))
let copy = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return copy
}
use
let originalImage = pickImage
attachment.image! = rotateImage(image: originalImage!)!
Center NSTextAttachment image next to single line UILabel
You can change the rect by subclassing NSTextAttachment
and overriding attachmentBoundsForTextContainer:proposedLineFragment:glyphPosition:characterIndex:
. Example:
- (CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex {
CGRect bounds;
bounds.origin = CGPointMake(0, -5);
bounds.size = self.image.size;
return bounds;
}
It's not a perfect solution. You have to figure out the Y-origin “by eye” and if you change the font or the icon size, you'll probably want to change the Y-origin. But I couldn't find a better way, except by putting the icon in a separate image view (which has its own disadvantages).
NSImage drawInRect:fromRect:operation:fraction results in a flipped image
Use the method
- (void)drawInRect:(NSRect)dstSpacePortionRect
fromRect:(NSRect)srcSpacePortionRect
operation:(NSCompositingOperation)op
fraction:(CGFloat)requestedAlpha
respectFlipped:(BOOL)respectContextIsFlipped
hints:(NSDictionary *)hints
along with - (void)lockFocusFlipped:(BOOL)flipped
to draw the image upside down. From the NSImage Reference manual.
replace NSTextAttachment that include an image to String
Umm.. I solved this problem.
First : Count number of NSTextAttachment
var count = 0
self.attributedText.enumerateAttribute(NSAttachmentAttributeName, in : NSMakeRange(0, self.attributedText.length), options: [], using: { attribute, range, _ in
if let attachment = attribute as? NSTextAttachment,
let image = attachment.image{
count = count + 1
}
})
return count
Second : Replace NSTextAttachment with String and calculate the changed range. <- Repeat
for i in 0..<self.countOfNSTextAttachment(){
let attributedString = NSMutableAttributedString(attributedString: self.attributedText)
var count = 0
attributedString.enumerateAttribute(NSAttachmentAttributeName, in : NSMakeRange(0, attributedString.length), options: [], using: { attribute, range, _ in
if let attachment = attribute as? NSTextAttachment,
let image = attachment.image{
let str = "[img src=\(image.accessibilityIdentifier!)]"
if count == 0{
attributedString.beginEditing()
attributedString.replaceCharacters(in: range, with: NSAttributedString(string : str))
attributedString.endEditing()
self.attributedText = attributedString
}else{
return
}
count = count + 1
}
})
}
return self.attributedText.string
Result : result
Perfect!!
Related Topics
Why Does a Function Have Long-Term Write Access to All of Its In-Out Parameters
Convert Input Data to Integer in Swift
Swift Bug? Calling Super Class Method When Subclass with Generic Type
List Scroll Freeze on Catalyst Navigationview
Swift Auto Completion Not Working in Xcode 6 Beta
Mapview Shows White Overlay on Interaction
Error Domain=Nscocoaerrordomain Code=3840 "Invalid Value Around Character 0
Swiftui: Make Remaining Area of List Item Detect Clicks
Realm Write Transaction Failing, Despite Being in Transaction
Swift Loaditem Closure Not Running
How to Get a Full List of Running Processes
Extract Reality Composer Scene for Arquicklook
How to Use This Fetchrequest() in Swift