Tuesday, July 31, 2012

How to use NSAttributedString in iOS 6


infoString=@"This is an example of Attributed String";

NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:infoString];
NSInteger _stringLength=[infoString length];

UIColor *_black=[UIColor blackColor];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:30.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSForegroundColorAttributeName value:_black range:NSMakeRange(0, _stringLength)];



               






UIColor *_red=[UIColor redColor];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:72.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSStrokeColorAttributeName value:_red range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithFloat:3.0] range:NSMakeRange(0, _stringLength)];















UIColor *_red=[UIColor redColor];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:72.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSStrokeColorAttributeName value:_red range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithFloat:-3.0] range:NSMakeRange(0, _stringLength)];

















UIColor *_red=[UIColor redColor];
UIColor *_green=[UIColor greenColor];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:72.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSForegroundColorAttributeName value:_green range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSStrokeColorAttributeName value:_red range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithFloat:-3.0] range:NSMakeRange(0, _stringLength)];



















UIColor *_green=[UIColor greenColor];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:72.0f];

NSShadow *shadowDic=[[NSShadow alloc] init];
[shadowDic setShadowBlurRadius:5.0];
[shadowDic setShadowColor:[UIColor grayColor]];
[shadowDic setShadowOffset:CGSizeMake(0, 3)];
              
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSForegroundColorAttributeName value:_green range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSShadowAttributeName value:shadowDic range:NSMakeRange(0, _stringLength)];
                



















UIColor *_red=[UIColor redColor];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:72.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
  [attString addAttribute:NSForegroundColorAttributeName value:_red range:NSMakeRange(0, _stringLength)];              
  [attString addAttribute:NSKernAttributeName value:[NSNumber numberWithInt:5] range:NSMak








UIColor *_red=[UIColor redColor];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:30.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSForegroundColorAttributeName value:_red range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:2] range:NSMakeRange(0, _stringLength)];








UIColor *_blue=[UIColor blueColor];
UIColor *_blueL=[UIColor colorWithRed:0 green:0 blue:0.5 alpha:0.7];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:30.0f];
                
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSForegroundColorAttributeName value:_blue range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSBackgroundColorAttributeName value:_blueL range:NSMakeRange(0, 20)];


                

17 comments:

  1. Hi Reetu Raj

    i was stucked up over addingAttribute for attribute string,help me over that....
    when ever i call addattribute to attstr and when i add NSFontAttributeName getting error..help me over this

    ReplyDelete
  2. If you XCode doesn't recognize NSFontAttributeName, you are probably not using SDK 6.0

    ReplyDelete
  3. Hi Reetu Raj,

    Are you be able to use NSAttributedString in iOS 4.3? It gave me the error. Or is it only supported in iOS 6?

    ReplyDelete
  4. label is my UIlabel to which i want to add attributed text:

    label.attributedText = attString;

    OR

    [label setAttributedText: attString];

    Nice post, thanks a lot.

    ReplyDelete
  5. You can use either, its fine. Just keep it consistent throughout.

    ReplyDelete
  6. NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:@"Iamthebest"];
    UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:25.0f];
    [attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0,1)];


    mLbl.attributedText=attString;

    This will make Character I as BOLD

    ReplyDelete
  7. Thanks for great and simple to understand tutorial on attributed string.

    Regards
    Aman
    http://www.amaniphoneblog.com

    ReplyDelete
  8. Thanks your blog is awesome.
    thanks for sharing information.

    Videocon 2.75G speed

    Videocon Telecom invests approx Rs 130 Cr to upgrade its network to 2.75G EDGE, a 3G standard technology, to enhance Customer experience on data speed. Rolls out a full-fledged marketing campaign to get Customer attention.

    ReplyDelete
  9. Hai Good information shared about IOS. In future everything about ios . This blog post is really helpful. Please keep updated tutorials…

    ReplyDelete
  10. Good Information and having excellent techniques. Now a days these mobile Technologies are been very specially designed and maintained as to get security from different Hackers Well when I completed my Project Management Professional Training in kuwait I was supposed to handle certain projects where I was able to configure about the security issues in mobile terms well I was having specific terms to determine the Technologies at that moment
    Thankyou so much for the blog well i want to see more from you which really help me a lot

    ReplyDelete
  11. I need to still get it working w/ everything NSAttributedString has to offer, but I think fontName, fontSize and color cover the bulk of it. Expect lots of updates soon :)

    I can help you get started with this if you need. Also looking for feedback, so if it makes your life easier, I'd say mission accomplished.
    For More Professional Path refer below.
    Oracle SCM Course
    Oracle Financials Course
    Hadoop Admin Course
    Microsoft CRM Dynamics Course

    ReplyDelete
  12. Thanks for sharing your valuable time with this article.
    btelinks
    Police Result

    ReplyDelete