Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions Aztec/Classes/TextKit/MediaAttachment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ open class MediaAttachment: NSTextAttachment {
/// Identifier used to match this attachment with a custom UIView subclass
///
private(set) open var identifier = String()

/// Attachment URL
///
fileprivate(set) public var url: URL?
Expand Down Expand Up @@ -204,7 +204,7 @@ open class MediaAttachment: NSTextAttachment {
// MARK: - NSTextAttachmentContainer

override open func image(forBounds imageBounds: CGRect, textContainer: NSTextContainer?, characterIndex charIndex: Int) -> UIImage? {

ensureImageIsUpToDate(in: textContainer)

guard let image = image else {
Expand All @@ -223,7 +223,7 @@ open class MediaAttachment: NSTextAttachment {
func mediaBounds(for bounds: CGRect) -> CGRect {
let containerWidth = bounds.size.width
let origin = CGPoint(x: xPosition(forContainerWidth: bounds.size.width), y: appearance.imageMargin)
let size = CGSize(width: onScreenWidth(containerWidth), height: onScreenHeight(containerWidth) - appearance.imageMargin)
let size = CGSize(width: onScreenWidth(containerWidth), height: onScreenHeight(containerWidth) - appearance.imageMargin * 2)
return CGRect(origin: origin, size: size)
}

Expand Down Expand Up @@ -321,22 +321,31 @@ open class MediaAttachment: NSTextAttachment {
/// Otherwise, we'll always take the whole container's width.
///
override open func attachmentBounds(for textContainer: NSTextContainer?, proposedLineFragment lineFrag: CGRect, glyphPosition position: CGPoint, characterIndex charIndex: Int) -> CGRect {

ensureImageIsUpToDate(in: textContainer)

if image == nil {
return .zero
}

var padding = (textContainer?.lineFragmentPadding ?? 0)
var padding = (textContainer?.lineFragmentPadding ?? 0) * 2
if let storage = textContainer?.layoutManager?.textStorage,
let paragraphStyle = storage.attribute(.paragraphStyle, at: charIndex, effectiveRange: nil) as? NSParagraphStyle {
padding += paragraphStyle.firstLineHeadIndent + paragraphStyle.tailIndent
let attachmentString = storage.attributedSubstring(from: NSMakeRange(charIndex, 1)).string
let headIndent = storage.string.isStartOfParagraph(at: attachmentString.startIndex) ? paragraphStyle.firstLineHeadIndent : paragraphStyle.headIndent

padding += headIndent

if paragraphStyle.tailIndent > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why do we need this IF, in what scenarios the tailIdent can be positive or negative?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is Discussion of tailIndent from apple:

If positive, this value is the distance from the leading margin (for example, the left margin in left-to-right text). If 0 or negative, it’s the distance from the trailing margin.

For example, a paragraph style designed to fit exactly in a 2-inch wide container has a head indent of 0.0 and a tail indent of 0.0. One designed to fit with a quarter-inch margin has a head indent of 0.25 and a tail indent of –0.25.

Copy link
Contributor

@SergioEstevao SergioEstevao Nov 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So could we rewrite like this:
padding += abs(paragraphStyle.tailIndent) + abs(headIndent)

padding += lineFrag.width - headIndent - paragraphStyle.tailIndent
} else if paragraphStyle.tailIndent < 0 {
padding -= paragraphStyle.tailIndent
}
}
let width = floor(lineFrag.width - (padding * 2))
let width = floor(lineFrag.width - padding)

let size = CGSize(width: width, height: onScreenHeight(width))

return CGRect(origin: CGPoint.zero, size: size)
}
}
Expand Down Expand Up @@ -384,7 +393,6 @@ private extension MediaAttachment {
self.invalidateLayout(in: textContainer)

}, onFailure: { [weak self] () in

self?.isFetchingImage = false
})
}
Expand Down Expand Up @@ -479,3 +487,4 @@ extension MediaAttachment {
public var imageMargin = CGFloat(10.0)
}
}