- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我无法让 attributedText 一起管理字体和粗体。我的场景是一个 View Controller ,它带有一个接受属性文本的可滚动 TextView 。
属性是通过使用下面的函数转换一个简单的 html 字符串(ul 和 b)来创建的。
没问题。当我稍后将文本设置为 attributedData 然后设置字体时,问题出现在我的 View Controller 中。
先处理文本,再处理字体:从显示中移除粗体。先处理字体,再处理文本:停止字体工作。
我怀疑粗体在字符串中没有表示为“粗体”,而是使用字体粗体。
当我设置文本,然后设置字体时,它会覆盖属性中的所有字体(包括粗体)。当我设置字体,然后是文本时,属性中的字体是最后一个,并且设置为默认字体但正确切换为粗体版本的字体。
有没有办法更改 attributedString 的字体并保持粗体/斜体?不是我喜欢的地方,但是字体可以在 NSAttributedString 函数的选项中设置吗?
func html2AttributedString(fromString: String) -> NSAttributedString {
var returnString: NSAttributedString
do {
let attributedData = fromString.dataUsingEncoding(NSUnicodeStringEncoding)
let tempAttributedString = try NSAttributedString(data: attributedData!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
returnString = tempAttributedString
} catch {
let tempString = "Unknown String"
let attributedData = tempString.dataUsingEncoding(NSUnicodeStringEncoding)
let tempAttributedString = try! NSAttributedString(data: attributedData!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
returnString = tempAttributedString
}
return returnString
}
在 View Controller 中设置文本
contentTextView.attributedText = descriptionTextAttributed
contentTextView.textColor = UIColor.blackColor()
if let font = UIFont(name: "StoneSans", size: 20.0) {
contentTextView.font = font
}
我可以看到更改字体的三个级别:html、转换时和完成转换时。
Wain 提出了在转换后在 attributedText 中更改它的方法,但我的 CPU 也有限,而且该方法对用户来说会明显变慢。
我已经尝试以编程方式为 html 执行此操作,但它不起作用。如果有人可以提供帮助,也许我的插入内容需要修复:
//let tempString = "<style> {font-family: sans-serif;}</style>" + fromString
//let tempString = "<body style='font-family=sans-serif'>" + fromString
我目前正在尝试使用 documentAttributes 获得正确的语法,以尝试更改转换期间使用的属性(请参阅 How do you use NSAttributedString?)。这段代码没有编译,因为我没有正确准备 documentAttributes:
if let font = UIFont(name: "StoneSans", size: CGFloat(AppData.sharedInstance.instructionFontSize)) {
do {
let attributedData = fromString.dataUsingEncoding(NSUnicodeStringEncoding)
let myAttributes:Dictionary = [
NSParagraphStyleAttributeName : NSMutableParagraphStyle(),
NSKernAttributeName : 3, // (-1,5)
NSFontAttributeName : font,
NSForegroundColorAttributeName : UIColor.whiteColor(),
NSShadowAttributeName : nil
]
let tempAttributedString = try NSAttributedString(data: attributedData!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: myAttributes)
returnString = tempAttributedString
} catch {
let tempString = "Unknown String"
let attributedData = tempString.dataUsingEncoding(NSUnicodeStringEncoding)
let tempAttributedString = try! NSAttributedString(data: attributedData!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
returnString = tempAttributedString
}
}
如果有人可以修复我的代码以解决这两种更改字体的早期方法,我们将不胜感激。
蒂姆。
最佳答案
没错,粗体和斜体信息是字体信息的一部分。使用 NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType
使用默认字体(或 HTML 中指定的任何字体)。
因此,您需要编辑字体而不是仅仅替换它们,例如:
NSMutableAttributedString *attributedString = [self mutableCopy];
[attributedString beginEditing];
[attributedString enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, attributedString.length) options:0 usingBlock:^(UIFont *value, NSRange range, BOOL *stop) {
NSString *fontName = [value.fontName lowercaseString];
UIFont *destinationFont = [UIFont XXXX];
if ([fontName rangeOfString:@"bold"].location != NSNotFound) {
destinationFont = [UIFont BOLD_XXXX];
} else if ([fontName rangeOfString:@"italic"].location != NSNotFound) {
destinationFont = [UIFont ITALIC_XXXX];
}
[attributedString removeAttribute:NSFontAttributeName range:range];
[attributedString addAttribute:NSFontAttributeName value:destinationFont range:range];
}];
[attributedString endEditing];
关于ios - 在 swift attributedString 中设置字体正在删除粗体强调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35200260/
如果您绝对必须设置 html 按钮的样式,那么强调其中一些按钮的正确方法是什么?例如“添加到购物车”通常会在视觉上得到强调,以使其脱颖而出。 选项 1 - 用 em 或 strong + css 包装
Underscore 提供了方法,throttle。来自他们的文档: 创建并返回所传递函数的新的 throttle 版本,当重复调用时,每等待毫秒最多只会实际调用一次原始函数。对于发生速度快于您无法跟
我正在使用 Docx4j 从模板创建文档。 我无法找到创建斜体段落的方法(HTML 中的)。 这是创建段落的方法示例,并提供以粗体进行创建的选项。 private static org.docx4j.
我正在研究一些简单的 Markdown 文本,以玩弄和学习 Pyparsing 和一般语法。我几乎立即遇到了一个我无法解决的问题。我正在尝试解析 CommonMark 的简单版本强调的规范。在此设置中
如果我使用 ggplot,则 x Axis (y==0) 的水平线与 y 的任何其他值相同。我想强调一个事实,即图表的底部不是 x Axis ,并且 x Axis 在图中较高。我怎样才能做到这一点?
我正在尝试实现一个包含特定公交时刻表的 ListView(它只是一个包含特定时间的列表)。 我正在寻找的是一种正确的方法(遵循指南)来突出显示/强调特定行以指示下一类车何时到达。 首先,我改变了行的背
所以我有一个关于 Kindle 设备上的 flexbox 支持的问题,如果你有关于总体支持的更多信息。似乎不支持 display:flex 和 flex-wrap:wrap/nowrap 属性,至少在
我似乎无法制作斜体(即*强调*)链接。我尝试了以下语法: *[text](url)* [*text*](url) *[text]*(url) [*emphasised* text](url) 根据 D
我需要调整别人构建的应用程序以使用我编写的 API。在生产环境中,该应用程序将向具有签名的受信任证书的服务器发出 HTTPS 请求。没问题。 但是,我的本地开发服务器没有 SSL 证书(除了我自己创建
有人有这四个的工作配置吗? - Django - uWSGI - Nginx - SSL 主要问题是如何为此正确设置SSL?我在谷歌上搜索了很多,但仍然无法正常工作。我有一个使用 unix socke
如何使用 GWT 或 Javascript 强调 DIV 内的文本: Element content = DOM.createElement("div"); content.setInnerText(
我是一名优秀的程序员,十分优秀!