Powered By Blogger

Saturday, February 28, 2015

Attributed string for multi-purpose in iPhone SDK.

Hi,
This post is regarding the use of attributed string for multi-purpose. For more, please check below statements.

NSString *mainString = @"iphoneappcode.blogspot.com";
    NSMutableAttributedString * attributedString;
    UILabel *lbl;
    
    //-- Attributed string with Multiple Colour Combinations. - (NSForegroundColorAttributeName)
    
    attributedString = [[NSMutableAttributedString alloc] initWithString: mainString];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,13)];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(13,9)];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(22,4)];
    
    lbl = (UILabel *)[self.view viewWithTag:1001];
    lbl.attributedText = attributedString;
    
    //-- Attributed string with different size of font. - (NSFontAttributeName)
    
    attributedString = [[NSMutableAttributedString alloc] initWithString:mainString];
    [attributedString addAttribute:NSFontAttributeName
                             value:[UIFont fontWithName:@"Helvetica-Bold" size:16.0f]
                             range:NSMakeRange(5, 8)];
    
    lbl = (UILabel *)[self.view viewWithTag:1002];
    lbl.attributedText = attributedString;
    
    //-- Attributed string with foreground and background colours. -  (NSForegroundColorAttributeName)
    
    UIColor *_blue = [UIColor magentaColor];

    attributedString = [[NSMutableAttributedString alloc] initWithString:mainString];
    [attributedString addAttribute:NSForegroundColorAttributeName value:_blue range:NSMakeRange(0, [mainString length])];
    
    lbl = (UILabel *)[self.view viewWithTag:1003];
    lbl.attributedText = attributedString;
    
    //-- Attributed string with Underlined format. - (NSUnderlineStyleAttributeName)
    
    mainString = @"iphoneappcode.blogspot.com";
    attributedString = [[NSMutableAttributedString alloc] initWithString:mainString];
    [attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(0, [mainString length])];
    
    lbl = (UILabel *)[self.view viewWithTag:1004];
    lbl.attributedText = attributedString;
    
    //-- Attributed string with Background color. - (NSBackgroundColorAttributeName)
    
    mainString = @"iphoneappcode.blogspot.com";
    attributedString = [[NSMutableAttributedString alloc] initWithString:mainString];
    // Set background color for entire range
    [attributedString addAttribute:NSBackgroundColorAttributeName
                             value:[UIColor cyanColor]
                             range:NSMakeRange(0, [attributedString length])];
    
    lbl = (UILabel *)[self.view viewWithTag:1005];
    lbl.attributedText = attributedString;


Regards,

Nilesh M. Prajapati