0

i have this code to show some texts in a UITextview..i just want to add a UILabel between these strings.i am getting text in this formate, 1 this is first text 2 this is second text 3 this is third text,,i want to add or append UILabel between numeric character and text.my code for showing text is

 NSMutableString *combined = [NSMutableString string];

    for(NSUInteger idx = 0; idx < [delegatee.allSelectedVerseEnglish count]; idx++) {
        [combined appendFormat:@"   %d %@", 
         idx + 1, 
         [delegatee.allSelectedVerseEnglish objectAtIndex:idx]];

    }
    NSNumberFormatter * f = [[NSNumberFormatter alloc] init]; 
    NSNumber * n = [f numberFromString:combined]; 
    NSLog(@"N: %@", n);
       maintextview.text =combined;

combined is the text in the above formate ,and maintextview is the UITextview.

am also getting the string range between two sentence

- (void)textViewDidEndEditing:(UITextView *)textView
{
    if (textView == maintextview) {

    mainpopupview.frame =CGRectMake(0, 0, 768, 1004)    ;
    [self.view addSubview:mainpopupview];

    NSRange selectedRange = [textView selectedRange];
    NSString *backString = [maintextview.text substringToIndex:selectedRange.location];
    NSRange backRange = [backString rangeOfString:@"   " options:NSBackwardsSearch];
    NSRange backRangee = [backString rangeOfString:@"   " options:NSBackwardsSearch];
    int  myRangeLenght = backRangee.location - backRange.location;
    NSRange myStringRange = NSMakeRange (backRange.location, myRangeLenght);
    NSString *forwardString  = [maintextview.text substringFromIndex:backRange.location];
    NSLog(@"%@",[[forwardString componentsSeparatedByString:@"   "] objectAtIndex:1]);
    NSLog (@"%@",  [maintextview.text substringWithRange:myStringRange]);

    NSString * myStringTxt = [[forwardString componentsSeparatedByString:@"   "] objectAtIndex:1];
    NSLog(@"1 %@", myStringTxt);

    //  maintextview.textColor = [UIColor yellowColor];

    NSRange myStringRangee = [maintextview.text rangeOfString:myStringTxt];
    [maintextview select:self];
    maintextview.selectedRange = myStringRangee;

is there any way to do this. Thanks in advance.

stackiphone
  • 1,245
  • 3
  • 19
  • 41
  • Just a question, you are making a new string after editing to place back into your UITextView? Why do you wish to have a separate UILabel in-between, versus simply adding the text (you wish to be in the UILable) into the string you are pushing back into the UITextView? – trumpetlicks Jun 01 '12 at 16:47
  • @trumpetlicks haii,thanks for the reply,i want the uilabel becz,,this is the story..the verse number need to be different color ,but in uitextvierw it is not possible to add two colors,so if i get the uilabel i can change the label text color and make the verse number in uilabel.ok? – stackiphone Jun 01 '12 at 16:50
  • OK, so you wish the text pieces to LOOK different :-) You are already implementing the textViewDidEndEditing delegate routine. How have you defined you TextView(s) (in a xib file, or have you created them dynamically in code)? – trumpetlicks Jun 01 '12 at 16:57
  • @trumpetlicks i defined the textview by xib – stackiphone Jun 01 '12 at 17:55
  • In this case, you could add your UILabel in the xib file as well, and simply reposition and color it (in code) based upon the input text. There are ways of getting the pixel based size of an NSString based upon font parameters that can aid you in sizing your UITextField and positioning your UILabel after the text has been edited. Link to some good stuff. https://discussions.apple.com/thread/1622739?start=0&tstart=0 – trumpetlicks Jun 01 '12 at 18:12
  • Here is also a GREAT link to a Growing Textview. I imagine you could borrow this code and manipulate it to work also for a UILabel. This way you could Grow (or shrink) your UITextField on the leftmost, position your UILabel to the right of that, then get the measure size of the UILabel to position your rightmost UITextField. – trumpetlicks Jun 01 '12 at 18:19

1 Answers1

0

I don't know what user interaction you need here, but if you're just displaying the text for the user, but could you replace your UITextView with a UIWebView (loading static text via loadHTMLString:baseURL:, using html markup to set the color of the text)? I refer to the UITextView documentation:

This class does not support multiple styles for text. The font, color, and text alignment attributes you specify always apply to the entire contents of the text view. To display more complex styling in your application, you need to use a UIWebView object and render your content using HTML.

If you really need to do this with separate UI controls (much more complicated than just using a UIWebView) you don't "insert" the UILabel in the text, but rather you'd probably have one control for the text up to the point of the color change, another control for the text of a different color or what have you, another control for the rest of the text. It would get hairly pretty quickly (unless it was laid out like a grid, and even then it's a hassle compared to a UIWebView).

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • 1
    I think that we can assume that stackiphone is live editing the text. Unfortunately a UIWebView is not an input capable text control. If he wishes to input text, he will most likely need to do what he is trying to do without a UIWebView. Even if UIWebView was input capable, he would then have to somehow get in the middle of the text input stream, and dynamically create the HTML Tagging around the text for his display needs to work properly. – trumpetlicks Jun 01 '12 at 18:07