Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在UITextView中垂直居中NSTextAttachment_Ios_Swift_Uitextview_Nstextattachment - Fatal编程技术网

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
    }
}