如果您想显示来自两个来源的文本,但文本出现在一个视图中,每个来源的行交替出现,您可以创建一个类,如下例所示。
我创建了一个UIView 的子类,它带有两个UITextViews 作为Subviews。文本通过DoubleTextView 类设置。
UITextViews 的来源不同,因此文本将显示为交替行。
好处:
- 不用担心哪些文本是原创的,哪些是您以编程方式添加的。
- 轻松设置不同的样式
可能的缺点:
- 只有一个 UITextView 是可编辑的,因为一个总是不可触摸的(它被覆盖了)
结果:
需要一些更好的数学来理想地定位或间隔文本。
代码:
将字符串转换为NSMutableAttributedString并设置行距:
func convertLineHeight(string string_I: String) -> NSMutableAttributedString {
let style = NSMutableParagraphStyle()
style.lineSpacing = lineSpacing + self.font.lineHeight
let attributes = [NSParagraphStyleAttributeName : style]
let attributedString = NSMutableAttributedString(string: string_I, attributes:attributes)
return attributedString
}
偏移一个视图:
let alphaPoint = CGPoint(x: 0, y: (lineSpacing / 2) + self.font.lineHeight)
// this is almost perfect. is off by a few pixels.
全班:
class DoubleTextView : UIView {
private var alphaTextView : UITextView!
private var betaTextView : UITextView!
private var lineSpacing : CGFloat = 0
var font : UIFont = UIFont.systemFontOfSize(UIFont.systemFontSize()) {
didSet {
alphaTextView.font = self.font
betaTextView.font = self.font
}
}
var alphaText : String = "" {
didSet {
alphaTextView.attributedText = convertLineHeight(string: alphaText)
}
}
var betaText : String = "" {
didSet {
betaTextView.attributedText = convertLineHeight(string: betaText)
}
}
var alphaAttributedText : NSMutableAttributedString = NSMutableAttributedString() {
didSet {
alphaTextView.attributedText = convertLineHeight(attributedString: alphaAttributedText)
}
}
var betaAttributedText : NSMutableAttributedString = NSMutableAttributedString() {
didSet {
betaTextView.attributedText = convertLineHeight(attributedString: betaAttributedText)
}
}
init(frame: CGRect, lineSpacing lineSpacing_I: CGFloat) {
lineSpacing = lineSpacing_I
super.init(frame: frame)
var adjustedSize = frame.size
adjustedSize.height -= ((lineSpacing / 2) + self.font.lineHeight)
let alphaPoint = CGPoint(x: 0, y: (lineSpacing / 2) + self.font.lineHeight)
alphaTextView = UITextView(frame: CGRect(origin: alphaPoint, size: adjustedSize))
alphaTextView.backgroundColor = UIColor.clearColor()
alphaTextView.font = self.font
betaTextView = UITextView(frame: CGRect(origin: CGPointZero, size: adjustedSize))
betaTextView.font = self.font
betaTextView.backgroundColor = UIColor.clearColor()
self.addSubview(alphaTextView)
self.addSubview(betaTextView)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func convertLineHeight(string string_I: String) -> NSMutableAttributedString {
let style = NSMutableParagraphStyle()
style.lineSpacing = lineSpacing + self.font.lineHeight
let attributes = [NSParagraphStyleAttributeName : style]
let attributedString = NSMutableAttributedString(string: string_I, attributes:attributes)
return attributedString
}
private func convertLineHeight(attributedString attributedString_I: NSMutableAttributedString) -> NSMutableAttributedString {
let style = NSMutableParagraphStyle()
style.lineSpacing = lineSpacing + self.font.lineHeight
let attributes = [NSParagraphStyleAttributeName : style]
attributedString_I.addAttributes(attributes, range: (attributedString_I.string as NSString).rangeOfString(attributedString_I.string))
return attributedString_I
}
}
var test = DoubleTextView(frame: CGRect(x: 0, y: 0, width: 200, height: 400), lineSpacing: 20)
test.backgroundColor = UIColor.whiteColor()
test.alphaText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
test.betaText = "This is the second text. It shows comments, edits, suggestions, thoughts about the first"