Pangu
Pangu

Reputation: 3819

How to word wrap text in a UITextView?

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.

enter image description here

.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+?

Upvotes: 2

Views: 9296

Answers (1)

Yaser
Yaser

Reputation: 408

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?

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

Upvotes: 5

Related Questions