xcode 如何在 UITextView 中自动换行文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27086740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:14:54  来源:igfitidea点击:

How to word wrap text in a UITextView?

iosobjective-cxcodeuiscrollviewuitextview

提问by Pangu

I have a UITextView contained within a UIScrollView. The UITextView is loaded dynamically depending on the UISegmentedControl, but I don't know how to word wrap it so the word moves to the next line.

我有一个包含在 UIScrollView 中的 UITextView。UITextView 是根据 UISegmentedControl 动态加载的,但我不知道如何对其进行自动换行,以便单词移动到下一行。

enter image description here

在此处输入图片说明

.m:

.m:

@interface Tester ()
{
    UITextView *sponsor;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    sponsorInfo = [ [UITextView alloc] initWithFrame:self.sponsorsScrollView.frame];

    [sponsor setEditable:NO];
}

- (IBAction)control:(UISegmentedControl *)sender
{
    if (self.sponsorSegmentedControl.selectedSegmentIndex == 0)
    {
        [self changeGenericAbout:self.cellIndex];
    }
    else
    {
        //
    }
}

- (void)changeGenericAbout:(int)index
{
    if (index == 0)
    {
        sponsorInfo.text = @"[text in screen shot]";


        [sponsor sizeToFit];
        [self.sponsorsScrollView addSubview:sponsor];
    }
}

How can I achieve this word wrapping in iOS 7+?

我怎样才能在 iOS 7+ 中实现这个自动换行?

回答by Yaser

First off I think your textview looks a bit strange. Shouldn't there be a margin on the right side as well? Now it sort of looks like the textview continues to the right. Are the frames correct or have you done something to the textContainerInset?

首先,我认为您的 textview 看起来有点奇怪。右侧不应该也有边距吗?现在看起来 textview 继续向右。框架是否正确,或者您是否对 textContainerInset 进行了某些操作?

Anyway to answer your question regarding changing linebreaks: a textview's textContainer property has a lineBreakMode which could be set to NSLineBreakByWordWrapping. I think that should solve it

无论如何回答您关于更改换行符的问题:textview 的 textContainer 属性有一个 lineBreakMode,可以设置为 NSLineBreakByWordWrapping。我认为应该解决它