문제

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?

도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top