更新UITextView时,我试图使文本与中心对齐,但出于某种原因,除中心对齐外,所有其他设置都保存。
这里是我初始化文本视图的位置
private let displayTextView: UITextView = {
let textView = UITextView()
let attributedText = NSMutableAttributedString(string: "Str", attributes: [NSAttributedString.Key.font: UIFont.fableFont(36, weight: .semibold)])
attributedText.append(NSAttributedString(string: "\n\nStr", attributes: [NSAttributedString.Key.font: UIFont.fableFont(18, weight: .medium), NSAttributedString.Key.foregroundColor: UIColor.black]))
textView.attributedText = attributedText
textView.textAlignment = .center
textView.isEditable = false
textView.isScrollEnabled = false
textView.isSelectable = false
textView.translatesAutoresizingMaskIntoConstraints = false
return textView
}()
这里是我更新文本视图的位置
private func getDisplayText(pageNumber: Int) -> NSAttributedString {
switch tapCount {
case 0:
let attributedText = NSMutableAttributedString(string: "Create", attributes: [NSAttributedString.Key.font: UIFont.fableFont(36, weight: .semibold)])
attributedText.append(NSAttributedString(string: "\n\nStr", attributes: [NSAttributedString.Key.font: UIFont.fableFont(18, weight: .medium), NSAttributedString.Key.foregroundColor: UIColor.black]))
return attributedText
case 1:
let attributedText = NSMutableAttributedString(string: "Str", attributes: [NSAttributedString.Key.font: UIFont.fableFont(36, weight: .semibold)])
attributedText.append(NSAttributedString(string: "\n\nStr", attributes: [NSAttributedString.Key.font: UIFont.fableFont(18, weight: .medium), NSAttributedString.Key.foregroundColor: UIColor.black]))
return attributedText
case 2:
let attributedText = NSMutableAttributedString(string: "Str", attributes: [NSAttributedString.Key.font: UIFont.fableFont(36, weight: .semibold)])
attributedText.append(NSAttributedString(string: "\n\nStr", attributes: [NSAttributedString.Key.font: UIFont.fableFont(18, weight: .medium), NSAttributedString.Key.foregroundColor: UIColor.black]))
return attributedText
default:
let attributedText = NSMutableAttributedString(string: "Str", attributes: [NSAttributedString.Key.font: UIFont.fableFont(36, weight: .semibold)])
attributedText.append(NSAttributedString(string: "\n\nStr", attributes: [NSAttributedString.Key.font: UIFont.fableFont(18, weight: .medium), NSAttributedString.Key.foregroundColor: UIColor.black]))
return attributedText
}
}
附加attributedText对齐方式
private func getDisplayText(pageNumber: Int) -> NSAttributedString {
// Define paragraph style - you got to pass it along to NSAttributedString constructor
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
switch pageNumber {
case 0:
let attributedText = NSMutableAttributedString(string: "Create", attributes: [NSAttributedString.Key.font: UIFont.fableFont(36, weight: .semibold)])
attributedText.append(NSAttributedString(string: "\n\nStr", attributes: [NSAttributedString.Key.font: UIFont.fableFont(18, weight: .medium), NSAttributedString.Key.foregroundColor: UIColor.black, , NSAttributedString.Key.paragraphStyle: paragraphStyle]))
return attributedText
case 1:
let attributedText = NSMutableAttributedString(string: "Str", attributes: [NSAttributedString.Key.font: UIFont.fableFont(36, weight: .semibold)])
attributedText.append(NSAttributedString(string: "\n\nStr", attributes: [NSAttributedString.Key.font: UIFont.fableFont(18, weight: .medium), NSAttributedString.Key.foregroundColor: UIColor.black, , NSAttributedString.Key.paragraphStyle: paragraphStyle]))
return attributedText
case 2:
let attributedText = NSMutableAttributedString(string: "Str", attributes: [NSAttributedString.Key.font: UIFont.fableFont(36, weight: .semibold)])
attributedText.append(NSAttributedString(string: "\n\nStr", attributes: [NSAttributedString.Key.font: UIFont.fableFont(18, weight: .medium), NSAttributedString.Key.foregroundColor: UIColor.black, , NSAttributedString.Key.paragraphStyle: paragraphStyle]))
return attributedText
default:
let attributedText = NSMutableAttributedString(string: "Str", attributes: [NSAttributedString.Key.font: UIFont.fableFont(36, weight: .semibold)])
attributedText.append(NSAttributedString(string: "\n\nStr", attributes: [NSAttributedString.Key.font: UIFont.fableFont(18, weight: .medium), NSAttributedString.Key.foregroundColor: UIColor.black, , NSAttributedString.Key.paragraphStyle: paragraphStyle]))
return attributedText
}
}