我想将NSString转换为NSAttributedString.
但我总是要这样做
但我总是要这样做
- NSAttributedString *useDict1=[[NSAttributedString alloc] initWithString:@"String"];
有没有其他方式,我不必每次都分配字典,但只是给字符串?
解决方法
我建议在NSString上创建一个类别,使用一种方法将其转换为NSAttributedString,然后在整个项目中使用该辅助方法.
像这样:
- @interface NSString (AttributedStringCreation)
- - (NSAttributedString *)attributedString;
- @end
- @implementation NSString (AttributedStringCreation)
- - (NSAttributedString *)attributedString {
- return [[NSAttributedString alloc] initWithString:self];
- }
- @end