字符属性NSAttributedString

action_timeline_v0.11_dev_project.zip 立即下载

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.

这句话就是对这个类的一个最简明扼要的概括。NSAttributedString管理一个字符串,以及与该字符串中的单个字符或某些范围的字符串相关的属性。比如这个字符串“ 北京 天安门”,“我”跟其他字符的颜色不一样,而“北京”与其他的字体和大小不一样,等等。NSAttributedString就是用来存储这些信息的,具体实现时,NSAttributedString维护了一个NSString,用来保存最原始的字符串,另有一个NSDictionary用来保存各个子串/字符的属性。



Create Attributed String

有3种方法创建Attributed String。

1. 使用initWithString:, initWithString:attributes:, 或者 initWithAttributedString: ,下面是一个实例代码:

1
2
3
4
5
UIFont  *font = [UI Font  fontWithName:@ "Palatino-Roman"  size:14.0];
NSDictionary  *attrsDictionary = [ NSDictionary  dictionaryWithObject:font
                                     forKey: NSFontAttributeName ];
NSAttributedString  *attrString = [[ NSAttributedString  alloc] initWithString:@ "strigil"
             attributes:attrsDictionary];

 可以看到上面创建的整个字符串关联了Font属性。如果希望只是对某一范围的字符串施加某个属性应该使用NSMutableAttributedString的 setAttributes:range:方法。这里例子是使用了Font属性,在Appkit中特殊定义了若干属性,这些属性被用于Core Text中。其他的属性包括前景色、背景色、是否有shadow等,具体可见本文

2. 使用initWithRTF:documentAttributes:, initWithRTFD:documentAttributes:, and initWithRTFDFileWrapper:documentAttributes:从rich text (RTF) 或者 rich text with attachments (RTFD) 数据中创建。

复制代码
NSData *rtfData = ...;  // assume rtfData is an NSData object containing valid RTF data
NSDictionary *docAttributes;
NSSize paperSize;
 
NSAttributedString *attrString;
 
if ((attrString = [[NSAttributedString alloc]
        initWithRTF: rtfData documentAttributes: &docAttributes])) {
 
    NSValue *value = [docAttrs objectForKey:@"PaperSize"];
    paperSize = [value sizeValue];
    // implementation continues...
复制代码

3. 使用initWithHTML:documentAttributes: 和 initWithHTML:baseURL:documentAttributes:从HTML数据中创建。有线程安全问题,使用时需要注意。

对RTF和HTML的支持都是AppKit对NSAttributedString的扩展。




Accessing Attributes

从上面对这个类的介绍可以知道,如果我们要访问某个子串/字符的属性,需要提供子串的位置和属性的名字,而如果不提供属性名字,那就把所有属性都返回。下面就是其对应的APIs:

1
2
3
4
5
6
attributesAtIndex:effectiveRange:
attributesAtIndex:longestEffectiveRange:inRange:
attribute:atIndex:effectiveRange:
attribute:atIndex:longestEffectiveRange:inRange:
fontAttributesInRange:
rulerAttributesInRange:

 fontAttributesInRange: 和 rulerAttributesInRange: 是由AppKit扩展的属性。

The first four methods also return by reference the effective range and the longest effective range of the attributes. These ranges allow you to determine the extent of attributes. Conceptually, each character in an attributed string has its own collection of attributes; however, it’s often useful to know when the attributes and values are the same over a series of characters. This allows a routine to progress through an attributed string in chunks larger than a single character. In retrieving the effective range, an attributed string simply looks up information in its attribute mapping, essentially the dictionary of attributes that apply at the index requested. In retrieving the longest effective range, the attributed string continues checking characters past this basic range as long as the attribute values are the same. This extra comparison increases the execution time for these methods but guarantees a precise maximal range for the attributes requested.

Methods that return an effective range by reference are not guaranteed to return the maximal range to which the attribute(s) apply; they are merely guaranteed to return some range over which they apply. In practice they will return whatever range is readily available from the attributed string's internal storage mechanisms, which may depend on the implementation and on the precise history of modifications to the attributed string.

那些用reference返回有效范围的方法并不保证一定返回attribute应用的最大范围。它们只保证返回那些attribute有效的一些范围。实际上,它们只是返回attributed string中容易返回的那些信息,是否容易与内部实现,已经对attributed string的精确修改历史有关。

Methods that return a longest effective range by reference, on the other hand, are guaranteed to return the longest range containing the specified index to which the attribute(s) in question apply (constrained by the value of the argument passed in forinRange:). For efficiency, it is important that the inRange: argument should be as small as appropriate for the range of interest to the client.

那些返回最长有效范围的方法时能够保证返回制定attribute有效的最长的range的。为了效率,inRange: 参数应该尽量小,能满足客户需要就好。

When you iterate over an attributed string by attribute ranges, either sort of method may be appropriate depending on the situation. If there is some processing to be done for each range, and you know that the full range for a given attribute is going to have to be handled eventually, it may be more efficient to use the longest-effective-range variant, so as not to have to handle the range in pieces. However, you should use the longest-effective-range methods with caution, because the longest effective range could be quite long—potentially the entire length of the document, if the inRange: argument is not constrained.

 

Changing an Attributed String

NSMutableAttributedString提供若干方法,即可以修改字符串,又可以修改字符串的属性。经过多次修改后,有些信息可能变的不一致了,为了让信息保持一致,可以使用下面的方法:

1
2
3
4
5
6
fixAttributesInRange:
fixAttachmentAttributeInRange:
fixFontAttributeInRange:
fixParagraphStyleAttributeInRange:
beginEditing
endEditing

 

这些方法都是AppKit的扩展功能。




/*

 字符属性

 

 字符属性可以应用于 attributed string 的文本中。

 

 NSString *const NSFontAttributeName;(字体)

 

 NSString *const NSParagraphStyleAttributeName;(段落)

 

 NSString *const NSForegroundColorAttributeName;(字体颜色)

 

 NSString *const NSBackgroundColorAttributeName;(字体背景色)

 

 NSString *const NSLigatureAttributeName;(连字符)

 

 NSString *const NSKernAttributeName;(字间距)

 

 NSString *const NSStrikethroughStyleAttributeName;(删除线)

 

 NSString *const NSUnderlineStyleAttributeName;(下划线)

 

 NSString *const NSStrokeColorAttributeName;(边线颜色)

 

 NSString *const NSStrokeWidthAttributeName;(边线宽度)

 

 NSString *const NSShadowAttributeName;(阴影)(横竖排版)

 

 NSString *const NSVerticalGlyphFormAttributeName;

 

 常量

 

 1> NSFontAttributeName(字体)

 

 该属性所对应的值是一个 UIFont 对象。该属性用于改变一段文本的字体。如果不指定该属性,则默认为12-point Helvetica(Neue)。

 

 2> NSParagraphStyleAttributeName(段落)

 

 该属性所对应的值是一个 NSParagraphStyle 对象。该属性在一段文本上应用多个属性。如果不指定该属性,则默认为 NSParagraphStyle 的defaultParagraphStyle 方法返回的默认段落属性。

 

 3> NSForegroundColorAttributeName(字体颜色)

 

 该属性所对应的值是一个 UIColor 对象。该属性用于指定一段文本的字体颜色。如果不指定该属性,则默认为黑色。

 

 4> NSBackgroundColorAttributeName(字体背景色)

 

 该属性所对应的值是一个 UIColor 对象。该属性用于指定一段文本的背景颜色。如果不指定该属性,则默认无背景色。

 

 5> NSLigatureAttributeName(连字符)

 

 该属性所对应的值是一个 NSNumber 对象(整数)。连体字符是指某些连在一起的字符,它们采用单个的图元符号。0 表示没有连体字符。1 表示使用默认的连体字符。2表示使用所有连体符号。默认值为 1(注意,iOS 不支持值为 2)。

 

 6> NSKernAttributeName(字间距)

 

 该属性所对应的值是一个 NSNumber 对象(整数)。字母紧排指定了用于调整字距的像素点数。字母紧排的效果依赖于字体。值为 0 表示不使用字母紧排。默认值为0。

 

 7> NSStrikethroughStyleAttributeName(删除线)

 

 该属性所对应的值是一个 NSNumber 对象(整数)。该值指定是否在文字上加上删除线,该值参考“Underline Style Attributes”。默认值是NSUnderlineStyleNone。

 

 8> NSUnderlineStyleAttributeName(下划线)

 

 该属性所对应的值是一个 NSNumber 对象(整数)。该值指定是否在文字上加上下划线,该值参考“Underline Style Attributes”。默认值是NSUnderlineStyleNone。

 

 9> NSStrokeColorAttributeName(边线颜色)

 

 该属性所对应的值是一个 UIColor 对象。如果该属性不指定(默认),则等同于 NSForegroundColorAttributeName。否则,指定为删除线或下划线颜色。更多细节见“Drawing attributedstrings that are both filled and stroked”。

 

 10> NSStrokeWidthAttributeName(边线宽度)

 

 该属性所对应的值是一个 NSNumber 对象(小数)。该值改变描边宽度(相对于字体size 的百分比)。默认为 0,即不改变。正数只改变描边宽度。负数同时改变文字的描边和填充宽度。例如,对于常见的空心字,这个值通常为3.0。

 

 11> NSShadowAttributeName(阴影)

 

 该属性所对应的值是一个 NSShadow 对象。默认为 nil。

 

 12> NSVerticalGlyphFormAttributeName(横竖排版)

 

 该属性所对应的值是一个 NSNumber 对象(整数)。0 表示横排文本。1 表示竖排文本。在 iOS 中,总是使用横排文本,0 以外的值都未定义。

 


 */

最小体积包围椭球(MVEE)的MATLAB基准测试与可重复实验,包含CGD、WA及RCD求解器.rar 立即下载

相关推荐

基于matlab的优化算法,解决线性规划、非线性规划、二次规划和多目标规划的问题.rar

基于matlab的优化算法,解决线性规划、非线性规划、二次规划和多目标规划的问题.rar

iOS NSAttributedString属性字符串)

ui

邓俊杰 6804

CAD+说明书空气滤清器连接板冲孔,冲槽,落料复合模设计

CAD+说明书空气滤清器连接板冲孔、冲槽、落料复合模设计

iOS 字符属性NSAttributedString描述【转载】

/* 字符属性 字符属性可以应用于 attributed string 的文本中。 NSString *const NSFontAttributeName;(字体) NSString *const NSParagraphStyleAttributeName;(段落) NSString *const NSForegroundColorAttrib...

dianye0094的博客 148

属性字符串(NSAttributedString)的简单应用

属性字符NSAttributedString 可以对字符串附加格式信息,由于对于对不同文本片段使用不同的格式,属性字符串类特别合适。 IOS 6中对样式文本有了大改善,大部分主要的UIKit控件都允许使用属性字符串。举例UILable,只要创建一个属性字符串,然后赋值给标签的attributedText属性即可。 - (IBAction)buttonPressed:(UIBut...

dcpe8917的博客 162

iOS 字符属性NSAttributedString描述

字符属性    字符属性可以应用于 attributed string 的文本中。    NSString *const NSFontAttributeName;(字体)    NSString *const NSParagraphStyleAttributeName;(段落)    NSString *const NSForegroundColor

学无止境! 无休无止! 402

iOS【UIKit字符属性NSAttributedString概述】

AJ分享,必须精品   UIKit字符属性NSAttributedString概述 字符属性 字符属性可以应用于 attributed string 的文本中。 NSString *const NSFontAttributeName;(字体) NSString *const NSParagraphStyleAttributeName;(段落) NSString *con

阿俊的博客 429

属性字符串 NSMutableAttributedString/NSAttributedString

由于iOS7新出的NSTextStorge是NSMutableAttributedString的子类,所以要用好NSTextStorage,首先要学好NSMutableAttributedString和NSAttributedString。 按个人的理解,NSAttributedString是一个带有属性字符串,通过该类可以灵活地操作和呈现多种样式的文字数据。   ali

岽少的专栏 2039

施耐德电气焊接设备.rar

施耐德电气焊接设备.rar

全自动烫金机.rar

全自动烫金机.rar

永磁同步电机全速域无传感器复合控制策略研究:基于高频注入与自适应滑模观测的加权融合方法(Simulink仿真实现)

内容概要:本文研究了一种用于永磁同步电机(PMSM)全速域运行的无传感器复合控制策略,提出了一种基于高频信号注入与自适应滑模观测器(SMO)的加权融合方法。该策略针对电机在零低速与中高速不同工况下的控制难题,采用脉振方波高频注入法实现静止及低速状态下转子初始位置的精确估计,并引入模糊超螺旋滑模观测器实现中高速运行时转速与位置的实时跟踪。为确保全速域内平滑过渡,设计了动态加权切换机制与相位同步校正策略,有效抑制了传统切换过程中的抖振与失步现象。整个控制系统在Simulink环境中构建仿真模型,经多工况测试验证,该复合控制策略在宽速范围内展现出优良的动静态性能、强鲁棒性以及良好的抗干扰能力,适用于高性能电机驱动系统的无传感器控制需求。; 适合人群:具备电机控制、现代控制理论及MATLAB/Simulink仿真基础的电气工程、自动化及相关专业的研究生、科研人员及从事高性能电机驱动系统开发的工程师。; 使用场景及目标:① 解决永磁同步电机在无机械传感器条件下全速域运行的转速与位置估计难题;② 学习高频注入法与滑模观测器的原理及其在低速和高速区的应用差异;③ 掌握异构观测器融合与平滑切换机制的设计方法,提升复杂控制系统的设计与仿真能力。; 阅读建议:建议读者结合文中系统架构图与仿真模型逐步理解各模块功能,重点关注高频注入信号的解调过程、滑模观测器的设计与稳定性分析、加权切换逻辑的实现方式,并通过调整参数进行仿真验证,深入掌握控制策略的实现细节与优化思路。

【半导体制造】基于SECS/GEM标准的设备通信协议实现:工厂自动化系统中设备状态监控与远程控制集成方案设计

内容概要:本文档为SECS-GEM规格说明书,详细定义了半导体制造设备与主机系统之间通信的标准化接口规范。文档涵盖了通信协议(如HSMS)、状态模型(通信、控制及设备处理状态)、设备能力实现(事件通知、数据采集、报警管理、远程控制、过程程序管理等)以及具体的消息类型和数据格式。通过状态图和消息交互场景,明确了设备在不同通信和控制状态下的行为逻辑,并规定了TCP/IP超时参数、变量命名、报警分类等级等关键技术细节,确保设备与主机间的可靠通信与协同控制。 适合人群:半导体设备制造商的技术工程师、自动化系统集成商、工厂MES系统开发人员,以及需要对接SECS/GEM协议的研发与运维人员。 使用场景及目标:①用于指导设备端GEM功能的开发与调试;②支持主机系统(如MES)正确解析设备状态、采集实时数据、下发控制指令;③实现设备报警监控、工艺程序管理、时间同步及终端信息交互等功能,提升产线自动化与信息化水平。 阅读建议:本规格书技术性强,建议结合实际通信抓包工具(如Wireshark)和SECS模拟器进行对照学习与测试验证,重点关注消息流向、状态转换条件及异常处理机制,以确保系统集成的准确性与稳定性。

手工木工切割机3D数模图纸 Solidworks设计.rar

手工木工切割机3D数模图纸 Solidworks设计.rar

QYR-18900-2026-2032全球与中国地上泳池市场现状及未来发展趋势 Sample liting.docx

QYR-18900-2026-2032全球与中国地上泳池市场现状及未来发展趋势 Sample liting.docx

包含一组MATLAB脚本,旨在使用拓扑优化技术设计异质机械测试.rar

包含一组MATLAB脚本,旨在使用拓扑优化技术设计异质机械测试.rar

上一篇: NSBundle
下一篇: NSCharacter​Set
Vic__li
博客等级 码龄11年 27粉丝 123原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值