Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert NSString into NSAttributedString without alloc init

I want to convert NSString into NSAttributedString. But i always have to do

 NSAttributedString *useDict1=[[NSAttributedString alloc] initWithString:@"String"];

Is there any other way such that i don't have to allocate the Dictionary every time, but just give the string?

like image 401
ios developer Avatar asked May 15 '14 11:05

ios developer


1 Answers

I'd suggest to create a category on NSString with a method that converts it to NSAttributedString and then use that helper method across your project.

Like this:

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

@implementation NSString (AttributedStringCreation)

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

@end
like image 172
Roman Avatar answered Sep 18 '22 14:09

Roman