Ios 在UITextView中垂直居中NSTextAttachment
我有一个自定义的Ios 在UITextView中垂直居中NSTextAttachment,ios,swift,uitextview,nstextattachment,Ios,Swift,Uitextview,Nstextattachment,我有一个自定义的NSTextAttachment类,用于缩放UITextView中的图像(基于)。这在纵向模式下非常有效。在横向模式下,图像不会覆盖整个屏幕,我希望将它们置于文本视图的中心位置 更改自定义类中的边界(在attachmentBoundsForTextContainer:中)没有任何区别(尝试更改边界中的x和y)。这还有什么可以改变的?谢谢。将UITextView的内容垂直居中。 let attachment = NSTextAttachment() let attachmentAt
NSTextAttachment
类,用于缩放UITextView
中的图像(基于)。这在纵向
模式下非常有效。在横向
模式下,图像不会覆盖整个屏幕,我希望将它们置于文本视图
的中心位置
更改自定义类中的边界(在
attachmentBoundsForTextContainer:
中)没有任何区别(尝试更改边界中的x
和y
)。这还有什么可以改变的?谢谢。将UITextView的内容垂直居中。
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
在视图中,将显示UIViewController的名称
textView.addObserver(self, forKeyPath: "contentSize", options: .New, context: nil)
textView.removeObserver(self, forKeyPath: "contentSize")
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
}
}
在视图中,UIViewController的图标将消失
textView.addObserver(self, forKeyPath: "contentSize", options: .New, context: nil)
textView.removeObserver(self, forKeyPath: "contentSize")
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
}
}
覆盖UIViewController中的observeValueForKeyPath
textView.addObserver(self, forKeyPath: "contentSize", options: .New, context: nil)
textView.removeObserver(self, forKeyPath: "contentSize")
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
}
}