Highlight UITextView text when centering text

I use the following code to highlight the text in mine UITextView- every time the text changes, I convert it to NSMutableAttributedStringso that I can focus it, add a selection and set the font.

Creature UITextView

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(40, 40, 300, 100)];
textView.center = self.view.center;
textView.font = [UIFont fontWithName:@"RopaSans-Italic" size:30];
textView.backgroundColor = [UIColor clearColor];
textView.textColor = [UIColor whiteColor];
textView.textAlignment = NSTextAlignmentCenter;
textView.text = [@"" uppercaseString];
textView.delegate = self;
[self.view addSubview:textView];

Delegate method

-(void)textViewDidChange:(UITextView *)textView{

    textView.text = [textView.text uppercaseString];
    NSString *string = textView.text;

    NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:textView.text];
    NSMutableParagraphStyle *paragrapStyle = [[NSMutableParagraphStyle alloc] init];
    paragrapStyle.alignment = NSTextAlignmentCenter;
    paragrapStyle.lineBreakMode = NSLineBreakByWordWrapping;

    [att addAttribute:NSParagraphStyleAttributeName value:paragrapStyle range:[textView.text rangeOfString:textView.text]];
    [att addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:[textView.text rangeOfString:textView.text]];
    [att addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"RopaSans-Italic" size:30] range:[textView.text rangeOfString:textView.text]];
    [att addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:[textView.text rangeOfString:textView.text]];

    //Find all occurencies of newline
    NSString *substring = @"\n";
    NSRange searchRange = NSMakeRange(0,string.length);
    NSRange foundRange;
    while (searchRange.location < string.length) {
        searchRange.length = string.length-searchRange.location;
        foundRange = [string rangeOfString:substring options:nil range:searchRange];
        if (foundRange.location != NSNotFound) {

            [att addAttribute:NSBackgroundColorAttributeName value:[UIColor clearColor] range:foundRange];

            searchRange.location = foundRange.location+foundRange.length;
        } else {
            break;
        }
    }

    textView.attributedText = att;

}

, , UITextView, , , (. 1). , (. 1). , "" , \n NSBackgroundColorAttributeName [UIColor clearColor]. , 2. (\ r \r\n), , UITextView, kinda , , "Enter" .

? Figure 1Figure 2

+4

All Articles