Wednesday, 17 April 2013

Working with NSMutableAttributedString


Code snippet to demonstrate how to use NSMutableAttributedString:


    UILabel *lblTitle = [[UILabel alloc] init];
    [lblTitle setFrame:CGRectMake(2, 180, 320, 25)];
    [self.view addSubview:lblTitle];
    
    NSString *strHello = @"Hello, this is a Demo of NSMutableAttributedString";
    NSRange range;
    NSMutableAttributedString *stringAttributed = [[NSMutableAttributedString alloc]initWithString:strHello];
    [stringAttributed addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:14.0] range:NSMakeRange(0, [strHello length])];
    range = [strHello rangeOfString:@"NSMutableAttributedString"];
    [stringAttributed addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13.0] range:range];
    range = [strHello rangeOfString:@"Demo"];
    [stringAttributed addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
    [lblTitle setAttributedText:stringAttributed];



    Follow link for help: Apple Documentation


No comments:

Post a Comment