ios - 带有 NSAttributedString 和自定义属性的 UITextView 不起作用

标签 ios cocoa-touch uitextview nsattributedstring

使用 UITextView 时,我尝试将自定义属性添加到属性字符串。但是,在分配给 UITextView 的属性文本后,所有自定义键都将丢失。像这样:

NSMutableAttributedString *lString = [[ NSMutableAttributedString alloc ] initWithString: @"astring"
                                             attributes: @{ @"customkey": @"customvalue" }];
NSLog(@"string: %@", lString);  // shows customkey present
textView.attributedText = lString;
NSLog(@"result: %@", self.textView.attributedText);   // shows customkey missing

这应该有效吗?

最佳答案

从 iOS 7 开始,它现在可以随心所欲地工作了:

来自 Accessing Attributes :

An attributed string identifies attributes by name, storing a value under the attribute name in an NSDictionary object, which is in turn associated with an NSRange that indicates the characters to which the dictionary’s attributes apply. You can assign any attribute name-value pair you wish to a range of characters, in addition to the standard attributes.

它适用于标准和 NSTextStorage (Text Kit) 支持的 UITextViews。

关于ios - 带有 NSAttributedString 和自定义属性的 UITextView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13648003/

相关文章:

iOS UITextView 识别错误链接

ios - 如何在ARC中释放NSMutableArray保存的数据

ios - 如何在 Objective c 的 Core Data 中添加数组作为字符串

iphone - 使用 Core Graphics 以 30-60 fps 的速度手动移动/旋转东西

cocoa-touch - 输出西类牙语字符

objective-c - 可以通过 IB 为 UITextView 分配变量名吗?

iphone - ios 应用程序因 XMPPMessageArchiving 而崩溃

iphone - UISwitch 设置为 ON,它不显示模态视图 Controller 转换

iphone - 帮助向 UITableViewCell 添加背景 View

ios - 如何从快速应用程序的键盘上完全删除隐藏/完成按钮?