WPAttributedMarkup 1.0.0

WPAttributedMarkup 1.0.0

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
发布上次发布2014年12月

Nigel Grange 维护。



  • Nigel Grange

WPAttributedMarkup 是一个简单的工具类别,可以用于轻松从带有标记标签的文本和样式字典创建富文本字符串。

这个类别允许您将以下文本

<bold>Bold</bold> text

通过使用样式字典如

  NSAttributedString *as = [@"<bold>Bold</bold" attributedStringWithStyleBook:@{@"body":[UIFont fontWithName:@"HelveticaNeue" size:18.0],
  @"bold":[UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0]}];

在这个例子中,与“body”关联的样式应用于整个文本,而与“bold”关联的样式应用于 < bold > 标签内的文本。

所有标签('body' 除外)都是用户定义的。

该字典被称为“样式书”,因为其目的是在其中集中存放所有应用程序字体、颜色等。

常见问题解答(FAQ)

问:这个能否将 html 转换为带样式的文本?

答:一般来说,不能。语法类似于 html,但所有标签都是用户自定义的。如果 html 格式正确,并且使用一系列严格的标签,则可以通过定义适当的标签集(如 b, h1, h2 等)将 html 转换为带属性的文本。

问:NSAttributedString 不已经使用类似

[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] 
                             options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                       NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} 
                  documentAttributes:nil error:nil];

答:是的。然而,上述方法非常慢(它使用背景中的 WebView),并且是异步执行的(试着在一个 UITableViewCell 中创建一个并快速滚动表格...)。

此外,所有样式必须在文档 CSS 中设置,而不是能够传递应用程序已使用的标准颜色和字体类。

支持哪些样式?

以下对象可以用于样式字典中

UIColor:用于给文本部分着色。

@"red": [UIColor redColor]

UIFont:用于设置文本部分的字体。

@"bold":[UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0]

NSDictionary:将带属性的字符串样式键/对应用于文本部分。这可以用于应用下划线、删除线、段落样式以及自定义属性等。

@"u": @{NSUnderlineStyleAttributeName : @(kCTUnderlineStyleSingle|kCTUnderlinePatternSolid)}

NSArray:一个任何样式项的数组,所有这些样式项都应用于文本部分。

@"u": @[[UIColor blueColor],
@{NSUnderlineStyleAttributeName : @(kCTUnderlineStyleSingle|kCTUnderlinePatternSolid)} ]

NSString:将命名样式应用于文本部分。这对于应用现有样式以及一些新属性而无需复制现有属性非常有用。

 @"red": [UIColor redColor],
 @"redunderline": @[ @{NSUnderlineStyleAttributeName : @(kCTUnderlineStyleSingle|kCTUnderlinePatternSolid)}, @"red" ]

UIImage:在文本部分的指定位置插入图像。注意:图像标签内的文本必须只包含一个字符,该字符将被图像替换。

@"thumb":[UIImage imageNamed:@"thumbIcon"]

在文本中使用

 <thumb> </thumb>

额外功能

还包含了一些额外的实用类,它们可能与 WPAttributedMarkup 有关联并可能有用。特别是,可以使用 WPHotspotLabel 类将块方法应用于富文本字符串的任何部分,从而轻松创建特定于应用的链接。

WPAttributedStyleAction - 一个包装块并允许使用 styledAction 方法将其插入到属性字符串中的类。这实际上会给文本添加一个 链接 样式,因此文本也会继承 链接 样式中定义的属性(如果有的话)。

它可以这样使用:

@"help":[WPAttributedStyleAction styledActionWithAction:^{
                             NSLog(@"Help action");
                         }]

WPTappableLabel - 一个简单的 UILabel 子类,允许设置 onTap 块,当标签被点击时会被调用。

WPHotspotLabel - WPTappableLabel 的一个子类,它检测点击位置文本的属性,如果找到了 WPAttributedStyleAction 属性,则执行动作。

注意,这些类尚未进行彻底测试,因此它们可能在所有条件下都不能按预期工作。特别是,WPHotspotLabel 使用 CoreText 布局来检测点击位置的属性,可能会导致与显示的布局不同。到目前为止,在所有使用简单标签和格式的测试中,检测都工作得正确,但是你已经收到警告了!

示例

Screen

// Example using fonts and colours
NSDictionary* style1 = @{@"body":[UIFont fontWithName:@"HelveticaNeue" size:18.0],
                         @"bold":[UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0],
                         @"red": [UIColor redColor]};




 // Example using arrays of styles, dictionary attributes for underlining and image styles

 NSDictionary* style2 = @{@"body" :
                             @[[UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0],
                               [UIColor darkGrayColor]],
                            @"u": @[[UIColor blueColor],
                                @{NSUnderlineStyleAttributeName : @(kCTUnderlineStyleSingle|kCTUnderlinePatternSolid)}
                                 ],
                            @"thumb":[UIImage imageNamed:@"thumbIcon"] };


// Example using blocks for actions when text is tapped. Uses the 'link' attribute to style the links

NSDictionary* style3 = @{@"body":[UIFont fontWithName:@"HelveticaNeue" size:22.0],
                         @"help":[WPAttributedStyleAction styledActionWithAction:^{
                             NSLog(@"Help action");
                         }],
                         @"settings":[WPAttributedStyleAction styledActionWithAction:^{
                             NSLog(@"Settings action");
                         }],
                         @"link": [UIColor orangeColor]};

self.label1.attributedText = [@"Attributed <bold>Bold</bold> <red>Red</red> text" attributedStringWithStyleBook:style1];

self.label2.attributedText = [@"<thumb> </thumb> Multiple <u>styles</u> text <thumb> </thumb>" attributedStringWithStyleBook:style2];

self.label3.attributedText = [@"Tap <help>here</help> to show help or <settings>here</settings> to show settings" attributedStringWithStyleBook:style3];

工作原理

该工具包含两个类别

NSMutableString+TagReplace : 用于内部移除 NSMutableString 中的所有标签,并构建包含开始和结束范围的标签数组。

NSString+WPAttributedMarkup : 包含一个公共方法

-(NSAttributedString*)attributedStringWithStyleBook:(NSDictionary*)styleBook;

使用 NSMutableString+TagReplace 构建标签数组,然后遍历每个标签并应用样式手册中找到的样式。

如果没有为标签找到样式,则简单地从字符串中移除标签,不应用任何样式。

如果在样式手册中找到了 body 标签,则将此应用在整个字符串上,然后再应用其他样式。