【发布时间】:2016-06-19 22:51:33
【问题描述】:
上下文
我正在尝试在文本视图中输入一堆随机文本并将其换行,以便该框完全充满符号。似乎文本视图正试图根据文本中的某些字符进行换行。我已将导致文本视图错误换行的字符列表缩小到以下内容:
]}).>/+
其余可以显示的字符有:
[<{(#@=*~
在 Playground 中运行的以下代码将导致 UITextView 在该行的最后一个字符之前换行。
import UIKit
func generateRandomText() -> NSAttributedString {
let characterLimit = 9500
let segmentSize = 500
// problem characters
// ] } ) . > / +
let characterOptions = ["[","<","{","(","#","@","=","*","~","+","]","}",")",".",">","/"]
let codeString = NSMutableAttributedString.init(string: "")
while codeString.string.characters.count < segmentSize {
let addition = NSAttributedString.init(string: characterOptions[Int(arc4random_uniform(16))], attributes: [NSForegroundColorAttributeName : UIColor.whiteColor()])
codeString.appendAttributedString(addition)
}
while codeString.string.characters.count < characterLimit {
codeString.appendAttributedString(codeString)
}
return codeString
}
let textView = UITextView.init(frame: CGRectMake(0, 0, 700, 700))
textView.textContainer.lineBreakMode = .ByCharWrapping
let codeFont = UIFont.init(name: "Menlo", size: 12)
textView.font = codeFont
textView.backgroundColor = UIColor.blackColor()
textView.attributedText = generateRandomText()
结果:
但是,没有问题字符的相同代码会产生我正在寻找的结果。
import UIKit
func generateRandomText() -> NSAttributedString {
let characterLimit = 9500
let segmentSize = 500
// problem characters
// ] } ) . > / +
let characterOptions = ["[","<","{","(","#","@","=","*","~"]
let codeString = NSMutableAttributedString.init(string: "")
while codeString.string.characters.count < segmentSize {
let addition = NSAttributedString.init(string: characterOptions[Int(arc4random_uniform(9))], attributes: [NSForegroundColorAttributeName : UIColor.whiteColor()])
codeString.appendAttributedString(addition)
}
while codeString.string.characters.count < characterLimit {
codeString.appendAttributedString(codeString)
}
return codeString
}
let textView = UITextView.init(frame: CGRectMake(0, 0, 700, 700))
textView.textContainer.lineBreakMode = .ByCharWrapping
let codeFont = UIFont.init(name: "Menlo", size: 12)
textView.font = codeFont
textView.backgroundColor = UIColor.blackColor()
textView.attributedText = generateRandomText()
结果:
问题
当字符串包含某些字符时,什么会导致文本视图换行?
【问题讨论】:
标签: ios swift uitextview