ios – 在没有alloc init的情况下将NSString转换为NSAttributedString

前端之家收集整理的这篇文章主要介绍了ios – 在没有alloc init的情况下将NSString转换为NSAttributedString前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想将NSString转换为NSAttributedString.
但我总是要这样做
  1. NSAttributedString *useDict1=[[NSAttributedString alloc] initWithString:@"String"];

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

解决方法

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

像这样:

  1. @interface NSString (AttributedStringCreation)
  2. - (NSAttributedString *)attributedString;
  3. @end
  4.  
  5. @implementation NSString (AttributedStringCreation)
  6.  
  7. - (NSAttributedString *)attributedString {
  8. return [[NSAttributedString alloc] initWithString:self];
  9. }
  10.  
  11. @end

猜你在找的iOS相关文章