Our DNA is written in Swift

DTCoreText 1.6.1

For the most part this is a bug fix release on DTCoreText, but there are also two new little features in the demo app. This update was made possible by a generous sponsor.



  • FIXED: [DTHTMLWriter] did not output span styles (like background color) in fragment mode
  • FIXED: DTHTMLWriter did not output name of anchors if present
  • FIXED: DTHTMLWriter output base URL even if href property was nil
  • FIXED: DTTextAttachmentDrawing fixed to have non-flipped coordinate system
  • FIXED: Rotated images would show incorrectly oriented in DTTextAttachmentDrawing
  • ADDED [SPONSORED]: Method to determine string index based on touch coordinates
  • ADDED [SPONSORED]: DemoApp demonstrates getting the range of a tapped word
  • ADDED: DemoApp now has toggle to display output of DTHTMLWriter in fragment and document modes
  • CHANGED: Made processing of custom HTML Attributes optional, via new parsing option

The big feature added in 1.6.0 was processing of custom HTML attributes. Unfortunately this is also very detrimental to the parsing performance so I made this functionality optional. It is off by default because the majority of use cases would be to just parse and display HTML as attributed strings. For editors or special use cases you can still activate the processing of custom HTML attributes by adding the parse option DTProcessCustomHTMLAttributes with an @(YES) NSNumber.

One bug had been reported twice, a typo prevented DTHTMLWriter from outputting the span styles (like background color) and instead it would always output the paragraph style instead in fragment mode. Additional improvements related to anchors include adding the name of an anchor if present in the attributes and omitting the href if there was no URL present. Please note that you cannot have named anchors for a range with length 0 because NSAttributedStrings cannot have an attribute for ranges of less than 1 characters.

A friendly sponsor wanted to have a method to determine a word the user tapped on. I did have this method already implemented for DTRichTextEditor so I moved it into the DTCoreTextLayoutFrame (Cursor) category. There are now also convenience methods for the view classes that call through these methods.

 Determines the closest string index to a point in the receiver's frame.
 This can be used to find the cursor position to position an input caret at.
 @param point The point
 @returns The resulting string index
- (NSInteger)closestCursorIndexToPoint:(CGPoint)point;
 The rectangle to draw a caret for a given index
 @param index The string index for which to determine a cursor frame
 @returns The cursor rectangle
- (CGRect)cursorRectAtIndex:(NSInteger)index;

Note that this is not necessarily the character the user tapped on, but the index where a cursor would be placed. The second method provides the frame where you would place a caret for a given string index. This might need some additional work for right-to-left text.

The Demo App now logs the tapped word to the console, check out the code for handling the tap gesture to see how this is done.

Two things affect the way how text attachments are drawn. There are two loops through the glyph runs of a document: once with the original coordinate system of the layer and a second pass drawing the text which requires a flipped coordinate system. I moved the call for the DTTextAttachmentDrawing call to the first loop which remedies a problem where something drawn in there would be upside down. For images I switched to drawing them with UIKit instead of Quartz because UIKit automatically adjusts the image rotation based on the EXIF info.

The update is tagged on GitHub as well as published as a new CocoaPods spec.

Tagged as:

Categories: Updates