Friday, September 19, 2014

NSMutableAttributedString Example



NSString *str = self.view2TextLbl.text;
NSRange range = [str rangeOfString:@"#"];


if(range.location != NSNotFound && range.length){


    UIFont *font1 = [UIFont myAlphaFontsBold:13];
    UIFont *font2 = [UIFont myAlphaFontsBold:14];
   
    UIColor *font1Color = [UIColor whiteColor];
    UIColor *font2Color = [UIColor blackColor];
   
    NSRange range1 = NSMakeRange(0, range.location);
    NSRange range2 = NSMakeRange(range.location, (str.length - range.location));
   
   
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:str];
   
    [attributedStr addAttribute:NSFontAttributeName value:font1 range:range1];
    [attributedStr addAttribute:NSFontAttributeName value:font2 range:range2];
   
    [attributedStr addAttribute:NSForegroundColorAttributeName value:font1Color range:range1];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:font2Color range:range2];
   
    self.view2TextLbl.attributedText = attributedStr;
 
}     

No comments: