我有一个自定义NSTextAttachment
类,用于在 a 中缩放图像UITextView
(基于此处的答案)。portrait
这在模式下效果很好。在landscape
模式下,图像不会覆盖整个屏幕,我想将它们放在textview
.
在自定义类 (in attachmentBoundsForTextContainer:
) 中更改边界不会产生影响(尝试同时更改x
和更改y
边界)。这还能如何改变?谢谢。
我有一个自定义NSTextAttachment
类,用于在 a 中缩放图像UITextView
(基于此处的答案)。portrait
这在模式下效果很好。在landscape
模式下,图像不会覆盖整个屏幕,我想将它们放在textview
.
在自定义类 (in attachmentBoundsForTextContainer:
) 中更改边界不会产生影响(尝试同时更改x
和更改y
边界)。这还能如何改变?谢谢。
垂直居中 UITextView 的内容。
在 UIViewController 的 viewWillAppear 中。
textView.addObserver(self, forKeyPath: "contentSize", options: .New, context: nil)
在你的 UIViewController 的 viewWillDisappear 中。
textView.removeObserver(self, forKeyPath: "contentSize")
覆盖 UIViewController 中的 observeValueForKeyPath。
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if keyPath == "contentSize" {
let height = textView.bounds.size.height
let contentHeight = textView.contentSize.height
let zoom = textView.zoomScale
let top = (height - contentHeight * zoom) / 2.0
textView.contentInset.top = top < 0.0 ? 0.0 : top
}
}
在 UITextView 内水平居中 NSAttributtedString 的内容。
let attachment = NSTextAttachment()
let attachmentAttrString = NSAttributedString(attachment: attachment)
let result = NSMutableAttributedString(attributedString: attachmentAttrString)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .Center
let attrs:[String:AnyObject] = [NSParagraphStyleAttributeName: paragraphStyle]
let range = NSMakeRange(0, result.length)
result.addAttributes(attrs, range: range)
textView.attributedText = result