3

水平スクロールUITextViewを作成するにはどうすればよいですか?

プログラムでテキストを設定すると、自動改行が追加されるため、垂直方向にしかスクロールできません...

 titleView.text = @"this is a very long text. this is a very long text. this is a very long text. this is a very long text. this is a very long text.";

回答ありがとうございます。

編集:これまでのところ、私はこれを試しました:

 UIScrollview *yourScrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0 ,0 ,       self.view.frame.size.width, 50)];
CGFloat textLength = [titleView.text sizeWithFont:titleView.font constrainedToSize:CGSizeMake(9999, 50) lineBreakMode:NSLineBreakByWordWrapping].width;

yourScrollview.contentSize = CGSizeMake(textLength + 200, 500); //or some value you like, you may have to try this out a few times

titleView.frame = CGRectMake(titleView.frame.origin.x, titleView.frame.origin.y, textLength, titleView.frame.size.height);

[yourScrollview addSubview: titleView];

NSLog(@"%f", textLength);

しかし、私は受け取った:「脅威1:シグナルSIGABRT」

4

1 に答える 1

7

私はまだこのようなことをしていませんが、これを達成するために次の手順を試します。

  1. 作成するUIScrollview *yourScrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0 ,0 , self.view.frame.size.width, 50)]; //

  2. CGFloat textLength = [titleView.text sizeWithFont:titleView.font constrainedToSize:CGSizeMake(9999, 50) lineBreakMode:NSLineBreakByWordWrapping].width; テキストの最終的な長さを取得するために使用 します

  3. セットするyourScrollView.contentSize = CGSizeMake(textLength + 20, 50); //or some value you like, you may have to try this out a few times

  4. また設定titleTextView.frame = CGRectMake(titleTextView.frame.origin.x, titleTextView.frame.origin.y, textLength, titleTextView.frame.size.height);

  5. titleViewをyourScrollViewのサブビューにします。[yourScrollView addSubview: titleView];

これがあなたに良いスタートを与えることを願っています!

編集:このコードは機能します:

UILabelの代わりにを使用したことに注意してくださいUITextView

    UILabel *titleView          = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
    titleView.text              = @"this is a very long text. this is a very long text. this is a very long text. this is a very long text. this is a very long text.";
    titleView.font              = [UIFont systemFontOfSize:18];
    titleView.backgroundColor   = [UIColor clearColor];
    titleView.numberOfLines     = 1;

    UIScrollView *myScrollView  = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    CGFloat textLength          = [titleView.text sizeWithFont:titleView.font constrainedToSize:CGSizeMake(9999, 50) lineBreakMode:NSLineBreakByWordWrapping].width;
    myScrollView.contentSize    = CGSizeMake(textLength + 20, 50); //or some value you like, you may have to try this out a few times

    titleView.frame             = CGRectMake(titleView.frame.origin.x, titleView.frame.origin.y, textLength, titleView.frame.size.height);

    [myScrollView addSubview: titleView];
    [self.view addSubview:myScrollView];
    [titleView release];
    [myScrollView release];
于 2013-03-03T19:59:36.423 に答える