iphone / ipad:究竟如何使用NSAttributedString? [英] iphone/ipad: How exactly use NSAttributedString?

查看:116
本文介绍了iphone / ipad:究竟如何使用NSAttributedString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,很多人都在谈论iPhone / iPad中的Rich Text,很多人都知道 NSAttributedString

Yes, many people are saying about Rich Text in iPhone/iPad and many knows about NSAttributedString.

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

But how to use NSAttributedString? I searched for much time, no extract clues for this.

我知道如何设置 NSAttributedString ,然后我该怎么办才能在iPhone / iPad上用富文本显示文字?

I know how to set up a NSAttributedString, then what should I do to display a text on iPhone/iPad with rich text?

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

The official docs says it should be used with CoreText.Framework, what does that mean?

有这样简单的方法吗?

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


推荐答案

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

You should take a look at AliSoftware's OHAttributedLabel. It is a subclass of UILabel that draws an NSAttributedString and also provides convenience methods for setting the attributes of an NSAttributedString from UIKit classes.

来自repo中提供的示例: / p>

From the sample provided in the 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+中,您可以使用< a href =https://developer.apple.com/library/ios/documentation/uikit/reference/UILabel_Class/Reference/UILabel.html#//apple_ref/occ/instp/UILabel/attributedText\"rel =noreferrer> UILabel的attributesText 属性。

Note: In iOS 6+ you can render attributed strings using the attributedText property of UILabel.

这篇关于iphone / ipad:究竟如何使用NSAttributedString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆