如何将文本拆分为单独的 UITextView 页面?
一个子问题是:
如何确定 UITextview 的内置内部边距是多少?
我有一个很长的主文本字符串,我试图将其拆分为单独的 UITextView 页面,然后我可以在 UIScrollView 内从一个页面滚动到另一个页面。我使用以下方法来确定 UITextView 中字符串的高度以及该字符串是否超过高度限制:
-(NSNumber *)getHeightByWidth: (NSString *) myString
mySize: (UIFont *) mySize
myWidth: (NSNumber *) myWidth
{
int intMyWidth = [myWidth intValue];
CGSize boundingSize = CGSizeMake(intMyWidth, CGFLOAT_MAX);
CGSize requiredSize = [myString sizeWithFont:mySize constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
NSNumber *retNumber = [[NSNumber alloc] initWithFloat:requiredSize.height];
return retNumber;
[retNumber release];
}
我使用以下 cellFont 作为 mySize 的输入来调用 getHeightByWidth 方法:
UIFont *cellFont = [UIFont fontWithName:@"Arial" size:14.0];
UITextView 的宽度为 320 像素,但是我注意到文本并没有从左边缘延伸到右边缘,因为内部边距看起来每边大约有 10 个像素。因此,当我调用 getHeightByWidth 时,我设置 myWidth = (320 - 10 - 10);但是在构建适合 UITextView 的字符串后,最后一行通常会有间隙,可以用主字符串中的下一个单词填充。
谁能告诉我为什么使用 UITextView 的此过程会出现文本最后一行上的这些间隙?
A subquestion is:
How do I determine what the built-in internal margins of a UITextview are?
I have a long master string of text that I am trying to split into separate UITextView pages that I can then scroll from page to page inside a UIScrollView. I use the following method to determine what the height of a string in a UITextView is and whether the string is over the height limit:
-(NSNumber *)getHeightByWidth: (NSString *) myString
mySize: (UIFont *) mySize
myWidth: (NSNumber *) myWidth
{
int intMyWidth = [myWidth intValue];
CGSize boundingSize = CGSizeMake(intMyWidth, CGFLOAT_MAX);
CGSize requiredSize = [myString sizeWithFont:mySize constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
NSNumber *retNumber = [[NSNumber alloc] initWithFloat:requiredSize.height];
return retNumber;
[retNumber release];
}
I call the getHeightByWidth method using the following cellFont as the input for mySize:
UIFont *cellFont = [UIFont fontWithName:@"Arial" size:14.0];
The UITextView is 320 pixels wide, but I notice that the text doesn't go from the left edge to the right edge as there are internal margins which look to be around 10 pixels on each side. So when I call getHeightByWidth I set myWidth = (320 - 10 - 10); But after building strings to fit within the UITextView, there are usually gaps on the last row that could be filled with the next words in the master string.
Can anyone tell me why these gaps on the last row of the text occur using this process for UITextView?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
内置边距由属性
contentInset
表示。您也可以自己配置边距。
如果您在 IB 中有文本视图,请查找内容插图。值必须为 0,但仍显示一些余量。尝试将它们设置为负值,例如 -4 或 -8。
在代码中,执行类似的操作 -
您必须根据您认为合适的值设置这些值。
The built-in margins are represented by the property
contentInset
.Also you can configure the margins yourself.
If you have your text view in IB, look for Content insets. The values must be 0, but it still displays some margin. Trying setting them to negative values such as -4 or -8.
In the code, do something like-
You have to set these values according to what you find suitable.