当前位置: 首页 > 工具软件 > DTCoreText > 使用案例 >

DTCoreText 要点记录

万俟财
2023-12-01

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">一、DTCoreText 中的主要方法</span>

DTCoreText: 主要用于,富文本处理。自带了HTML标签解析,文本渲染(主要封装了CoreText)。

主要方法:

/**
 Initializes and returns a new `NSAttributedString` object from the HTML contained in the given object and base URL.
 
 Options can be:
 
 - DTMaxImageSize: the maximum CGSize that a text attachment can fill
 - DTDefaultFontFamily: the default font family to use instead of Times New Roman
 - DTDefaultFontName: the default font face to use instead of Times New Roman
 - DTDefaultFontSize: the default font size to use instead of 12
 - DTDefaultTextColor: the default text color
 - DTDefaultLinkColor: the default color for hyperlink text
 - DTDefaultLinkDecoration: the default decoration for hyperlinks
 - DTDefaultLinkHighlightColor: the color to show while the hyperlink is highlighted
 - DTDefaultTextAlignment: the default text alignment for paragraphs
 - DTDefaultLineHeightMultiplier: The multiplier for line heights
 - DTDefaultFirstLineHeadIndent: The default indent for left margin on first line
 - DTDefaultHeadIndent: The default indent for left margin except first line
 - DTDefaultListIndent: The amount by which lists are indented
 - DTDefaultStyleSheet: The default style sheet to use
 - DTUseiOS6Attributes: use iOS 6 attributes for building (UITextView compatible)
 - DTWillFlushBlockCallBack: a block to be executed whenever content is flushed to the output string
 - DTIgnoreInlineStylesOption: All inline style information is being ignored and only style blocks used
 
 @param data The data in HTML format from which to create the attributed string.
 @param options Specifies how the document should be loaded. Contains values described in NSAttributedString(HTML).
 @param docAttributes Currently not in use.
 @returns Returns an initialized object, or `nil` if the data can’t be decoded.
 */
- (id)initWithHTML:(NSData *)data options:(NSDictionary *)options documentAttributes:(NSDictionary * __autoreleasing*)docAttributes;

方法中:options 参数尤为重要,包含了 HTML 最大图片尺寸设定,字体,行间距,css格式,首行缩进...。


二、DTCoreText 中 问题汇总

1、锚点跳转问题。

在DTHTMLAttributedStringBuilder.m进行HTML的解析,在对锚点进行解析时,默认是关键字是 “name”:

		// the name attribute of A becomes an anchor_currentTag.anchorName = [_currentTag attributeForKey:@"name"];

但HTML通常是这种形式:

<a class="footnote-link" href="../Text/Section0012.xhtml#footnote-3018-12" id="footnote-3018-12-backlink">12</a>

这个时候:
_currentTag.anchorName = nil;

处理办法:
DTHTMLAttributedStringBuilder.m中:
_currentTag.anchorName = [_currentTag attributeForKey:@"id"];

DTHTMLElement.m 修改:

	if (_anchorName && _link)
	{
		[tmpDict setObject:_anchorName forKey:DTAnchorAttribute];
	}






 类似资料: