ios - 如何在 UITextView 中水平和垂直对齐文本?

标签 ios uitextview

<分区>

如何在 UITextView 中水平和垂直对齐文本? 我想将 UITextView 中的文本与水平对齐和垂直对齐对齐。 有什么自定义方法吗? 请帮帮我......

最佳答案

这是一个 Swift 解决方案,现在有所不同,因为内容插入和偏移量已略有变化。此解决方案适用于 Xcode 7 beta 6。

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    textView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.New, context: nil)
}

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    textView.removeObserver(self, forKeyPath: "contentSize")
}

Apple 已经更改了内容偏移和插图的工作方式,现在需要这个稍微修改过的解决方案来设置内容插图的顶部而不是偏移。

/// Force the text in a UITextView to always center itself.
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    let textView = object as! UITextView
    var topCorrect = (textView.bounds.size.height - textView.contentSize.height * textView.zoomScale) / 2
    topCorrect = topCorrect < 0.0 ? 0.0 : topCorrect;
    textView.contentInset.top = topCorrect
}

关于ios - 如何在 UITextView 中水平和垂直对齐文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21669688/

相关文章:

iphone - PDF 查看器 iPad 应用程序

iphone - 有什么方法可以在不开始滚动的情况下显示 UITextView 的滚动指示器?

ios - 粘贴操作时 UITextView 奇怪的动画故障 (iOS 11)

ios - cordova 在最新的 12.4 iOS 版本中支持 Indexeddb 吗?

ios - 用于iOS测试飞行Beta测试的沙盒帐户

iphone - 当父 View 为 UIScrollView UNTIL I scoll 时不显示 UITextView 文本

iOS - 如何让 UITextView 始终位于 UIImageView 之上?

ios - 带有链接 : disable scrolling, 的 UITextView 已启用用户交互

iOS静态表格根据textView自动调整大小

ios - SwiftUI 自定义 List 不能在 ForEach 中使用 Binding