200506--iOS之NSAttributedString类

2023-05-16

Class

NSAttributedString

A string that has associated attributes (such as visual style, hyperlinks, or accessibility data) for portions of its text.

          --一种字符串,它的部分文本具有相关属性(如视觉样式、超链接或辅助功能数据)。

Declaration

class NSAttributedString : NSObject

Overview      --概览

An NSAttributedString object manages character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string. An association of characters and their attributes is called an attributed string. The cluster’s two public classes, NSAttributedString and NSMutableAttributedString, declare the programmatic interface for read-only attributed strings and modifiable attributed strings, respectively.

     NSAttributedString对象管理字符串与字符串相关的属性集(例如,字体和字距),这些属性应用于字符串中的单个字符某范围内的字符。字符及其关联的属性称为带属性的字符串。集群的两个公共类 NSAttributedStringNSMutableAttributedString 分别声明只读带属性字符串可修改带属性字符串的编程接口。

An attributed string identifies attributes by name, using an NSDictionary object to store a value under the given name. You can assign any attribute name/value pair you wish to a range of characters—it is up to your application to interpret custom attributes (see Attributed String Programming Guide). If you are using attributed strings with the Core Text framework, you can also use the attribute keys defined by that framework.

          --属性字符串按名称标识属性,使用NSDictionary对象给定名称下存储值。您可以将想要的属性的 名/值 对 分配给某范围内的字符,由app来解释自定义属性(请参阅Attributed String Programming Guide)。如果在核心文本框架中使用属性字符串,也可以使用该框架定义的属性键。

You use attributed strings with any APIs that accept them, such as Core Text. The AppKit and UIKit frameworks also provide a subclass of NSMutableAttributedString, called NSTextStorage, to provide the storage for the extended text-handling system. In iOS 6 and later you can use attributed strings to display formatted text in text views, text fields, and some other controls. Both AppKit and UIKit also define extensions to the basic attributed string interface that allows you to draw their contents in the current graphic context.

        --你可以将属性字符串与接受它们的api(如Core Text)一起使用。AppKit和UIKit框架还提供了NSMutableAttributedString的一个子类,称为NSTextStorage,为扩展文本处理系统提供存储。在iOS 6和更高版本中,可以使用属性化字符串在text views, text fields和一些其他控件中显示格式化文本AppKitUIKit还定义了基本属性字符串接口的extensions,该接口允许您在当前图形上下文中绘制它们的内容

The default font for NSAttributedString objects is Helvetica 12-point, which may differ from the default system font for the platform. Thus, you might want to create new strings with non-default attributes suitable for your application. You can also use the NSParagraphStyle class and its subclass NSMutableParagraphStyle to encapsulate the paragraph or ruler attributes used by the NSAttributedString classes.

      --NSAttributedString对象的默认字体Helvetica 12-point,,它可能与平台的默认系统字体不同。因此,您可能希望创建没有默认属性的新字符串。那么,你可以使用NSParagraphStyle类及其子类NSMutableParagraphStyle来封装NSAttributedString类使用的段落标尺属性。

Be aware that comparisons of NSAttributedString objects using the isEqual(_:) method look for exact equality. The comparison includes both a character-by-character string equality check and an equality check of all attributes. Such a comparison is not likely to yield a match if the string has many attributes, such as attachments, lists, and tables, for example.

      --请注意,使用isEqual(:)方法对NSAttributedString对象进行比较会寻找完全相等的值。比较包括逐个字符串的相等检查和所有属性的相等检查。如果字符串具有许多属性(例如附件、列表和表),则这样的比较 可能。产生不匹配。

The NSAttributedString class is “toll-free bridged” with its Core Foundation counterpart, CFAttributedString. See Toll-Free Bridging for more information.

      --NSAttributedString类与其核心基础对应项CFAttributedString是“免费桥接”的。有关详细信息,请参阅免费桥接。

 

Topics            --专题

Creating an NSAttributedString Object           --创建一个NSAttributedString对象

init(string: String)

Returns an NSAttributedString object initialized with the characters of a given string and no attribute information.

init(string: String, attributes: [NSAttributedString.Key : Any]?)

Returns an NSAttributedString object initialized with a given string and attributes.

init(attributedString: NSAttributedString)

Returns an NSAttributedString object initialized with the characters and attributes of another given attributed string.

init(data: Data, options: [NSAttributedString.DocumentReadingOptionKey : Any], documentAttributes: AutoreleasingUnsafeMutablePointer<NSDictionary?>?)

Initializes and returns a new attributed string object from the data contained in the given data object.

init?(docFormat: Data, documentAttributes: AutoreleasingUnsafeMutablePointer<NSDictionary?>?)

Initializes and returns a new NSAttributedString object from Microsoft Word format data contained in the given NSData object.

init?(html: Data, documentAttributes: AutoreleasingUnsafeMutablePointer<NSDictionary?>?)

Initializes and returns a new NSAttributedString object from HTML contained in the given data object.

init?(html: Data, baseURL: URL, documentAttributes: AutoreleasingUnsafeMutablePointer<NSDictionary?>?)

Initializes and returns a new NSAttributedString object from the HTML contained in the given object and base URL.

init?(html: Data, options: [NSAttributedString.DocumentReadingOptionKey : Any], documentAttributes: AutoreleasingUnsafeMutablePointer<NSDictionary?>?)

Initializes and returns a new attributed string object from HTML contained in the given data object.

init?(rtf: Data, documentAttributes: AutoreleasingUnsafeMutablePointer<NSDictionary?>?)

Initializes a new attributed string object by decoding the stream of RTF commands and data contained in the given data object.

init?(rtfd: Data, documentAttributes: AutoreleasingUnsafeMutablePointer<NSDictionary?>?)

Initializes a new attributed string object by decoding the stream of RTFD commands and data contained in the given data object.

init?(rtfdFileWrapper: FileWrapper, documentAttributes: AutoreleasingUnsafeMutablePointer<NSDictionary?>?)

Initializes a new attributed string object from the specified file wrapper containing an RTFD document.

init(url: URL, options: [NSAttributedString.DocumentReadingOptionKey : Any], documentAttributes: AutoreleasingUnsafeMutablePointer<NSDictionary?>?)

Initializes a new NSAttributedString object from the contents of the given URL.

init(attachment: NSTextAttachment)

Creates an attributed string with an attachment.

 

Retrieving Character Information        --获取字符信息

var string: String

The character contents of the receiver as an NSString object.

var length: Int

The length of the receiver’s string object.

 

Retrieving Attribute Information       --获取字符属性信息

func attributes(at: Int, effectiveRange: NSRangePointer?) -> [NSAttributedString.Key : Any]

Returns the attributes for the character at a given index.

func attributes(at: Int, longestEffectiveRange: NSRangePointer?, in: NSRange) -> [NSAttributedString.Key : Any]

Returns the attributes for the character at a given index, and by reference the range over which the attributes apply.

func attribute(NSAttributedString.Key, at: Int, effectiveRange: NSRangePointer?) -> Any?

Returns the value for an attribute with a given name of the character at a given index, and by reference the range over which the attribute applies.

func attribute(NSAttributedString.Key, at: Int, longestEffectiveRange: NSRangePointer?, in: NSRange) -> Any?

Returns the value for the attribute with a given name of the character at a given index, and by reference the range over which the attribute applies.

 

Comparing Attributed Strings              --比较属性字符串

func isEqual(to: NSAttributedString) -> Bool

Returns a Boolean value that indicates whether the receiver is equal to another given attributed string.

 

Extracting a Substring               --抽取子字符串

func attributedSubstring(from: NSRange) -> NSAttributedString

Returns an NSAttributedString object consisting of the characters and attributes within a given range in the receiver.

 

Enumerating over Attributes in a String          --枚举字符串的属性

func enumerateAttribute(NSAttributedString.Key, in: NSRange, options: NSAttributedString.EnumerationOptions, using: (Any?, NSRange, UnsafeMutablePointer<ObjCBool>) -> Void)

Executes the specified closure for each range of a particular attribute in the attributed string.

func enumerateAttributes(in: NSRange, options: NSAttributedString.EnumerationOptions, using: ([NSAttributedString.Key : Any], NSRange, UnsafeMutablePointer<ObjCBool>) -> Void)

Executes the specified closure for each range of attributes in the attributed string.

 

Retrieving Font Attribute Information            --获取字体属性信息

func fontAttributes(in: NSRange) -> [NSAttributedString.Key : Any]

Returns the font attributes in effect for the character at the given location.

func rulerAttributes(in: NSRange) -> [NSAttributedString.Key : Any]

Returns the ruler (paragraph) attributes in effect for the characters within the given range.

 

Calculating Linguistic Units             --计算语言单位

func doubleClick(at: Int) -> NSRange

Returns the range of characters that form a word (or other linguistic unit) surrounding the given index, taking language characteristics into account.

func lineBreak(before: Int, within: NSRange) -> Int

Returns the appropriate line break when the character at the index won’t fit on the same line as the character at the beginning of the range.

func lineBreakByHyphenating(before: Int, within: NSRange) -> Int

Returns the index of the closest character before the given index, and within the given range, that can be placed on a new line by hyphenating.

func nextWord(from: Int, forward: Bool) -> Int

Returns the index of the first character of the word after or before the given index.

 

Calculating Ranges                --计算字符范围

func itemNumber(in: NSTextList, at: Int) -> Int

Returns the index of the item at the given location within the list.

func range(of: NSTextBlock, at: Int) -> NSRange

Returns the range of the individual text block that contains the given location.

func range(of: NSTextList, at: Int) -> NSRange

Returns the range of the given text list that contains the given location.

func range(of: NSTextTable, at: Int) -> NSRange

Returns the range of the given text table that contains the given location

 

Generating Data           --生成数据

func data(from: NSRange, documentAttributes: [NSAttributedString.DocumentAttributeKey : Any]) -> Data

Returns an data object that contains a text stream corresponding to the characters and attributes within the given range.

func fileWrapper(from: NSRange, documentAttributes: [NSAttributedString.DocumentAttributeKey : Any]) -> FileWrapper

Returns an NSFileWrapper object that contains a text stream corresponding to the characters and attributes within the given range.

func docFormat(from: NSRange, documentAttributes: [NSAttributedString.DocumentAttributeKey : Any]) -> Data?

Returns a data object that contains a Microsoft Word–format stream corresponding to the characters and attributes within the specified range.

func rtf(from: NSRange, documentAttributes: [NSAttributedString.DocumentAttributeKey : Any]) -> Data?

Returns an NSData object that contains an RTF stream corresponding to the characters and attributes within the given range, omitting all attachment attributes.

func rtfd(from: NSRange, documentAttributes: [NSAttributedString.DocumentAttributeKey : Any]) -> Data?

Returns a data object that contains an RTFD stream corresponding to the characters and attributes within the range.

func rtfdFileWrapper(from: NSRange, documentAttributes: [NSAttributedString.DocumentAttributeKey : Any]) -> FileWrapper?

Returns an NSFileWrapper object that contains an RTFD document corresponding to the characters and attributes within the given range.

 

Drawing the String           --绘制字符串

func draw(at: CGPoint)

Draws the attributed string starting at the specified point in the current graphics context.

func draw(in: CGRect)

Draws the attributed string inside the specified bounding rectangle in the current graphics context.

func draw(with: CGRect, options: NSStringDrawingOptions, context: NSStringDrawingContext?)

Draws the attributed string in the specified bounding rectangle using the provided options.

 

Getting Metrics for the String          --获取字符串的量度

func size() -> CGSize

Returns the size required to draw the string.

func boundingRect(with: CGSize, options: NSStringDrawingOptions, context: NSStringDrawingContext?) -> CGRect

Returns the bounding rectangle required to draw the string.

func containsAttachments(in: NSRange) -> Bool

Returns a Boolean value that indicates if the attributed string contains a property configured in the specified range.

 

Testing String Data Sources           --测试字符串的数据源

class var textTypes: [String]

Returns an array of UTI strings identifying the file types supported by the receiver, either directly or through a user-installed filter service.

class var textUnfilteredTypes: [String]

Returns an array of UTI strings identifying the file types supported directly by the receiver.

 

Constants             --常量

struct NSAttributedString.Key

Attributes that you can apply to text in an attributed string.

struct NSAttributedString.DocumentAttributeKey

Attributes that apply to a document.

struct NSAttributedString.DocumentType

Constants that can be returned for the document type in the document attributes dictionary.

struct NSAttributedString.DocumentReadingOptionKey

Options for importing documents

struct NSAttributedString.TextLayoutSectionKey

Values for the textLayoutSections key.

struct NSAttributedString.TextEffectStyle

A value for the textEffect key.

enum NSAttributedString.SpellingState

Values for the spellingState attribute key.

enum NSWritingDirectionFormatType

Options for specifying writing direction used with writingDirection.

enum NSTextWritingDirection

Options for specifying text writing direction used with writingDirection.

Deprecatedstruct NSUnderlineStyle

Constants for specifying both the underlineStyle and strikethroughStyle attributes of an attributed string.

struct NSAttributedString.EnumerationOptions

These constants describe the options available to the enumerateAttribute(_:in:options:using:) and enumerateAttributes(in:options:using:) methods.

 

Attributes for Generating HTML

These document-wide attributes provide control over the form of generated HTML—you use them only for writing HTML

 

Deprecated Methods         --已经被丢弃了的方法

init?(path: String, documentAttributes: AutoreleasingUnsafeMutablePointer<NSDictionary?>?)

Initializes a new attribute string object from RTF or RTFD data contained in the file at the given path.

Deprecatedinit?(url: URL, documentAttributes: AutoreleasingUnsafeMutablePointer<NSDictionary?>?)

Initializes a new attributed string object from the data at the given URL.

Deprecatedinit(fileURL: URL, options: [AnyHashable : Any], documentAttributes: AutoreleasingUnsafeMutablePointer<NSDictionary?>?)

Initializes a new attributed string object from the data at the given URL.

Deprecatedvar containsAttachments: Bool

Returns whether the attribute string contains any attachment attributes.

Deprecatedfunc url(at: Int, effectiveRange: NSRangePointer) -> URL?

Returns a URL, either from a link attribute or from text at the given location that appears to be a URL string, for use in automatic link detection.

Deprecatedfunc draw(with: NSRect, options: NSString.DrawingOptions)

Draws the receiver with the specified options, within the given rectangle in the current graphics context.

Deprecatedfunc boundingRect(with: NSSize, options: NSString.DrawingOptions) -> NSRect

Calculates and returns bounding rectangle for the receiver drawn using the options specified, within the given rectangle in the current graphics context.

Deprecated

 

Type Aliases         --类型别名

typealias NSAttributedString.CompletionHandler

 

Type Methods         --类方法

class func loadFromHTML(data: Data, options: [NSAttributedString.DocumentReadingOptionKey : Any], completionHandler: NSAttributedString.CompletionHandler)

class func loadFromHTML(fileURL: URL, options: [NSAttributedString.DocumentReadingOptionKey : Any], completionHandler: NSAttributedString.CompletionHandler)

class func loadFromHTML(request: URLRequest, options: [NSAttributedString.DocumentReadingOptionKey : Any], completionHandler: NSAttributedString.CompletionHandler)

class func loadFromHTML(string: String, options: [NSAttributedString.DocumentReadingOptionKey : Any], completionHandler: NSAttributedString.CompletionHandler)

 

Relationships           --继承关系

Inherits From

NSObject

Conforms To

  • CVarArg

  • Equatable

  • Hashable

  • NSCopying

  • NSItemProviderReading

  • NSItemProviderWriting

  • NSMutableCopying

  • NSPasteboardReading

  • NSPasteboardWriting

  • NSSecureCoding

See Also

Strings with Metadata

class NSMutableAttributedString

A mutable string object that also contains attributes (such as visual style, hyperlinks, or accessibility data) associated with various portions of its text content.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

200506--iOS之NSAttributedString类 的相关文章

随机推荐

  • OpenSSH权限提升漏洞(CVE-2021-41617)修复 Centos 7升级Openssh 8.8

    OpenSSH权限提升漏洞 xff08 CVE 2021 41617 xff09 修复 1 准备工作2 安装必须的包3 下载OpenSsh 8 8p14 OpenSsh 解压安装5 配置文件修改6 重启服务7 意外 Centos 7升级Op
  • Jenkins 构建报错:Couldn‘t find any revision to build. Verify the repository and branch configuration for

    Jenkins 构建报错 Couldn 39 t find any revision to build Verify the repository and branch configuration for 1 错误信息 2 错误原因3 解决
  • 利用阿里云下载国外镜像,国内顺畅下载k8s.gcr.io的镜像

    国内顺畅下载k8s gcr io的镜像 1 起因 配置kube dns是3个k8s gcr io的镜像无法下载 报错如下 Error response from daemon Get https k8s gcr io v2 net http
  • pip 使用阿里源

    pip 使用阿里源 使用pip install 的时候默认会去国外服务器下载 所以经常断开或者速度很慢 只需要在原来的命令后加上 i https mirrors aliyun com pypi simple即可直接从阿里源上安装 pip s
  • sun.misc包找不到

    转 http blog csdn net jbxiaozi article details 7351768 1 右键项目 属性 java bulid path jre System Library access rules resoluti
  • npm安装vue报错:npm ERR! code ETIMEDOUT

    npm安装vue报错 信息如下 C span class token punctuation span Users span class token punctuation span Q span class token operator
  • 将element-plus分页组件由默认英文,改为中文

    1 现象 分页组件默认显示为英文 但实际页面中大多都是中文 弄个英文显得比较突兀 2 配置 在main js中添加以下两句语句 span class token function import span locale from span c
  • [Gitops--2]Argocd和Gitlab-runner安装配置

    ArgoCd Argo是一组k8s原生工具集 用于运行和管理k8s上的作业和应用程序 Argo提供了一种在k8s上创建工作和应用的三种计算模式 服务模式 工作流模式和基于事件模式 所有的Argo工具都实现为了创建控制器和自定义资源 为什么选
  • Windows update 0x8024401c 0x80244019

    Windows 更新失败 报错 0x8024401c 0x80244019 以系统管理员身份运行 net stop wuauserv reg delete f HKEY LOCAL MACHINE span class token punc
  • K8s常见面试题20问

    K8s常见面试题19问 收集了一些K8s常见问题和同学们面试常被问到的问题 如果有新的面试题私聊或者留言给我 1 Docker和虚拟机有那些不同 虚拟化环境下每个 VM 是一台完整的计算机 xff0c 在虚拟化硬件之上运行所有组件 xff0
  • Dockerfile常用命令

    Dockerfile常用命令 1 Dockerfile Dockerfile是一个文本文件 用一组指令来完成镜像的构建 每一条指令构建一层镜像 所有尽量将相同的命令合并成一行以减少中间镜像的层数 2 From 必须 指定基础镜像即我从哪里可
  • Kubesphere流水线实现蓝绿发布

    Kubesphere流水线实现蓝绿发布 1 Gitlab仓库准备 1 1 创建仓库 新建空白项目 名字随便取 greenweb 复制克隆地址 http 192 168 31 199 deploy greenweb git 1 2 初始化并上
  • 【NetWorkX实例(3)】图、边、节点等相关方法

    更全面的NetworkX中文使用手册 xff0c 请收藏 xff1a NetworkX中文使用手册 在 NetWorkX实例 1 基础操作一文中 xff0c 介绍了networkx中图的生成 xff0c 下面就介绍一下图 边 节点等相关方法
  • Python调用外部EXE程序遍历窗体及控件并获取控件信息。

    背景 我的工作中经常手工运行一个windows程序 xff08 密码生成工具 xff09 xff0c 获取该程序的计算结果 xff0c 手工填到登录表单的中 该程序非常久远 xff0c 已无人维护 根据凡是重复2次以上的工作都应该自动化原则
  • J-Link RTT Viewer使用教程(附代码)

    目录 RTT Real Time Transfer 简介 使用教程 常用API介绍 RTT缓冲大小修改 使用printf重定向 官方例程 RTT Real Time Transfer 简介 平常调试代码中使用串口打印log xff0c 往往
  • [问题记录]JNI的整型数组返回出现stack corruption

    问题记录 JNI的整型数组返回出现stack corruption 在项目中编写了一个返回整型数组的JNI代码 xff0c 但是在测试时发现问题 xff0c 会产生stack corruption错误 xff0c debug之后发现是ret
  • Android逆向系列--JDWP协议

    Android逆向系列 JDWP协议 背景简介使用源码调用参考 背景 经常会遇到各种各样需要使用jdwp知识的场景 xff0c 比如调试Java源码 比如抓帧等等 xff0c 这些关联知识点通常都会极其复杂 xff0c 如果不能很好的了解j
  • 银河麒麟V10桌面版系统将用户开发Qt界面程序添加为开机自启动

    银河麒麟V10桌面版系统将用户开发Qt界面程序添加为开机自启动 银河麒麟V10桌面版系统允许用户开发自己的qt界面程序并将其添加为开机自启动 这样 xff0c 每次开机后 xff0c 用户开发的qt界面程序会自动启动 xff0c 无需手动打
  • 环境搭建-Linux-Mysql安装-10.3.7-MariaDB-log MariaDB Server

    10 3 7 MariaDB log MariaDB Server 安装记录 linux 系统 CentOS7 无脑安装 sudo yum install y redhat lsbsudo yum install y net tools关闭
  • 200506--iOS之NSAttributedString类

    Class NSAttributedString A string that has associated attributes such as visual style hyperlinks or accessibility data f