textViewのサイズを変更した後、textContainerをtextViewと同じサイズ(テキストの幅をサイズ変更されたtextViewと同じにする)にします。したがって、現在行っているのは、UITextView
プログラムで作成し、(textView内のテキストの有無にかかわらず)境界を変更してtextViewのサイズを変更しますが、textContainerは同じサイズのままです(テキストはtextViewよりも小さい長方形で表示されます)。
ここにいくつかのコードがあります:
let textStorage = NSTextStorage()
let layoutManager = NSLayoutManager()
textStorage.addLayoutManager(layoutManager)
let textContainer = NSTextContainer()
layoutManager.addTextContainer(textContainer)
textContainer.widthTracksTextView = true
textContainer.heightTracksTextView = true
self.textView = UITextView(frame: textViewFrame, textContainer: textContainer)
以降:
self.textView.bounds = CGRect(x, y, newWidth, newHeight)
現在、なぜそれが機能せず、解決策を見つけることができないのか、私は考えがありません。
これを遊び場に投げてください。textViewのサイズを設定するさまざまな方法でコメントアウト/コメントアウトします。bounds
確かに設定しても、コンテナは更新されません。frame
またはを設定size
します。
import Foundation
import UIKit
import XCPlayground
var str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean feugiat purus a pharetra rhoncus. Phasellus eget velit risus. Duis varius et massa non pretium. Praesent sollicitudin egestas mi, non cursus eros ornare congue. Pellentesque ex leo, hendrerit posuere euismod ut, ultricies eleifend lectus. Phasellus eu interdum sem. Quisque ut condimentum justo. Integer non neque mauris. Vestibulum eu tellus ac nunc mollis maximus a vitae lectus. In hac habitasse platea dictumst. Sed neque sapien, elementum quis volutpat quis, dictum id ligula. Cras egestas finibus fermentum."
let textViewFrame = CGRectZero
let textStorage = NSTextStorage()
let layoutManager = NSLayoutManager()
textStorage.addLayoutManager(layoutManager)
let textContainer = NSTextContainer(size: textViewFrame.size)
layoutManager.addTextContainer(textContainer)
textContainer.widthTracksTextView = true
textContainer.heightTracksTextView = true
var textView = UITextView(frame: textViewFrame, textContainer: textContainer)
XCPShowView("MyView", view: textView)
textView.text = str
//textView.bounds = CGRect(x: 0, y: 0, width: 200, height: 200)
//textView.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
textView.frame.size = CGSize(width: 200, height: 200)
//textView.bounds = CGRect(x: 0, y: 0, width: 400, height: 200)
//textView.frame = CGRect(x: 0, y: 0, width: 400, height: 200)
textView.frame.size = CGSize(width: 400, height: 200)
UITextViewを回転しているためにここにいる場合は、将来の読者:UITextViewを変換します
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加