简体   繁体   English

如何在UITextView中自动换行?

[英]How to word wrap text in a UITextView?

I have a UITextView contained within a UIScrollView. 我有一个包含在UIScrollView中的UITextView。 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. UITextView是根据UISegmentedControl动态加载的,但我不知道如何对其自动换行,因此该单词将移至下一行。

在此处输入图片说明

.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+中实现自动换行?

First off I think your textview looks a bit strange. 首先,我认为您的textview看起来有些奇怪。 Shouldn't there be a margin on the right side as well? 右侧是否也应留有边距? Now it sort of looks like the textview continues to the right. 现在看起来好像textview继续向右。 Are the frames correct or have you done something to the textContainerInset? 框架是否正确,或者您对textContainerInset做过什么?

Anyway to answer your question regarding changing linebreaks: a textview's textContainer property has a lineBreakMode which could be set to NSLineBreakByWordWrapping. 无论如何要回答有关更改换行符的问题:textview的textContainer属性具有lineBreakMode,可以将其设置为NSLineBreakByWordWrapping。 I think that should solve it 我认为应该解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM