首页 > 解决方案 > 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
    }
  }

标签: swiftuitextview

解决方案


将段落样式附加到属性字符串

 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
}

推荐阅读