如何让TextView能够水平滚动 [英] How to let TextView be able to scroll horizontally

查看:73
本文介绍了如何让TextView能够水平滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道TextView嵌入在ScrollView中.如果String (其中不包含"\n")

I know the TextView is embedded in a ScrollView. And if there is a fairly long String(Which contains no "\n")

TextView将根据TextView的宽度自动进行换行.

The TextView will automatically do the line-wrap, according to the width of the TextView.

如果TextView的高度很短,那么我们可以垂直滚动它. 如何禁用自动换行?这样,如果没有遇到"\ n",则不会换行.而是让用户水平滚动以查看文本.

If TextView's height is short, then we are able to scroll it vertically. How do you disable the auto line-wrap? Such that, if there are no "\n" encounters, it does not line wrap. Rather, it lets the user scroll horizontally to view the text.

我该如何实现?

推荐答案

我想出了如何在许多人的帮助下做到这一点:D,谢谢,我们开始了!

I figure out how to do this with many helps of you guys :D, thanks, and here we go!

1..因此,首先,我们需要一个longlonglong字符串,对吗?

1. So, firstly, We need a longlonglong String, right?

let displayStr = "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 "

2.假设我们有一个UIScrollView,它通过@IBOutlet链接或通过调用.viewWithTag(xxx)获得,我们将其命名为scroll:

2. Assume we have a UIScrollView which is linked by @IBOutlet or got by calling the .viewWithTag(xxx) .We will let it be named as scroll :

3..现在该获取字符串的 size 了,这是我们将使用的关键功能:

3. It's time to get the size of our string, here is a key function we'll use it:

哦!我几乎忘了定义我们将使用哪种 Font (这是一个至关重要的参数),以及字符串大小的 max-size 是什么

Oh! I almost forget to define what kind of Font( this's a crucial parameter ) we will use, and What is the max-size of our string's size

let maxSize = CGSizeMake(9999, 9999)
let font = UIFont(name: "Menlo", size: 16)!
//key function is coming!!!
let strSize = (displayStr as NSString).boundingRectWithSize(maxSize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName : font], context: nil)

4.好,现在我们可以将字符串放入textView中,您需要以编程方式创建一个新的UITextView,以便我们可以定义与字符串大小相同的框架(哦,也许更大一些:D):

4. OK, now we can put the string into a textView, you need to programmatically create a new UITextView, so that we can define the frame which is identical to the string's size(Oh, maybe a little bigger :D) :

let frame = CGRectMake(0, 0, size.width+50, size.height+10)
let textView = UITextView(frame: frame)
textView.editable = false
textView.scrollEnabled = false//let textView becomes unScrollable
textView.font = font
textView.text = displayStr

5..回到我们的scroll,我们将其设置为contentSize:

5. Back to our scroll, we will set its contentSize:

scroll.contentSize = CGSizeMake(size.width, size.height)

6..最后,添加Subview: scroll.addSubview(textView)

6. Finally, addSubview: scroll.addSubview(textView)

您可以看到,textView嵌入在scrollView中,这使它可以 用2个方向滚动.

You can see, textView is embed in a scrollView, which allow it to scroll with 2 directions.

B.T.W.我的工具只是一个演示 静态字符串.如果您希望用户使用不会行的textView 如果他不输入任何"\ n",则换行,您可能需要动态计算 字符串大小. :D

B.T.W. My implement is just a demo for static String. if you want user to use a textView which will not line wrap if he doesn't input any "\n", you may need dynamically calculate the string size. :D

[我希望这会有所帮助]

[I hope this will help]

这篇关于如何让TextView能够水平滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆