简体   繁体   中英

How to add UILabel between two strings

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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