iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iphone/ipad:究竟如何使用NSAttributedString?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
是的,很多人在谈iPhone / iPad中的Rich Text,并且很多人都知道NSAttributedString。

但是如何使用NSAttributedString?我搜索了很多时间,没有提取线索。

我知道如何设置一个NSAttributedString,然后我该怎么做在iPhone / iPad上显示文本与富文本?

官方文档说,它应该与CoreText.Framework一起使用,这是什么意思?

有这样简单的方法吗?

NSAttributedString *str;
.....
UILabel *label;
label.attributedString = str;

解决方法

你应该看看 AliSoftware’s OHAttributedLabel.它是一个UILabel的子类,绘制一个NSAttributedString,并提供了方便方法来设置NSAttributedString从UIKit类的属性

从repo中提供的示例:

#import "NSAttributedString+Attributes.h"
#import "OHAttributedLabel.h"

/**(1)** Build the NSAttributedString *******/
NSMutableAttributedString* attrStr = [NSMutableAttributedString attributedStringWithString:@"Hello World!"];
// for those calls we don't specify a range so it affects the whole String
[attrStr setFont:[UIFont systemFontOfSize:12]];
[attrStr setTextColor:[UIColor grayColor]];
// Now we only change the color of "Hello"
[attrStr setTextColor:[UIColor redColor] range:NsmakeRange(0,5)];


/**(2)** Affect the NSAttributedString to the OHAttributedLabel *******/
myAttributedLabel.attributedText = attrStr;
// Use the "Justified" alignment
myAttributedLabel.textAlignment = UITextAlignmentJustify;
// "Hello World!" will be displayed in the label,justified,"Hello" in red and " World!" in gray.

注意:在iOS 6中,您可以使用UILabel的attributedText属性呈现属性字符串。

大佬总结

以上是大佬教程为你收集整理的iphone/ipad:究竟如何使用NSAttributedString?全部内容,希望文章能够帮你解决iphone/ipad:究竟如何使用NSAttributedString?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。