NSAttributedString(FWFoundation)

@interface NSAttributedString (FWFoundation)

如果需要实现行内图片可点击效果,可使用UITextView添加附件或Link并实现delegate.shouldInteractWith方法即可

  • 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;