回应我之前发布的一个例子,Andreas Oetjen 展示了一种巧妙的方法来显示分数和小数表textView使用属性字符串 https://stackoverflow.com/a/42153358/2348597对齐数字,使小数点或正斜杠字符出现在同一列中,如示例 1 所示below。通过对属性字符串的介绍,我被提示问:如何以不同的颜色或字体显示每一列? (将数据与应用程序中其他位置找到的文件类型相关联)
When I try to do this the closest I get is the display shown in Example 2 below and I can’t figure out why the text is not displayed in two columns, one in blue, the other in red with numbers in the red column aligned the same as in Example 1. Example 2 has two issues. Firstly, paragraph formatting of Example 1 is lost when font formatting is introduced and secondly, there are two versions of the text, one in colour, the other in black. I suspect both issues have the same underlying cause.
根据 Andreas 的回答,第一个代码示例below组装组成每一行的字符串,然后使用NSMutableParagraphStyle()
创建示例 1 中所示的布局 (above)。第二个代码示例在每行附加格式化字符串之前对字符串应用格式化 - 我尝试交错NSMutableParagraphStyle()
and NSMutableAttributedString
- 创建示例 2 中所示的布局(above)。两者都在 Playground 中进行了测试。在第二个示例中,我尝试切换调用格式化函数的顺序,例如
appendLeftColumnParagraph(textIn: column1)
appendLeftColumnFont(i: i)
and
appendRightColumnParagraph(textIn: column2)
appendRightColumnFont(i: i)
代替
appendLeftColumnFont(i: i)
appendLeftColumnParagraph(textIn: column1)
and
appendRightColumnFont(i: i)
appendRightColumnParagraph(textIn: column2)
但这只会使彩色字符出现在黑色字符之后而不是之前。几乎就像我只能期望使用以下任一方法附加属性字符串NSMutableAttributedString
or NSMutableParagraphStyle()
但不是两者兼而有之。要么是这样,要么是我不理解附加属性字符串的一些基本内容。
所以,假设这是正确的问题,我该如何交错NSMutableParagraphStyle()
and NSMutableAttributedString
格式化显示的字符串UITextView
分为两列,一列为蓝色,另一列为红色,红色列中的数字与示例 1 中的对齐方式相同?
如果有脑细胞新鲜的人能帮我回答这个问题,我将不胜感激。谢谢。
代码示例 1 - 仅使用 NSMutableParagraphStyle()
import UIKit
import PlaygroundSupport
var tuningArray = ["1/1", "76.04900", "193.15686", "310.26471", "5/4", "503.42157", "579.47057", "696.57843", "25/16", "889.73529", "1006.84314", "1082.89214"]
var keyArray = ["x", "1", "x", "x", "4", "x", "6", "x", "x", "9", "x", "11"]
let scaleSize = tuningArray.count
var textMessage = ""
for i in 0..<scaleSize {
var textLine = "\t" + keyArray[i] + "\t" + tuningArray[i] + "\n"
textMessage += textLine
}
let paragraphStyle = NSMutableParagraphStyle()
let leftTabLocation = CGFloat(50)
let rightTabLocation = CGFloat(185)
var leftTab = NSTextTab(textAlignment: .left, location: leftTabLocation, options: [:])
let rightTerminators:CharacterSet = [".", "/"]
let rightTabOptions = [NSTabColumnTerminatorsAttributeName:rightTerminators]
var rightTab = NSTextTab(textAlignment: .natural, location: rightTabLocation, options:rightTabOptions)
paragraphStyle.tabStops = [leftTab, rightTab]
var attributedText = NSAttributedString(string:textMessage,attributes: [NSParagraphStyleAttributeName: paragraphStyle])
let formattedText = UITextView(frame:CGRect(x:0, y:0, width:300, height:200))
formattedText.attributedText = attributedText
PlaygroundPage.current.liveView = formattedText
代码示例 2 - NSMutableParagraphStyle() 和 NSMutableAttributedString.init(string: )
import UIKit
import PlaygroundSupport
var tuningArray = ["1/1", "76.04900", "193.15686", "310.26471", "5/4", "503.42157", "579.47057", "696.57843", "25/16", "889.73529", "1006.84314", "1082.89214"]
var keyArray = ["x", "1", "x", "x", "4", "x", "6", "x", "x", "9", "x", "11"]
let scaleSize = tuningArray.count
var textMessage = ""
var textString = String()
var formattedString = NSAttributedString()
var rangeOfKeyString = NSRange()
var attributeKey = NSMutableAttributedString()
var rangeOfTuningString = NSRange()
var attributeTuning = NSMutableAttributedString()
let attributedTextMessage = NSMutableAttributedString()
func appendLeftColumnParagraph(textIn: String) {
let paragraphStyle = NSMutableParagraphStyle()
let leftTabLocation = CGFloat(50.0)
let leftTab = NSTextTab(textAlignment: .left, location: leftTabLocation, options: [:])
paragraphStyle.tabStops = [leftTab]
let attributedLeftColumnParagraph = NSAttributedString(string:textIn, attributes: [NSParagraphStyleAttributeName: paragraphStyle])
attributedTextMessage.append(attributedLeftColumnParagraph)
}
func appendRightColumnParagraph(textIn: String) {
let paragraphStyle = NSMutableParagraphStyle()
let rightTabLocation = CGFloat(185.0)
let rightSeparators:CharacterSet = [".", "/"]
let rightTabOptions = [NSTabColumnTerminatorsAttributeName: rightSeparators]
let rightTab = NSTextTab(textAlignment: .natural, location: rightTabLocation, options: rightTabOptions)
paragraphStyle.tabStops = [rightTab]
let attributedRightColumnParagraph = NSAttributedString(string:textIn, attributes: [NSParagraphStyleAttributeName: paragraphStyle])
attributedTextMessage.append(attributedRightColumnParagraph)
}
func appendLeftColumnFont(i: Int){
rangeOfKeyString = (keyArray[i] as NSString).range(of: keyArray[i])
attributeKey = NSMutableAttributedString.init(string: keyArray[i])
attributeKey.addAttribute(NSForegroundColorAttributeName, value: UIColor.blue, range: rangeOfKeyString)
attributedTextMessage.append(attributeKey)
}
func appendRightColumnFont(i: Int){
rangeOfTuningString = (tuningArray[i] as NSString).range(of: tuningArray[i])
attributeTuning = NSMutableAttributedString.init(string: tuningArray[i])
attributeTuning.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: rangeOfTuningString)
attributedTextMessage.append(attributeTuning)
}
func appendTab(){
let attributedTab = NSMutableAttributedString.init(string: "\t")
attributedTextMessage.append(attributedTab)
}
func appendNewLine(){
let attributedNewLine = NSMutableAttributedString.init(string: "\n")
attributedTextMessage.append(attributedNewLine)
}
for i in 0..<scaleSize {
// var textLine = "\t" + keyArray[i] + "\t" + tuningArray[i] + "\n"
let column1 = keyArray[i]
let column2 = tuningArray[i]
appendTab()
appendLeftColumnFont(i: i)
appendLeftColumnParagraph(textIn: column1)
appendTab()
appendRightColumnFont(i: i)
appendRightColumnParagraph(textIn: column2)
appendNewLine()
}
formattedString = attributedTextMessage
let formattedText = UITextView(frame:CGRect(x:0, y:0, width:300, height:200))
formattedText.attributedText = formattedString
PlaygroundPage.current.liveView = formattedText