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

Session 222 - Introduction to Attributed String...

wwdcman
June 25, 2012
120

Session 222 - Introduction to Attributed Strings for iOS

wwdcman

June 25, 2012
Tweet

Transcript

  1. These are confidential sessions—please refrain from streaming, blogging, or taking

    pictures Session 222 Introduction to Attributed Strings for iOS Aki “I ⍰ Unicode” Inoue Cocoa Engineer Drawing strings with dramatic expression
  2. What Is an Attributed String ? Associating attributes to characters

    Helvetica Neue Helvetica Neue Helvetica Neue + Yellow I’m an attributed string
  3. What Is an Attributed String ? Associating attributes to characters

    Times New Roman + Underline Helvetica Neue Helvetica Neue + Yellow I’m an attributed string Helvetica Neue Italic
  4. What Can You Do with It? Multi-style text An Apple

    Engineer Abducted by An Alien Spacecraft A spacecraft landed at the Cuperti… TheNews - 6/13/12 at 3:18 PM An Apple Engineer Abducted by An Alien Spacecraft A spacecraft landed at the Cupert… TheNews - 6/13/12 at 3:18 PM
  5. What Can You Do with It? Multi-style text An Apple

    Engineer Abducted by An Alien Spacecraft A spacecraft landed at the Cuperti… TheNews - 6/13/12 at 3:18 PM An Apple Engineer Abducted by An Alien Spacecraft A spacecraft landed at the Cupert… TheNews - 6/13/12 at 3:18 PM
  6. What Can You Do with It? Multi-style text An Apple

    Engineer Abducted by An Alien Spacecraft A spacecraft landed at the Cuperti… TheNews - 6/13/12 at 3:18 PM An Apple Engineer Abducted by An Alien Spacecraft A spacecraft landed at the Cupert… TheNews - 6/13/12 at 3:18 PM
  7. •An attribute dictionary per character - (NSString *)string - (NSDictionary

    *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range; •Instantiating with a string and attributes dictionary NSAttributedString - (id)initWithString:(NSString *)string attributes:(NSDictionary *)attrs;
  8. •An attribute dictionary per character - (NSString *)string - (NSDictionary

    *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range; •Instantiating with a string and attributes dictionary NSAttributedString - (id)initWithString:(NSString *)string attributes:(NSDictionary *)attrs;
  9. Specifying a font Instantiating an Attributed String •Using NSFontAttributeName NSAttributedString

    *string = [[NSAttributedString alloc] ! ! initWithString:@”Hello World!!” ! ! attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:12.0f }];
  10. Specifying a font Instantiating an Attributed String Hello World!! •Using

    NSFontAttributeName NSAttributedString *string = [[NSAttributedString alloc] ! ! initWithString:@”Hello World!!” ! ! attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:12.0f }];
  11. •An attribute dictionary per character - (NSString *)string - (NSDictionary

    *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range; •Instantiating with a string and attributes dictionary - (id)initWithString:(NSString *)string attributes:(NSDictionary *)attrs; NSAttributedString
  12. •An attribute dictionary per character - (NSString *)string - (NSDictionary

    *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range; •Instantiating with a string and attributes dictionary - (id)initWithString:(NSString *)string attributes:(NSDictionary *)attrs; •No attribute for an empty string NSAttributedString
  13. •Mutable methods for attributes - (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range; - (void)addAttributes:(NSDictionary

    *)attrs range:(NSRange)range; - (void)addAttribute:(NSString *)key value:(id)value range:(NSRange)range; - (void)removeAttribute:(NSString *)key range:(NSRange)range; Changing attributes NSMutableAttributedString
  14. •Mutable methods for attributes - (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range; - (void)addAttributes:(NSDictionary

    *)attrs range:(NSRange)range; - (void)addAttribute:(NSString *)key value:(id)value range:(NSRange)range; - (void)removeAttribute:(NSString *)key range:(NSRange)range; •Modifying existing attributes •Creating multi-style string Changing attributes NSMutableAttributedString
  15. Emphasizing a range Creating Multi-style String •Using NSForegroundColorAttributeName NSMutableAttributedString *string

    = [[NSMutableAttributedString alloc] ! ! initWithString:@”Dentist at 9:15am.” ! ! attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:12.0f }]; [string addAttribute:NSForegroundColorAttributeName ! ! value:[UIColor redColor] ! ! range:NSMakeRange(11, 6)]; // Change the time, 9:15am, to red
  16. Emphasizing a range Creating Multi-style String •Using NSForegroundColorAttributeName NSMutableAttributedString *string

    = [[NSMutableAttributedString alloc] ! ! initWithString:@”Dentist at 9:15am.” ! ! attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:12.0f }]; [string addAttribute:NSForegroundColorAttributeName ! ! value:[UIColor redColor] ! ! range:NSMakeRange(11, 6)]; // Change the time, 9:15am, to red Dentist at 9:15am.
  17. Emphasizing a range Creating Multi-style String •Using NSForegroundColorAttributeName NSMutableAttributedString *string

    = [[NSMutableAttributedString alloc] ! ! initWithString:@”Dentist at 9:15am.” ! ! attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:12.0f }]; [string addAttribute:NSForegroundColorAttributeName ! ! value:[UIColor redColor] ! ! range:NSMakeRange(11, 6)]; // Change the time, 9:15am, to red Dentist at 9:15am. •Using NSForegroundColorAttributeName NSMutableAttributedString *string = [[NSMutableAttributedString alloc] ! ! initWithString:@”Dentist at 9:15am.” ! ! attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:12.0f }]; [string addAttribute:NSForegroundColorAttributeName ! ! value:[UIColor redColor] ! ! range:NSMakeRange(11, 6)]; // Change the time, 9:15am, to red
  18. Emphasizing a range Creating Multi-style String •Using NSForegroundColorAttributeName NSMutableAttributedString *string

    = [[NSMutableAttributedString alloc] ! ! initWithString:@”Dentist at 9:15am.” ! ! attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:12.0f }]; [string addAttribute:NSForegroundColorAttributeName ! ! value:[UIColor redColor] ! ! range:NSMakeRange(11, 6)]; // Change the time, 9:15am, to red Dentist at 9:15am.
  19. •Mutable methods for string contents - (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str; -

    (void)replaceCharactersInRange:(NSRange)range ! ! ! ! withAttributedString:(NSAttributedString *)attributedString; - (void)insertAttributedString:(NSAttributedString *)attributedString ! ! ! ! ! ! ! atIndex:(NSUInteger)location; - (void)appendAttributedString:(NSAttributedString *)attributedString; - (void)deleteCharactersInRange:(NSRange)range; Modifying string contents NSMutableAttributedString
  20. •Mutable methods for string contents - (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str; -

    (void)replaceCharactersInRange:(NSRange)range ! ! ! ! withAttributedString:(NSAttributedString *)attributedString; - (void)insertAttributedString:(NSAttributedString *)attributedString ! ! ! ! ! ! ! atIndex:(NSUInteger)location; - (void)appendAttributedString:(NSAttributedString *)attributedString; - (void)deleteCharactersInRange:(NSRange)range; Modifying string contents NSMutableAttributedString
  21. - (void)replaceCharactersInRange:(NSRange)range ! ! ! ! withAttributedString:(NSAttributedString *)attributedString; - (void)insertAttributedString:(NSAttributedString

    *)attributedString ! ! ! ! ! ! ! atIndex:(NSUInteger)location; - (void)appendAttributedString:(NSAttributedString *)attributedString; - (void)deleteCharactersInRange:(NSRange)range; Modifying string contents NSMutableAttributedString •Mutable methods for string contents - (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str;
  22. •Moving the appointment time 1 hour early NSMutableAttributedString *string; //

    a mutable string [string replaceCharactersInRange:NSMakeRange(11, 6) withString:@”8:15am”]; Modifying String Contents
  23. •Moving the appointment time 1 hour early NSMutableAttributedString *string; //

    a mutable string [string replaceCharactersInRange:NSMakeRange(11, 6) withString:@”8:15am”]; Dentist at 9:15am. Modifying String Contents
  24. •Moving the appointment time 1 hour early NSMutableAttributedString *string; //

    a mutable string [string replaceCharactersInRange:NSMakeRange(11, 6) withString:@”8:15am”]; Modifying String Contents Dentist at 8:15am.
  25. •Moving the appointment time 1 hour early NSMutableAttributedString *string; //

    a mutable string [string replaceCharactersInRange:NSMakeRange(11, 6) withString:@”8:15am”]; Modifying String Contents Dentist at 8:15am.
  26. Modifying String Contents Dentist at 8:15am. •Moving the appointment time

    1 hour early NSMutableAttributedString *string; // a mutable string [string replaceCharactersInRange:NSMakeRange(11, 6) withString:@”8:15am”];
  27. Modifying String Contents Dentist at 8:15am. •Moving the appointment time

    1 hour early NSMutableAttributedString *string; // a mutable string [string replaceCharactersInRange:NSMakeRange(11, 6) withString:@”8:15am”];
  28. •Attributes are preserved during string content replacement •Three rules to

    remember 1. When replacing, the new characters inherit attributes from the first character of the range being replaced Attributes Preserved 1. When replacing, the new characters inherit attributes from the first character of the range being replaced
  29. •Attributes are preserved during string content replacement •Three rules to

    remember 1. When replacing, the new characters inherit attributes from the first character of the range being replaced 2. When inserting, the incoming string inherit from the previous character Attributes Preserved 2. When inserting, the incoming string inherit from the previous character
  30. •Attributes are preserved during string content replacement •Three rules to

    remember 1. When replacing, the new characters inherit attributes from the first character of the range being replaced 2. When inserting, the incoming string inherit from the previous character 3. When inserting at the beginning, the attributes at index 0 are used Attributes Preserved 3. When inserting at the beginning, the attributes at index 0 are used
  31. •Attributes are preserved during string content replacement •Three rules to

    remember 1. When replacing, the new characters inherit attributes from the first character of the range being replaced 2. When inserting, the incoming string inherit from the previous character 3. When inserting at the beginning, the attributes at index 0 are used NOTE: Remember the “no character, no attributes” rule Attributes Preserved
  32. •Insertion [string replaceCharactersInRange:NSMakeRange(3, 0) withString:@”ABC”]; Replacement rule samples Modifying String

    Contents defghi •Insertion [string replaceCharactersInRange:NSMakeRange(3, 0) withString:@”ABC”]; abc
  33. •Insertion [string replaceCharactersInRange:NSMakeRange(3, 0) withString:@”ABC”]; Replacement rule samples Modifying String

    Contents defghi •Insertion [string replaceCharactersInRange:NSMakeRange(3, 0) withString:@”ABC”]; ABC abc
  34. •Insertion [string replaceCharactersInRange:NSMakeRange(3, 0) withString:@”ABC”]; Replacement rule samples Modifying String

    Contents defghi •Insertion [string replaceCharactersInRange:NSMakeRange(3, 0) withString:@”ABC”]; abcABC
  35. •No character, no attributes [string deleteCharactersInRange:NSMakeRange(0, 9)]; [string replaceCharactersInRange:NSMakeRange(0, 0)

    withString:@”ABC”]; Replacement rule samples Modifying String Contents abcdefghi •No character, no attributes [string deleteCharactersInRange:NSMakeRange(0, 9)]; [string replaceCharactersInRange:NSMakeRange(0, 0) withString:@”ABC”];
  36. •No character, no attributes [string deleteCharactersInRange:NSMakeRange(0, 9)]; [string replaceCharactersInRange:NSMakeRange(0, 0)

    withString:@”ABC”]; Replacement rule samples Modifying String Contents ABC [string replaceCharactersInRange:NSMakeRange(0, 0) withString:@”ABC”];
  37. Querying Attributes •An attribute dictionary per character - (NSString *)string

    - (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range;
  38. Querying Attributes •An attribute dictionary per character - (NSString *)string

    - (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range;
  39. Querying Attributes •An attribute dictionary per character - (NSString *)string

    - (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range;
  40. Querying Attributes Not guaranteed to return the maximum range •An

    attribute dictionary per character - (NSString *)string - (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range;
  41. •An attribute dictionary per character - (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range; NSRange

    range; NSDictionary *dict = [string attributesAtIndex:4 effectiveRange:&range]; Querying Attributes abcdefghi
  42. •An attribute dictionary per character - (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range; NSRange

    range; NSDictionary *dict = [string attributesAtIndex:4 effectiveRange:&range]; Querying Attributes abcdefghi NSRange range; NSDictionary *dict = [string attributesAtIndex:4 effectiveRange:&range];
  43. •An attribute dictionary per character - (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range; NSRange

    range; NSDictionary *dict = [string attributesAtIndex:4 effectiveRange:&range]; Querying Attributes abcdefghi
  44. •An attribute dictionary per character - (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range; NSRange

    range; NSDictionary *dict = [string attributesAtIndex:4 effectiveRange:&range]; Querying Attributes abcdefghi &range
  45. •An attribute dictionary per character - (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range; NSRange

    range; NSDictionary *dict = [string attributesAtIndex:4 effectiveRange:&range]; Querying Attributes abcdefghi &range {3, 3}
  46. •An attribute dictionary per character - (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range; NSRange

    range; NSDictionary *dict = [string attributesAtIndex:4 effectiveRange:&range]; Querying Attributes abcdefghi &range {4, 2}
  47. NSRange range; NSDictionary *dict = [string attributesAtIndex:4 effectiveRange:&range]; •An attribute

    dictionary per character - (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range; Querying Attributes abcdefghi &range {4, 1}
  48. Using longest effective range methods Getting Contiguous Run Range •Variants

    of attribute query methods - (NSDictionary *)attributesAtIndex:(NSUInteger)location ! ! ! ! longestEffectiveRange:(NSRangePointer)range inRange:(NSRange)maximumRange; - (id)attribute:(NSString)attribute atIndex:(NSUInteger)location ! ! ! ! longestEffectiveRange:(NSRangePointer)range inRange:(NSRange)maximumRange;
  49. •Getting the contiguous range for attributes abcdefghi Using longest effective

    range methods Getting Contiguous Run Range Helvetica Neue Red
  50. •Getting the contiguous range for attributes abcdefghi Using longest effective

    range methods Getting Contiguous Run Range Helvetica Neue Blue
  51. •Getting the contiguous range for attributes abcdefghi Using longest effective

    range methods Getting Contiguous Run Range Helvetica Neue Italic Blue
  52. abcdefghi •Getting the contiguous range for attributes Using longest effective

    range methods Getting Contiguous Run Range Helvetica Neue Italic Green
  53. abcdefghi •Getting the contiguous range for NSFontAttributeName Using longest effective

    range methods Getting Contiguous Run Range Helvetica Neue
  54. abcdefghi •Getting the contiguous range for NSFontAttributeName Using longest effective

    range methods Getting Contiguous Run Range Helvetica Neue Italic
  55. •Enumeration methods -enumerateAttributesInRange:options:usingBlock: -enumerateAttribute:inRange:options:usingBlock: •Changing attributes based on existing attributes

    •Modifying string contents while preserving the attributes •Modifying string contents while preserving the attributes Querying Attributes
  56. Uppercase while preserving attributes Enumerating for Modification NSMutableAttributedString *attrString; NSLocale

    *locale = [NSLocale currentLocale]; [attrString enumerateAttributesInRange:NSMakeRange(0, [attrString length]) ! options:0 ! usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) { ! ! NSString *string = [[attrString string] substringWithRange:range]; ! ! [attrString replaceCharactersInRange:range ! ! ! ! ! withString:[string uppercaseStringWithLocale:locale]]; }]; abcdefghi abcdefghi
  57. Uppercase while preserving attributes Enumerating for Modification NSMutableAttributedString *attrString; NSLocale

    *locale = [NSLocale currentLocale]; [attrString enumerateAttributesInRange:NSMakeRange(0, [attrString length]) ! options:0 ! usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) { ! ! NSString *string = [[attrString string] substringWithRange:range]; ! ! [attrString replaceCharactersInRange:range ! ! ! ! ! withString:[string uppercaseStringWithLocale:locale]]; }]; abcdefghi abcdefghi
  58. Uppercase while preserving attributes Enumerating for Modification NSMutableAttributedString *attrString; NSLocale

    *locale = [NSLocale currentLocale]; [attrString enumerateAttributesInRange:NSMakeRange(0, [attrString length]) ! options:0 ! usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) { ! ! NSString *string = [[attrString string] substringWithRange:range]; ! ! [attrString replaceCharactersInRange:range ! ! ! ! ! withString:[string uppercaseStringWithLocale:locale]]; }]; abcdefghi abcdefghi
  59. Uppercase while preserving attributes Enumerating for Modification NSMutableAttributedString *attrString; NSLocale

    *locale = [NSLocale currentLocale]; [attrString enumerateAttributesInRange:NSMakeRange(0, [attrString length]) ! options:0 ! usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) { ! ! NSString *string = [[attrString string] substringWithRange:range]; ! ! [attrString replaceCharactersInRange:range ! ! ! ! ! withString:[string uppercaseStringWithLocale:locale]]; }]; abcdefghi abcdefghi
  60. Uppercase while preserving attributes Enumerating for Modification NSMutableAttributedString *attrString; NSLocale

    *locale = [NSLocale currentLocale]; [attrString enumerateAttributesInRange:NSMakeRange(0, [attrString length]) ! options:0 ! usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) { ! ! NSString *string = [[attrString string] substringWithRange:range]; ! ! [attrString replaceCharactersInRange:range ! ! ! ! ! withString:[string uppercaseStringWithLocale:locale]]; }]; uppercaseStringWithLocale: abcdefghi abcdefghi
  61. Uppercase while preserving attributes Enumerating for Modification NSMutableAttributedString *attrString; NSLocale

    *locale = [NSLocale currentLocale]; [attrString enumerateAttributesInRange:NSMakeRange(0, [attrString length]) ! options:0 ! usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) { ! ! NSString *string = [[attrString string] substringWithRange:range]; ! ! [attrString replaceCharactersInRange:range ! ! ! ! ! withString:[string uppercaseStringWithLocale:locale]]; }]; abcdefghi abcdefghi
  62. NSMutableAttributedString *attrString; NSLocale *locale = [NSLocale currentLocale]; [attrString enumerateAttributesInRange:NSMakeRange(0, [attrString

    length]) ! options:0 ! usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) { ! ! NSString *string = [[attrString string] substringWithRange:range]; ! ! [attrString replaceCharactersInRange:range ! ! ! ! ! withString:[string uppercaseStringWithLocale:locale]]; }]; Uppercase while preserving attributes Enumerating for Modification ABCdefghi
  63. NSMutableAttributedString *attrString; NSLocale *locale = [NSLocale currentLocale]; [attrString enumerateAttributesInRange:NSMakeRange(0, [attrString

    length]) ! options:0 ! usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) { ! ! NSString *string = [[attrString string] substringWithRange:range]; ! ! [attrString replaceCharactersInRange:range ! ! ! ! ! withString:[string uppercaseStringWithLocale:locale]]; }]; Uppercase while preserving attributes Enumerating for Modification ABCDEfghi
  64. NSMutableAttributedString *attrString; NSLocale *locale = [NSLocale currentLocale]; [attrString enumerateAttributesInRange:NSMakeRange(0, [attrString

    length]) ! options:0 ! usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) { ! ! NSString *string = [[attrString string] substringWithRange:range]; ! ! [attrString replaceCharactersInRange:range ! ! ! ! ! withString:[string uppercaseStringWithLocale:locale]]; }]; Uppercase while preserving attributes Enumerating for Modification ABCDEFghi
  65. NSMutableAttributedString *attrString; NSLocale *locale = [NSLocale currentLocale]; [attrString enumerateAttributesInRange:NSMakeRange(0, [attrString

    length]) ! options:0 ! usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) { ! ! NSString *string = [[attrString string] substringWithRange:range]; ! ! [attrString replaceCharactersInRange:range ! ! ! ! ! withString:[string uppercaseStringWithLocale:locale]]; }]; Uppercase while preserving attributes Enumerating for Modification ABCDEFGHI
  66. NSMutableAttributedString *attrString; NSLocale *locale = [NSLocale currentLocale]; [attrString enumerateAttributesInRange:NSMakeRange(0, [attrString

    length]) ! options:0 ! usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) { ! ! NSString *string = [[attrString string] substringWithRange:range]; ! ! [attrString replaceCharactersInRange:range ! ! ! ! ! withString:[string uppercaseStringWithLocale:locale]]; }]; Uppercase while preserving attributes Enumerating for Modification ABCDEFGHI
  67. NSAttributedString *string = [[NSAttributedString alloc] ! ! initWithString:@”Hello World!!” !

    ! attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:12.0f }]; Drawing an Attributed String
  68. NSAttributedString *string = [[NSAttributedString alloc] ! ! initWithString:@”Hello World!!” !

    ! attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:12.0f }]; Drawing an Attributed String [string drawAtPoint:NSMakePoint(10.0f, 10.0f)]; // rendering at 10,10
  69. NSStringDrawing API •Simple drawing/sizing methods - (void)drawInRect:(CGRect)rect; - (void)drawAtPoint:(CGPoint)point; -

    (CGSize)size; •Extended drawing/sizing methods - (void)drawWithRect:(CGRect)rect ! options:(NSStringDrawingOptions)options ! context:(NSStringDrawingContext *)context; - (CGRect)boundingRectWithSize:(CGSize)size ! options:(NSStringDrawingOptions)options ! context:(NSStringDrawingContext *)context;
  70. Available Attributes •Font •Text color •Paragraph style •Text background color

    •Ligature, kerning, and baseline offset •Underline and strike-through •Stroke width and color •Shadow
  71. Available Attributes •Font •Text color •Paragraph style •Text background color

    •Ligature, kerning, and baseline offset •Underline and strike-through •Stroke width and color •Shadow
  72. •Font NSFontAttributeName (UIFont) •Text color! ! ! ! NSForegroundColorAttributeName (UIColor)

    •Paragraph style NSParagraphStyleAttributeName (NSParagraphStyle) Available Attributes
  73. •Font NSFontAttributeName (UIFont) •Text color! ! ! ! NSForegroundColorAttributeName (UIColor)

    •Paragraph style NSParagraphStyleAttributeName (NSParagraphStyle) Available Attributes A missing attribute implies its default value.
  74. Attribute default value Available Attributes •Font [UIFont systemFontOfSize:[UIFont systemFontSize]] •Text

    color! ! ! ! [UIColor blackColor] •Paragraph style [NSParagraphStyle defaultParagraphStyle]
  75. Quartz graphics state independent Missing Attributes NSAttributedString *string; // No

    attribute specified string = [[NSAttributedString alloc] initWithString:@”Hello World!!”]; // Leaving CGContext color properties with red color [[UIColor redColor] set]; UIRectFill(rect); [string drawAtPoint:point];
  76. Quartz graphics state independent Missing Attributes NSAttributedString *string; // No

    attribute specified string = [[NSAttributedString alloc] initWithString:@”Hello World!!”]; // Leaving CGContext color properties with red color [[UIColor redColor] set]; UIRectFill(rect); [string drawAtPoint:point];
  77. Quartz graphics state independent Missing Attributes NSAttributedString *string; // No

    attribute specified string = [[NSAttributedString alloc] initWithString:@”Hello World!!”]; // Leaving CGContext color properties with red color [[UIColor redColor] set]; UIRectFill(rect); [string drawAtPoint:point]; NSAttributedString *string; // No attribute specified string = [[NSAttributedString alloc] initWithString:@”Hello World!!”]; // Leaving CGContext color properties with red color [[UIColor redColor] set]; UIRectFill(rect); [string drawAtPoint:point];
  78. Quartz graphics state independent Missing Attributes NSAttributedString *string; // No

    attribute specified string = [[NSAttributedString alloc] initWithString:@”Hello World!!”]; // Leaving CGContext color properties with red color [[UIColor redColor] set]; UIRectFill(rect); [string drawAtPoint:point];
  79. Quartz graphics state independent Missing Attributes NSAttributedString *string; // No

    attribute specified string = [[NSAttributedString alloc] initWithString:@”Hello World!!”]; // Leaving CGContext color properties with red color [[UIColor redColor] set]; UIRectFill(rect); [string drawAtPoint:point];
  80. Quartz graphics state independent Missing Attributes NSAttributedString *string; // No

    attribute specified string = [[NSAttributedString alloc] initWithString:@”Hello World!!”]; // Leaving CGContext color properties with red color [[UIColor redColor] set]; UIRectFill(rect); [string drawAtPoint:point]; Hello World!! // Renders with system font in black
  81. Available Attributes •Font NSFontAttributeName (UIFont) •Text color! ! ! !

    NSForegroundColorAttributeName (UIColor) •Paragraph style NSParagraphStyleAttributeName (NSParagraphStyle)
  82. Available Attributes •Font NSFontAttributeName (UIFont) •Text color! ! ! !

    NSForegroundColorAttributeName (UIColor) •Paragraph style NSParagraphStyleAttributeName (NSParagraphStyle)
  83. NSParagraphStyle Layout Text Like Pages Documents •Encapsulates various paragraph-wide style

    attributes •Allows richer word processor like text formatting
  84. NSParagraphStyle Layout Text Like Pages Documents •Encapsulates various paragraph-wide style

    attributes •Allows richer word processor like text formatting •The paragraph style for the first character is used for the rest of paragraph
  85. NSParagraphStyle Layout Text Like Pages Documents •Encapsulates various paragraph-wide style

    attributes •Allows richer word processor like text formatting •The paragraph style for the first character is used for the rest of paragraph
  86. Line break mode NSParagraphStyle •Very similar to UILineBreakMode @property NSLineBreakMode

    lineBreakMode; NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; style.lineBreakMode = NSLineBreakByTruncatingTail; NSMutableAttributedString *string = [[NSMutableAttributedString alloc] ! ! initWithString:@”Hello World!!” ! ! attributes:@{ NSParagraphStyleAttributeName : style }];
  87. Line break mode NSParagraphStyle •Very similar to UILineBreakMode @property NSLineBreakMode

    lineBreakMode; NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; style.lineBreakMode = NSLineBreakByTruncatingTail; NSMutableAttributedString *string = [[NSMutableAttributedString alloc] ! ! initWithString:@”Hello World!!” ! ! attributes:@{ NSParagraphStyleAttributeName : style }];
  88. Line break mode NSParagraphStyle •Very similar to UILineBreakMode @property NSLineBreakMode

    lineBreakMode; NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; style.lineBreakMode = NSLineBreakByTruncatingTail; NSMutableAttributedString *string = [[NSMutableAttributedString alloc] ! ! initWithString:@”Hello World!!” ! ! attributes:@{ NSParagraphStyleAttributeName : style }];
  89. Line break mode NSParagraphStyle •Very similar to UILineBreakMode @property NSLineBreakMode

    lineBreakMode; NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; style.lineBreakMode = NSLineBreakByTruncatingTail; NSMutableAttributedString *string = [[NSMutableAttributedString alloc] ! ! initWithString:@”Hello World!!” ! ! attributes:@{ NSParagraphStyleAttributeName : style }]; Hello Wor…
  90. Rest of drawing options Drawing Attributed Strings •Extended NSStringDrawing methods

    •Advanced attributes •…and more Advanced Attributed Strings for iOS Mission Thursday 10:15am
  91. How do I get this stuff in my labels? Attributed

    Strings in UIKit Ian Baird Label Engineer
  92. Attributed Strings in UIKit UILabel is the nexus of power

    I’m a UILabel with an attributed title.
  93. UILabel Attributed Strings in UIKit •Existing properties text font textColor

    textAlignment lineBreakMode shadowColor Big red dog.
  94. UILabel Attributed Strings in UIKit •Existing properties text font textColor

    textAlignment lineBreakMode shadowColor shadowOffset Big red dog.
  95. UILabel Attributed Strings in UIKit •Style properties apply style to

    entire attributed string, for example Big red dog.
  96. UILabel Attributed Strings in UIKit •Style properties apply style to

    entire attributed string, for example font font: Helvetica Neue Big red dog.
  97. UILabel Attributed Strings in UIKit •Style properties apply style to

    entire attributed string, for example font textColor Big red dog. textColor: red
  98. UIButton Attributed Strings in UIKit •Existing method setTitle:forControlState: •New method

    setAttributedTitle:forControlState: •Attributed title takes precedence Touch Me
  99. UITableView Attributed Strings in UIKit •No new properties or methods

    Just set attributedText on the cell or header view’s textLabel.
  100. Demo summary Attributed Strings in UIKit •Drawing text using attributed

    string drawInRect: •Enhancing existing labels NSMutableAttributedString, addAttribute:value:range: •Formatting paragraphs NSMutableParagraphStyle
  101. More information Jake Behrens UI Frameworks Evangelist [email protected] Documentation UIKit

    Documentation https://developer.apple.com/library/ios Apple Developer Forums http://devforums.apple.com
  102. Related Sessions Advanced Attributed Strings for iOS Mission Thursday 10:15am

    Keyboard Input in iOS Russian Hill Wednesday 2:00PM Core Text and Fonts Russian Hill Wednesday 4:30PM