NSAttributedString(FWFoundation)

@interface NSAttributedString (FWFoundation)

如果需要实现行内图片可点击效果,可使用UITextView添加附件或Link并实现delegate.shouldInteractWith方法即可。 注意iOS在后台运行时,如果调用NSAttributedString解析html会导致崩溃(如动态切换深色模式时在后台解析html)。解决方法是提前在前台解析好或者后台异步到下一个主线程RunLoop

  • NSAttributedString对象转换为html字符串

    Declaration

    Objective-C

    - (nullable NSString *)fw_htmlString;
  • 计算所占尺寸,需设置Font等

    Declaration

    Objective-C

    @property (nonatomic, readonly) CGSize fw_textSize;
  • 计算在指定绘制区域内所占尺寸,需设置Font等

    Declaration

    Objective-C

    - (CGSize)fw_textSizeWithDrawSize:(CGSize)drawSize;
  • html字符串转换为NSAttributedString对象。如需设置默认字体和颜色,请使用addAttributes方法或附加CSS样式

    Declaration

    Objective-C

    + (nullable instancetype)fw_attributedStringWithHtmlString:
        (nonnull NSString *)htmlString;
  • 图片转换为NSAttributedString对象,可实现行内图片样式。其中bounds.x会设置为间距,y常用算法:(font.capHeight - image.size.height) / 2.0

    Declaration

    Objective-C

    + (nonnull NSAttributedString *)fw_attributedStringWithImage:
                                        (nullable UIImage *)image
                                                          bounds:(CGRect)bounds;
  • 快速创建NSAttributedString并指定高亮部分文字和样式,链接设置NSLinkAttributeName|URL属性即可

    Declaration

    Objective-C

    + (nonnull NSAttributedString *)
        fw_attributedStringWithString:(nonnull NSString *)string
                           attributes:
                               (nullable NSDictionary<NSAttributedStringKey, id> *)
                                   attributes
                            highlight:(nonnull NSString *)highlight
                  highlightAttributes:
                      (nullable NSDictionary<NSAttributedStringKey, id> *)
                          highlightAttributes;
  • 快速创建NSAttributedString,自定义字体

    Declaration

    Objective-C

    + (nonnull instancetype)fw_attributedString:(nonnull NSString *)string
                                       withFont:(nullable UIFont *)font;
  • 快速创建NSAttributedString,自定义字体和颜色

    Declaration

    Objective-C

    + (nonnull instancetype)fw_attributedString:(nonnull NSString *)string
                                       withFont:(nullable UIFont *)font
                                      textColor:(nullable UIColor *)textColor;
  • html字符串转换为NSAttributedString对象,可设置默认系统字体和颜色(附加CSS方式)

    Declaration

    Objective-C

    + (nullable instancetype)
        fw_attributedStringWithHtmlString:(nonnull NSString *)htmlString
                        defaultAttributes:
                            (nullable NSDictionary<NSAttributedStringKey, id> *)
                                attributes;
  • html字符串转换为NSAttributedString主题对象,可设置默认系统字体和动态颜色,详见FWThemeObject

    Declaration

    Objective-C

    + (nonnull FWThemeObject<NSAttributedString *> *)
        fw_themeObjectWithHtmlString:(nonnull NSString *)htmlString
                   defaultAttributes:
                       (nullable NSDictionary<NSAttributedStringKey, id> *)
                           attributes;
  • Undocumented

    Declaration

    Objective-C

    + (NSString *)fw_CSSStringWithColor:(UIColor *)color NS_REFINED_FOR_SWIFT;
  • Undocumented

    Declaration

    Objective-C

    + (NSString *)fw_CSSStringWithFont:(UIFont *)font NS_REFINED_FOR_SWIFT;