创建笔记视图的最佳方法
我正在编写一个 iPhone/iPad 应用程序,允许用户填写笔记的空白。每周的笔记都会有所不同。我看到从我们的网络服务器下载 XML 格式的笔记。我正在努力解决的是如何动态构建一个可以对齐文本和输入字段的视图,类似于下面的示例。
示例图片 http://www.ccvonline.com/downloads/ios/example.png< /a>
我需要让输入控件与文本内联。这可以用 Core Text 来完成吗?有示例代码吗?现在我正在考虑使用 UIWebView 并用 HTML/CSS 构建它。但这感觉有点像作弊。有人有其他想法吗?
I'm writing an iPhone/iPad application that will allow a user to fill in blanks for notes. The notes will be different each week. I see downloading the notes in an XML format from our webserver. What I'm struggling with is how to dynamically build a View that can align the text and input fields similar to the sample below.
sample image http://www.ccvonline.com/downloads/ios/example.png
I need to have the input controls inline with the text. Can this be done with Core Text? Any sample code? Right now I'm thinking about using an UIWebView and build it in HTML/CSS. But this feels a bit like cheating. Anyone have another idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从当前行的 NSString 的
sizeWithFont:
方法开始。下载笔记时,确保每个笔记分为两部分,文本字段之前的部分和文本字段之后的部分。创建该大小的标签并将其添加到视图层次结构中。
通过一些简单的算术,您现在知道第一个标签右端的位置。然后您可以添加到文本字段中。您可能需要摆弄框架才能使其正确对齐 - 如果您想要一个好的 UI,请不要忽略这一步!
然后,通过更多的数学运算,您可以创建第二个标签来显示第二部分。该代码与第一段代码类似,不同之处在于要计算 x 位置,您需要执行
22.0 + textSize.width + 5.0 + 50.0 + 5.0
来考虑所有现有的元素。支持换行会很困难。您可能必须在换行符处手动剪切字符串才能使其适合。上面的代码只处理一行,因为
sizeWithFont:
方法只能返回单个矩形。如果您有多于一行,它永远不会返回小于文本整个宽度的内容,从而使其不太有用。This is probably the easiest method to visualize. An alternative method which would make it more efficient especially while scrolling (by cutting out one of three labels) is to add a bunch of blank space in the string and have one label go across the whole screen. You'd probably want a method that calculates the number of blank spaces to make up a certain width for the text field, and then adjust your x-coordinates as necessary. I leave that up to you, if you'd like to try that.
Start with NSString's
sizeWithFont:
method for your current line. When the notes are downloaded, make sure each note is in two parts, the part before and the part after the text field.Create a label of that size and add it to the view hierarchy.
With some simple arithmetic, you now know the location of the right end of the first label. You can then add in a text field. You may have to fiddle with the frame to make it line up properly—if you want a good UI, don't overlook this step!
Then with a bit more math you can create a second label to display the second part. The code will be similar to the first block of code, except that to calculate the x-location you'll need to do
22.0 + textSize.width + 5.0 + 50.0 + 5.0
to account for all the existing elements.Supporting line wrapping would be difficult. You may have to manually cut the string at a line break to make it fit. The above code only deals with one line, as the
sizeWithFont:
method can only return a single rectangle. If you have more than one line, it'll never return something less than the entire width of the text, making it somewhat less useful.This is probably the easiest method to visualize. An alternative method which would make it more efficient especially while scrolling (by cutting out one of three labels) is to add a bunch of blank space in the string and have one label go across the whole screen. You'd probably want a method that calculates the number of blank spaces to make up a certain width for the text field, and then adjust your x-coordinates as necessary. I leave that up to you, if you'd like to try that.
如果您有开始和结束文本,您可以尝试使用 NSString 的
-sizeWithFont:constrainedToSize:
或-sizeWithFont:constrainedToSize:lineBreakMode:
。此外,您还可以在 XML 文件中添加一个参数,该参数给出每个元素所需的宽度。If you have the beginning and ending texts you can try using the
-sizeWithFont:constrainedToSize:
or-sizeWithFont:constrainedToSize:lineBreakMode:
of NSString. Also, you could add a parameter in your XML file that gives the required width of each element.考虑到除了该屏幕截图之外没有任何要求,示例代码将很困难,该屏幕截图一开始似乎安排得不太好。
最简单的方法可能就是获取文本,将其分解为单词,然后将它们排列在可用的空间中。这是一种限制性的观点,还是其他什么。您还需要注释数据来确定空白区域的位置。这可能只是一个索引(即,如果它是单词 4, 4)。
只需子类化视图并使用核心图形即可。
初级版本:
这会产生以下结果:
该项目是一个简单的视图,我设置了一个
UIView
作为文本绑定的参考框架,以及我移动到位的UITextField
。您可以在屏幕上复制多个这样的句子。空白只是一个填充符,以简化循环处理,索引决定了框的显示位置。Sample code would be difficult considering no reqs beyond that screenshot, which doesn't appear to be arranged very well to begin with.
The easiest way may just be to get the text, break it into words and then arrange them in the space you have available. Be that a constraining view, or whatever. You'll also want to have note data to determine where the blank space will be. This could just be an index (i.e. if its word 4, 4).
Just subclass a view and use core graphics.
Rudimentary version:
This produces this:
The project is a simple view, I set up a
UIView
as the reference frame upon which the text is bound, and aUITextField
that I move into place. You can duplicate for more than one such sentence on screen. The blank is just a filler to simplify the loop processing, the index is what determines where the box is shown.