【问题标题】:How to use Dynamic Type in TextView inside Table View Cell如何在表格视图单元格内的 TextView 中使用动态类型
【发布时间】:2019-09-08 10:27:24
【问题描述】:

我想在我的应用程序中实现动态类型,以便应用程序可以适应用户的字体偏好。我有一个带有 3 个静态单元格的表格视图。第一个和第二个有UITextField,它们工作正常,没有任何问题。第三个是UITextView 的单元格不能缩放。动态类型设置为开启,字体为正文。

这是我在UITableViewController 中的代码。

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.rowHeight = UITableView.automaticDimension
    tableView.estimatedRowHeight = 100 
    setupTextFieldsAndTextView()
}

func setupTextFieldsAndTextView() {
    emailTF.keyboardType = UIKeyboardType.asciiCapable
    titleTF.keyboardType = UIKeyboardType.asciiCapable
    messageTextView.keyboardType = UIKeyboardType.asciiCapable

    emailTF.returnKeyType = .next
    titleTF.returnKeyType = .next
    messageTextView.returnKeyType = .default

    emailTF.delegate = self
    titleTF.delegate = self
    messageTextView.delegate = self
}

UITableView 代表

 override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableView.automaticDimension
}

我希望拥有与 iOS 日历应用程序相同的设计。基于所选字体类型的单元格比例,但UITextView 仍然是可滚动的,并且里面的文本也由用户选择。屏幕中的UITextView 是一个带有文本占位符“Notes”的单元格。

【问题讨论】:

  • @ShadeToD :当您声称带有 UITextView 的单元格不随动态类型缩放时,您是在谈论高度还是字体大小?
  • @XLE_22 我的意思是单元格的高度和该单元格内文本的字体。从 iOS 日历应用中查看 2 张照片。它缩放单元格高度以及单元格内的文本。 (带有占位符“备注”的单元格)
  • 你可以试试你的textview单元听UIContentSizeCategory.didChangeNotification然后刷新你的textview?

标签: ios swift accessibility dynamic-type-feature


【解决方案1】:

检查UITextView 的几个步骤:

  • 它必须在您的牢房内。
  • 它的约束必须与单元格一起设置。
  • 如果您想使用动态类型功能,必须选择其文本样式。
  • 如果您想使用动态类型功能,它的Automatically Adjusts Font 属性必须在界面生成器中打勾。
  • 关闭属性检查器的滚动视图部分中的Scrolling Enabled 属性。

检查完所有这些元素后,单元格高度和UITextView 字体大小应调整为所需大小

我建议您观看 WWDC 2017 视频中的 detailed summary,该视频完美地解释了如何使用动态类型构建应用程序。

【讨论】:

  • 附上截图回答会更好
  • @ArshadShaik:我建议的详细摘要包含许多有用的信息和一个有效的示例,这些示例公开了我认为截图根本无法替代的代码和技术。 ?
【解决方案2】:

请在 tableView 中检查 textView 的动态高度。

// Tableview Delegate Method
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "NotesCell", for: indexPath) as! NotesCell

    cell.txtViewNotes.tag = indexPath.row
    cell.txtViewNotes.delegate = self

    return cell;
}

// Textview Delegate Method
func textViewDidChange(_ textView: UITextView) {
        adjustFrames(textView)
}

// Custom Method    
func adjustFrames(_ textView: UITextView)  {
    let indexPath = IndexPath(row: textView.tag, section: 0)
    let cell = yourTableView.cellForRow(at: indexPath) as! NotesCell

    UIView.setAnimationsEnabled(false);

    self.yourTableView.beginUpdates()
        cell.constNoteHeight.constant = textView.contentSize.height
        textView.beginFloatingCursor(at: CGPoint.zero)
        textView.endFloatingCursor()
    self.yourTableView.endUpdates()

    UIView.setAnimationsEnabled(true);

}

注意:constNoteHeight是textView的高度常数。

【讨论】:

    【解决方案3】:

    使用GrowingTextView,用于 UITextView

    【讨论】:

    • 我想要和 iOS 日历应用一样的设计。当您键入文本时,在单元格内的该应用程序文本视图内不会增长。
    • 你好@ShadeToD,我遇到了同样的问题,所以我在单元格内使用了GrowingTextview。请检查我的屏幕:prntscr.com/ndjahg
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多