swift - UITextView 对齐方式在更新时发生变化
问题描述
我试图在更新 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
}
}
解决方案
将段落样式附加到属性字符串
private func getDisplayText(pageNumber: Int) -> NSAttributedString {
// Define paragraph style - you got to pass it along to NSAttributedString constructor
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
var stringVal = ""
switch pageNumber {
case 0:
stringVal = "Create"
default:
stringVal = "Str"
}
let attributedText = NSMutableAttributedString(string: stringVal, attributes: [.font: UIFont.systemFont(ofSize: 36, weight: .semibold)])
attributedText.append(NSAttributedString(string: "\n\nStr", attributes: [.font: UIFont.systemFont(ofSize: 18, weight: .medium), .foregroundColor: UIColor.black,.paragraphStyle: paragraphStyle]))
return attributedText
}
推荐阅读
- python - picamera.exc.PiCameraValueError:分辨率 320x240 的缓冲区长度不正确
- javascript - JSON,从字符串中检索
- python - 删除重复项并根据列保留前两个值
- python - 替换第二次出现的字母
- sql - SQL. query: find a warehouse in which this item is not stored
- python - Python字符串格式化“:<10”是什么意思?
- c# - 如何在 C# 中声明一个正则表达式以跳过一个字符、用另一个替换一个字符并在特定位置添加一个新字符?
- android - 如何处理上传密钥
- git - 如何搜索提交中的所有删除?
- c# - 在 .NET 5 中将 IActionResult 与 Azure Functions 一起使用?