将NSString转换为NSAttributedString而不使用alloc init

ios*_*per 6 nsattributedstring ios

我想转换NSStringNSAttributedString.但我总是要这样做

 NSAttributedString *useDict1=[[NSAttributedString alloc] initWithString:@"String"];
Run Code Online (Sandbox Code Playgroud)

有没有其他方式,我不必Dictionary每次都分配,但只是给字符串?

小智 12

我建议在NSString上创建一个类,使用一种方法将其转换为NSAttributedString,然后在整个项目中使用该辅助方法.

像这样:

@interface NSString (AttributedStringCreation)
   - (NSAttributedString *)attributedString;
@end

@implementation NSString (AttributedStringCreation)

- (NSAttributedString *)attributedString {
   return [[NSAttributedString alloc] initWithString:self];
}

@end
Run Code Online (Sandbox Code Playgroud)