Share
Embed
Session 222 - Introduction to Attributed Strings for iOS
wwdcman
June 25, 2012
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
210 slides
-
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 -
Multi-style String Drawing in UIKit You can take it off
the wishlist now! in UIKit -
•Attributed string essentials •Drawing with basic attributes •UIKit adoption Agenda
•Attributed string essentials -
•Attributed string essentials •Drawing with basic attributes •UIKit adoption Agenda
•Drawing with basic attributes -
What Is an Attributed String ? Associating attributes to characters
I’m an attributed string -
What Is an Attributed String ? Associating attributes to characters
I’m an attributed string Helvetica Neue -
What Is an Attributed String ? Associating attributes to characters
I’m an attributed string Helvetica Neue -
What Is an Attributed String ? Associating attributes to characters
Helvetica Neue Helvetica Neue Helvetica Neue + Yellow I’m an attributed string -
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 -
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 -
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 -
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 -
•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; -
•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; -
Specifying a font Instantiating an Attributed String •Using NSFontAttributeName NSAttributedString
*string = [[NSAttributedString alloc] ! ! initWithString:@”Hello World!!” ! ! attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:12.0f }]; -
Specifying a font Instantiating an Attributed String Hello World!! •Using
NSFontAttributeName NSAttributedString *string = [[NSAttributedString alloc] ! ! initWithString:@”Hello World!!” ! ! attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:12.0f }]; -
•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 -
•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 -
•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 -
•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 -
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 -
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. -
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 -
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. -
•Mutable methods for string contents - (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str; Modifying
string contents NSMutableAttributedString -
•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 -
•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 -
- (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; -
•Moving the appointment time 1 hour early NSMutableAttributedString *string; //
a mutable string [string replaceCharactersInRange:NSMakeRange(11, 6) withString:@”8:15am”]; Modifying String Contents -
•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 -
•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. -
•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. -
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”]; -
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”]; -
•Attributes are preserved during string content replacement •Three rules to
remember Attributes Preserved -
•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 -
•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 -
•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 -
•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 -
•Insertion [string replaceCharactersInRange:NSMakeRange(3, 0) withString:@”ABC”]; Replacement rule samples Modifying String
Contents defghi •Insertion [string replaceCharactersInRange:NSMakeRange(3, 0) withString:@”ABC”]; abc -
•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 -
•Insertion [string replaceCharactersInRange:NSMakeRange(3, 0) withString:@”ABC”]; Replacement rule samples Modifying String
Contents defghi •Insertion [string replaceCharactersInRange:NSMakeRange(3, 0) withString:@”ABC”]; abcABC -
•Insertion at the beginning [string replaceCharactersInRange:NSMakeRange(0, 0) withString:@”ABC”]; Replacement rule
samples Modifying String Contents defghi abc -
•Insertion at the beginning [string replaceCharactersInRange:NSMakeRange(0, 0) withString:@”ABC”]; Replacement rule
samples Modifying String Contents ABC defghi abc -
•Insertion at the beginning [string replaceCharactersInRange:NSMakeRange(0, 0) withString:@”ABC”]; Replacement rule
samples Modifying String Contents defghi abc ABC -
•Cross-run replacement [string replaceCharactersInRange:NSMakeRange(2, 3) withString:@”ABC”]; Replacement rule samples Modifying
String Contents ABC ab de c fghi -
•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”]; Replacement rule samples Modifying String Contents abcdefghi •No character, no attributes [string deleteCharactersInRange:NSMakeRange(0, 9)]; [string replaceCharactersInRange:NSMakeRange(0, 0) withString:@”ABC”]; -
•No character, no attributes [string deleteCharactersInRange:NSMakeRange(0, 9)]; [string replaceCharactersInRange:NSMakeRange(0, 0)
withString:@”ABC”]; Replacement rule samples Modifying String Contents -
•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”]; -
Querying Attributes •An attribute dictionary per character - (NSString *)string
- (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range; -
Querying Attributes •An attribute dictionary per character - (NSString *)string
- (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range; -
Querying Attributes •An attribute dictionary per character - (NSString *)string
- (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range; -
Querying Attributes Not guaranteed to return the maximum range •An
attribute dictionary per character - (NSString *)string - (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range; -
•An attribute dictionary per character - (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range; NSRange
range; NSDictionary *dict = [string attributesAtIndex:4 effectiveRange:&range]; Querying Attributes abcdefghi -
•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]; -
•An attribute dictionary per character - (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range; NSRange
range; NSDictionary *dict = [string attributesAtIndex:4 effectiveRange:&range]; Querying Attributes abcdefghi -
•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 -
•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} -
•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} -
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} -
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; -
Using longest effective range methods Getting Contiguous Run Range •Getting
the contiguous range for attributes abcdefghi -
Using longest effective range methods Getting Contiguous Run Range •Getting
the contiguous range for attributes abcdefghi -
•Getting the contiguous range for attributes abcdefghi Using longest effective
range methods Getting Contiguous Run Range -
•Getting the contiguous range for attributes abcdefghi Using longest effective
range methods Getting Contiguous Run Range Helvetica Neue Red -
•Getting the contiguous range for attributes abcdefghi Using longest effective
range methods Getting Contiguous Run Range Helvetica Neue Blue -
•Getting the contiguous range for attributes abcdefghi Using longest effective
range methods Getting Contiguous Run Range Helvetica Neue Italic Blue -
abcdefghi •Getting the contiguous range for attributes Using longest effective
range methods Getting Contiguous Run Range Helvetica Neue Italic Green -
abcdefghi •Getting the contiguous range for NSFontAttributeName Using longest effective
range methods Getting Contiguous Run Range -
abcdefghi •Getting the contiguous range for NSFontAttributeName Using longest effective
range methods Getting Contiguous Run Range Helvetica Neue -
abcdefghi •Getting the contiguous range for NSFontAttributeName Using longest effective
range methods Getting Contiguous Run Range Helvetica Neue Italic -
abcdefghi •Getting the contiguous range for NSForegroundColorAttributeName Using longest effective
range methods Getting Contiguous Run Range -
abcdefghi •Getting the contiguous range for NSForegroundColorAttributeName Using longest effective
range methods Getting Contiguous Run Range Red -
abcdefghi •Getting the contiguous range for NSForegroundColorAttributeName Using longest effective
range methods Getting Contiguous Run Range Blue -
abcdefghi •Getting the contiguous range for NSForegroundColorAttributeName Using longest effective
range methods Getting Contiguous Run Range Green -
•Enumeration methods -enumerateAttributesInRange:options:usingBlock: -enumerateAttribute:inRange:options:usingBlock: •Changing attributes based on existing attributes
•Modifying string contents while preserving the attributes Querying Attributes -
•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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
NSAttributedString *string = [[NSAttributedString alloc] ! ! initWithString:@”Hello World!!” !
! attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:12.0f }]; Drawing an Attributed String -
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 -
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; -
NSStringDrawing API •Simple drawing/sizing methods - (void)drawAtPoint:(CGPoint)point; - (CGSize)size; -
(void)drawInRect:(CGRect)rect; -
NSStringDrawing API •Simple drawing/sizing methods - (void)drawAtPoint:(CGPoint)point; - (CGSize)size; -
(void)drawInRect:(CGRect)rect; -
NSStringDrawing API •Simple drawing/sizing methods - (void)drawInRect:(CGRect)rect; ! -drawInRect:withFont:lineBreakMode:alignment: -
(void)drawAtPoint:(CGPoint)point; - (CGSize)size; -
NSStringDrawing API •Simple drawing/sizing methods - (void)drawInRect:(CGRect)rect; - (void)drawAtPoint:(CGPoint)point; !
-drawAtPoint:withFont: - (CGSize)size; -
NSStringDrawing API •Simple drawing/sizing methods - (void)drawInRect:(CGRect)rect; - (void)drawAtPoint:(CGPoint)point; -
(CGSize)size; ! -sizeWithFont: -
Available Attributes •Font •Text color •Paragraph style •Text background color
•Ligature, kerning, and baseline offset •Underline and strike-through •Stroke width and color •Shadow -
Available Attributes •Font •Text color •Paragraph style •Text background color
•Ligature, kerning, and baseline offset •Underline and strike-through •Stroke width and color •Shadow -
•Font NSFontAttributeName (UIFont) •Text color! ! ! ! NSForegroundColorAttributeName (UIColor)
•Paragraph style NSParagraphStyleAttributeName (NSParagraphStyle) Available Attributes -
•Font NSFontAttributeName (UIFont) •Text color! ! ! ! NSForegroundColorAttributeName (UIColor)
•Paragraph style NSParagraphStyleAttributeName (NSParagraphStyle) Available Attributes A missing attribute implies its default value. -
Attribute default value Available Attributes •Font [UIFont systemFontOfSize:[UIFont systemFontSize]] •Text
color! ! ! ! [UIColor blackColor] •Paragraph style [NSParagraphStyle defaultParagraphStyle] -
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]; -
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]; -
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]; -
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]; -
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]; -
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 -
Available Attributes •Font NSFontAttributeName (UIFont) •Text color! ! ! !
NSForegroundColorAttributeName (UIColor) •Paragraph style NSParagraphStyleAttributeName (NSParagraphStyle) -
Available Attributes •Font NSFontAttributeName (UIFont) •Text color! ! ! !
NSForegroundColorAttributeName (UIColor) •Paragraph style NSParagraphStyleAttributeName (NSParagraphStyle) -
NSParagraphStyle Layout Text Like Pages Documents •Encapsulates various paragraph-wide style
attributes •Allows richer word processor like text formatting -
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 -
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 -
NSParagraphStyle •Line break mode •Alignment •Spacings •Indentation •Line height control
•Hyphenation •Base writing direction -
NSParagraphStyle •Line break mode •Alignment •Spacings •Indentation •Line height control
•Hyphenation •Base writing direction -
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 }]; -
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 }]; -
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 }]; -
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… -
•Additional alignments introduced @property NSTextAlignment alignment; NSTextAlignmentLeft NSTextAlignmentCenter NSTextAlignmentRight NSTextAlignmentJustified
Alignment NSParagraphStyle This paragraph shows the new alignment mode, justification. -
•Additional alignments introduced @property NSTextAlignment alignment; NSTextAlignmentLeft NSTextAlignmentCenter NSTextAlignmentRight NSTextAlignmentJustified
NSTextAlignmentNatural Alignment NSParagraphStyle Hello World!! -
•Additional alignments introduced @property NSTextAlignment alignment; NSTextAlignmentLeft NSTextAlignmentCenter NSTextAlignmentRight NSTextAlignmentJustified
NSTextAlignmentNatural Alignment NSParagraphStyle !!!"#$"ا #&'() -
Rest of drawing options Drawing Attributed Strings •Extended NSStringDrawing methods
•Advanced attributes •…and more -
Rest of drawing options Drawing Attributed Strings •Extended NSStringDrawing methods
•Advanced attributes •…and more Advanced Attributed Strings for iOS Mission Thursday 10:15am -
How do I get this stuff in my labels? Attributed
Strings in UIKit Ian Baird Label Engineer -
Attributed Strings in UIKit UILabel is the nexus of power
I’m a UILabel with a title. -
Attributed Strings in UIKit UILabel is the nexus of power
I’m a UILabel with an attributed title. -
UILabel Attributed Strings in UIKit •Existing properties text font textColor
textAlignment Big red dog. -
UILabel Attributed Strings in UIKit •Existing properties text font textColor
textAlignment lineBreakMode Big red dog. -
UILabel Attributed Strings in UIKit •Existing properties text font textColor
textAlignment lineBreakMode shadowColor Big red dog. -
UILabel Attributed Strings in UIKit •Existing properties text font textColor
textAlignment lineBreakMode shadowColor shadowOffset Big red dog. -
UILabel Attributed Strings in UIKit •Style properties apply style to
entire attributed string, for example Big red dog. -
UILabel Attributed Strings in UIKit •Style properties apply style to
entire attributed string, for example font font: Helvetica Neue Big red dog. -
UILabel Attributed Strings in UIKit •Style properties apply style to
entire attributed string, for example font textColor Big red dog. textColor: red -
UIButton Attributed Strings in UIKit •Existing method setTitle:forControlState: •New method
setAttributedTitle:forControlState: Touch Me -
UIButton Attributed Strings in UIKit •Existing method setTitle:forControlState: •New method
setAttributedTitle:forControlState: •Attributed title takes precedence Touch Me -
UITableView Attributed Strings in UIKit •No new properties or methods
Just set attributedText on the cell or header view’s textLabel. -
Demo summary Attributed Strings in UIKit •Drawing text using attributed
string drawInRect: •Enhancing existing labels NSMutableAttributedString, addAttribute:value:range: •Formatting paragraphs NSMutableParagraphStyle -
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 -
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 -
The last 3 slides after the logo are intentionally left
blank for all presentations. -
The last 3 slides after the logo are intentionally left
blank for all presentations. -
The last 3 slides after the logo are intentionally left
blank for all presentations.