Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-09-2012, 12:46 AM   #1 (permalink)
Registered Member
 
manojb's Avatar
 
Join Date: Jan 2011
Posts: 263
manojb is on a distinguished road
Default UITextView - New line at WordWrap possible??

Hey guys,

Just wanna ask if there are any possibilities to add a '\n' character to the end of the line when a word wraps in a UITextView?

I am sure that RETURN ('\n') is not going to impact me on this.

--Edit--
Enter some text in UITextView. When the text view is resigning its first responder, append '\n' characters to the end of every line (the line in text view where the word wrapped) and obtain the complete content as a string.

Thanks in advance.
__________________
U can not fail, until U Quit...

Manoj B

Last edited by manojb; 02-10-2012 at 03:30 AM. Reason: -- bump --
manojb is offline   Reply With Quote
Old 02-09-2012, 06:03 AM   #2 (permalink)
Registered Member
 
manojb's Avatar
 
Join Date: Jan 2011
Posts: 263
manojb is on a distinguished road
Default Help me Pls !!!

Quote:
Originally Posted by manojb View Post
Hey guys,

Just wanna ask if there are any possibilities to add a '\n' character to the end of the line when a word wraps in a UITextView?

I am sure that RETURN ('\n') is not going to impact me on this.

Thanks in advance.
I got to this much (see code). Can someone suggest me what to proceed further ?

Code:
// create the complete range of the text content
    NSRange textRange = NSMakeRange(0, [textView text].length);
    NSRange delimiter;

// every word's length variable
    NSInteger wordsLength = 0;
    
    for (wordsLength = textRange.location; wordsLength <= [textView text].length; wordsLength = delimiter.location)
    {
        NSLog(@"WordsLength - %d, maxLimit - %f, textRange - %@", wordsLength, maxLimit, NSStringFromRange(textRange));
        
        // Check if a character is right after the cursor
        unichar c = ' '; // uni-codes as 32 - blank space
        
        if(wordsLength != [textView.text length])
        {
            // get the character at the position
            c = [textView.text characterAtIndex:textRange.location];
        }
        
        // If not a character, then cursor move to the end of a word
        if(c != 32 && c != 10)
        {
            delimiter = [textView.text rangeOfCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet] options:NSLiteralSearch 
                                                    range:NSMakeRange(textRange.location, [textView.text length] - textRange.location)];
            
            if(delimiter.location == NSNotFound)
            {
                delimiter.location = [textView.text length];
            }
        }
        else if (c == 32) // for blank space character add 1 to the delimiter
        {
            delimiter.location += 1;
        }
        
        textRange = delimiter;
    }
I am trying to find at which character, the line ends (with a word wrap).
__________________
U can not fail, until U Quit...

Manoj B
manojb is offline   Reply With Quote
Old 03-28-2012, 06:18 AM   #3 (permalink)
Registered Member
 
manojb's Avatar
 
Join Date: Jan 2011
Posts: 263
manojb is on a distinguished road
Default

Quote:
Originally Posted by manojb View Post
Hey guys,

Just wanna ask if there are any possibilities to add a '\n' character to the end of the line when a word wraps in a UITextView?

I am sure that RETURN ('\n') is not going to impact me on this.

--Edit--
Enter some text in UITextView. When the text view is resigning its first responder, append '\n' characters to the end of every line (the line in text view where the word wrapped) and obtain the complete content as a string.

Thanks in advance.
Bump. anybody got ideas ??
__________________
U can not fail, until U Quit...

Manoj B
manojb is offline   Reply With Quote
Old 04-03-2012, 12:21 AM   #4 (permalink)
Registered Member
 
manojb's Avatar
 
Join Date: Jan 2011
Posts: 263
manojb is on a distinguished road
Default

This can wrap sentences (spaced words) and long (not spaced) words.. But not when a combination of both are available.

Pass YES to the flag if the content needs to be wrapped as words or NO if the content is a long word.

Code:
- (void) insertCarriageString:(NSArray*)wordsArray asWords:(BOOL)flag inView:(UITextView *)textView
{
    NSMutableString *replacedString = [NSMutableString stringWithString:@""];
    CGFloat spaceWidth = [@" " sizeWithFont:textView.font].width;
    CGFloat wordWidth = 0;
    CGFloat availableSize = textView.frame.size.width - (2 * textView.contentOffset.x);
    
    for (NSString *word in wordsArray)
    {
        wordWidth = [word sizeWithFont:textView.font].width;
        if (flag) wordWidth += spaceWidth;
        
        if (wordWidth > availableSize)
        {
            [replacedString appendString:@"\r"];
            NSLog(@"wordwidth %6.2f - available %6.2f - word %@", wordWidth, availableSize, word);
            availableSize = textView.frame.size.width - (2 * textView.contentOffset.x) - wordWidth;
            wordWidth = 0;
        }
        else
        {
            availableSize -= wordWidth;
            NSLog(@"wordwidth %6.2f - available %6.2f - word %@", wordWidth, availableSize, word);
        }
        
        [replacedString appendString:word];
        if (flag) [replacedString appendString:@" "];
    }

//    NSLog(@"replacedString - \n%@", [replacedString stringByReplacingOccurrencesOfString:@"\r" withString:@"<< NEW >>\r"]);
}
__________________
U can not fail, until U Quit...

Manoj B
manojb is offline   Reply With Quote
Reply

Bookmarks

Tags
textview, uitextview, word wrap, wordwrap

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 400
12 members and 388 guests
chiataytuday, chits12345, fiftysixty, gmarro, iOS.Lover, KennyChong, kilobytedump, Leslie80, Matrix23, ryantcb, xerohuang, Yosh_K
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,670
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Yosh_K
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:23 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0