Upgrade to Pro — share decks privately, control downloads, hide ads and more …

NSAttributedString for iOS

NSAttributedString for iOS

NSAttributedString for iOS in iOS Meetup Tokyo

Non Umemoto

October 04, 2012
Tweet

More Decks by Non Umemoto

Other Decks in Programming

Transcript

  1. UILabel *label = [[UILabel alloc] init]; ~~ some codes for

    label ~~ label.text = @"Listen to the web"; 12೥10݄5೔༵ۚ೔
  2. iOS6 You have more power with NSAttributedString There are a

    lot of hassles for using CoreText, right? 12೥10݄5೔༵ۚ೔
  3. UILabel *label = [[UILabel alloc] init]; ~~ some codes for

    label ~~ NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:@"Listen to the web"]; NSInteger stringLength = [attString length]; [attString addAttribute:NSKernAttributeName value:@5 range:NSMakeRange(0, stringLength)]; label.attributedText = attString; 12೥10݄5೔༵ۚ೔
  4. NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:@"Listen to the web"]; UIColor

    *_red = [UIColor redColor]; [attString addAttribute:NSForegroundColorAttributeName value:_red range:NSMakeRange(0, 6)]; label.attributedText = attString; 12೥10݄5೔༵ۚ೔
  5. NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:@"Listen to the web"]; [attString

    addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:2] range:NSMakeRange(10, 7)]; label.attributedText = attString; 12೥10݄5೔༵ۚ೔
  6. Available in UIKit • UITextView • UITextField • UILabel •

    UIButton • UITableView • UIPickerVIew for example... 12೥10݄5೔༵ۚ೔
  7. Available Attributes • Font • Text color • Paragraph style

    • Text background color • Ligature, kerning, and baseline offset • Underline and strike-through • Stroke width and color • Shadow 12೥10݄5೔༵ۚ೔
  8. One more thing A little bit advanced talk from now,

    (and personal) 12೥10݄5೔༵ۚ೔
  9. On my web speaker app, I wanna highlight the text

    being spoken in real time. (Without using CoreText.) This is UITextView. 12೥10݄5೔༵ۚ೔
  10. Unfortunately, we need to reset UITextView every time setting a

    new attributedString. textView.attributedText = attString So, it can be too SLOW for long text. (It must be ok for short text) 12೥10݄5೔༵ۚ೔
  11. Thank you If you are interested, check out the session

    of WWDC2012 to find out more. “Session 222 - Introduction to Attributed Strings for iOS” 12೥10݄5೔༵ۚ೔