html - 仅使用粗体和斜体将 NSAttributedString 转换为 HTML

标签 html ios objective-c cocoa-touch nsattributedstring

我希望将 NSAttributedString 转换为 HTML,但只保留粗体和斜体属性。我不关心字体系列、字体大小或类似的东西。

我基本上是想转换: 猎鹰

<b>Go</b> <em>Falcons</em>

有办法吗?

最佳答案

我不确定这是否是一个可行的解决方案,但一种方法是枚举属性并遍历每个段并制作您自己的 HTML。

 [str enumerateAttribute:NSFontAttributeName
                inRange:range
                options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
             usingBlock:^(id value, NSRange range, BOOL *stop) {
               UIFont *currentFont = value;
               if ([currentFont.fontName rangeOfString:@"bold" options:NSCaseInsensitiveSearch]
                       .location != NSNotFound) {
                   //Do something with the string and range for bold? add tags and append in a different string
               } 
               // Similarly do something for for non-bold, itatlic or normal text. Keep Appending them in a string
             }];

关于html - 仅使用粗体和斜体将 NSAttributedString 转换为 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33158992/

相关文章:

html - IE 在 vi​​sual studio 和服务器上的调试之间显示不同的页面

ios - 正确显示不是来自图像 Assets 的视网膜图形

objective-c - AppleScript 很酷。我可以在普通的 Objective-C 中做同样的事情吗?

ios - 详细信息 View Controller 中缺少 UIBarButton

ios - objectAtIndexedSubscript 和 objectAtIndex 之间的区别

iphone - 可达性和通知中心

objective-c - 适用于 NSArray 中包含的字符串数量

javascript - 如何制作即使在 iPhone Safari 中也能正常工作的完全响应式背景图片?

html - 仅输入数字并添加最大数量的字母

javascript - 多个页面部分的垂直居中对齐