Creating an NSAttributedString Object

- initWithString:

Returns an NSAttributedString object initialized with the characters of a given string and no attribute information.

Example

NSAttributedString *attributedString = [[NSAttributedString alloc]initWithString:@"Eezy"];
NSLog(@"%@",attributedString);
		

Output

2014-04-02 05:14:13.265 iOS-Tutorial[502:a0b] Eezy{
}
		

- initWithAttributedString:

Returns an NSAttributedString object initialized with the characters and attributes of another given attributed string.

Example

NSAttributedString *tempAttributedString = [[NSAttributedString alloc]initWithString:@"Eezy"];

NSAttributedString *attributedString = [[NSAttributedString alloc]initWithAttributedString:tempAttributedString];

NSLog(@"%@",attributedString);
		

Output

2014-04-02 05:16:22.208 iOS-Tutorial[517:a0b] Eezy{
}
		

- initWithString:attributes:

Returns an NSAttributedString object initialized with a given string and attributes.

Example

NSMutableDictionary *attributesDictionary = [NSMutableDictionary dictionary];
    
[attributesDictionary setObject:[UIFont systemFontOfSize:12] forKey:NSFontAttributeName];
[attributesDictionary setObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
[attributesDictionary setObject:[UIColor blackColor] forKey:NSBackgroundColorAttributeName];
[attributesDictionary setObject:@5.0 forKey:NSBaselineOffsetAttributeName];
[attributesDictionary setObject:@2.0 forKey:NSStrikethroughStyleAttributeName];
[attributesDictionary setObject:[UIColor redColor] forKey:NSStrokeColorAttributeName];
[attributesDictionary setObject:@2.0 forKey:NSStrokeWidthAttributeName];
    
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 2.0;
[attributesDictionary setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
    
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor lightGrayColor];
shadow.shadowBlurRadius = 1.0;
shadow.shadowOffset = CGSizeMake(0.0, 2.0);
[attributesDictionary setObject:shadow forKey:NSShadowAttributeName];
NSAttributedString *attributedString = [[NSAttributedString alloc]initWithString:@"Eezy Tutorials Website"];
   
NSLog(@"%@",attributedString);
		

Output

2014-04-02 05:43:01.534 iOS-Tutorial[590:a0b] Eezy Tutorials Website{
    NSBackgroundColor = "UIDeviceWhiteColorSpace 0 1";
    NSBaselineOffset = 5;
    NSColor = "UIDeviceWhiteColorSpace 1 1";
    NSFont = " font-family: \".HelveticaNeueInterface-M3\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
    NSParagraphStyle = "Alignment 4, LineSpacing 2, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n    28L,\n    56L,\n    84L,\n    112L,\n    140L,\n    168L,\n    196L,\n    224L,\n    252L,\n    280L,\n    308L,\n    336L\n), DefaultTabInterval 0, Blocks (null), Lists (null), BaseWritingDirection -1, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
    NSShadow = "NSShadow {0, 2} blur = 1 color = {UIDeviceWhiteColorSpace 0.666667 1}";
    NSStrikethrough = 2;
    NSStrokeColor = "UIDeviceRGBColorSpace 1 0 0 1";
    NSStrokeWidth = 2;
}		

Retrieving Character Information

- string

Returns the character contents of the receiver as an NSString object.

Example

[attributesDictionary setObject:shadow forKey:NSShadowAttributeName];
NSAttributedString *attributedString = [[NSAttributedString alloc]initWithString:@"Eezy Tutorials Website"];
NSLog(@"%@",[attributedString string]);		

Output

2014-04-02 05:45:40.673 iOS-Tutorial[603:a0b] Eezy Tutorials Website		

- length

Returns the length of the receiver’s string object.

Example

NSAttributedString *attributedString = [[NSAttributedString alloc]initWithString:@"Eezy Tutorials Website"];
NSLog(@"%d",[attributedString length]);		

Output

2014-04-02 05:46:18.445 iOS-Tutorial[616:a0b] 22
		

Retrieving Attribute Information

- attributesAtIndex:effectiveRange:

Returns the attributes for the character at a given index.

Example

NSMutableDictionary *attributesDictionary = [NSMutableDictionary dictionary];
[attributesDictionary setObject:[UIFont systemFontOfSize:12] forKey:NSFontAttributeName];
    
NSAttributedString *attributedString = [[NSAttributedString alloc]initWithString:@"Eezy Tutorials Website" attributes:attributesDictionary];
NSRange range =NSMakeRange(0,10);
NSLog(@"%@",[attributedString attributesAtIndex:0 effectiveRange:&range]);		

Output

2014-04-02 05:54:29.129 iOS-Tutorial[635:a0b] {
    NSFont = " font-family: \".HelveticaNeueInterface-M3\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}		

- attributesAtIndex:longestEffectiveRange:inRange:

Returns the attributes for the character at a given index, and by reference the range over which the attributes apply.

Example

NSMutableDictionary *attributesDictionary = [NSMutableDictionary dictionary];
[attributesDictionary setObject:[UIFont systemFontOfSize:12] forKey:NSFontAttributeName];
    
NSAttributedString *attributedString = [[NSAttributedString alloc]initWithString:@"Eezy Tutorials Website" attributes:attributesDictionary];
NSRange limitRange = NSMakeRange(0,10);
NSRange effectiveRange;
NSLog(@"%@",[attributedString attributesAtIndex:0 longestEffectiveRange:&effectiveRange inRange:limitRange]);		

Output

2014-04-02 06:01:07.798 iOS-Tutorial[658:a0b] {
    NSFont = " font-family: \".HelveticaNeueInterface-M3\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}		

- attribute:atIndex:effectiveRange:

Returns the value for an attribute with a given name of the character at a given index, and by reference the range over which the attribute applies.

Example

NSMutableDictionary *attributesDictionary = [NSMutableDictionary dictionary];
[attributesDictionary setObject:[UIFont systemFontOfSize:12] forKey:NSFontAttributeName];
    
NSAttributedString *attributedString = [[NSAttributedString alloc]initWithString:@"Eezy Tutorials Website" attributes:attributesDictionary];
NSRange effectiveRange;
NSLog(@"%@",[attributedString attribute:NSFontAttributeName atIndex:0 effectiveRange:&effectiveRange]);		

Output

2014-04-02 06:03:01.473 iOS-Tutorial[675:a0b]  font-family: ".HelveticaNeueInterface-M3"; font-weight: normal; font-style: normal; font-size: 12.00pt		

- attribute:atIndex:longestEffectiveRange:inRange:

Returns the value for the attribute with a given name of the character at a given index, and by reference the range over which the attribute applies.

Example

NSMutableDictionary *attributesDictionary = [NSMutableDictionary dictionary];
[attributesDictionary setObject:[UIFont systemFontOfSize:12] forKey:NSFontAttributeName];
    
NSAttributedString *attributedString = [[NSAttributedString alloc]initWithString:@"Eezy Tutorials Website" attributes:attributesDictionary];
NSRange limitRange = NSMakeRange(0,10);
NSRange effectiveRange;
NSLog(@"%@",[attributedString attribute:NSFontAttributeName atIndex:0 longestEffectiveRange:&effectiveRange inRange:limitRange]);		

Output

2014-04-02 06:04:07.750 iOS-Tutorial[690:a0b]  font-family: ".HelveticaNeueInterface-M3"; font-weight: normal; font-style: normal; font-size: 12.00pt		

Comparing Attributed Strings

- isEqualToAttributedString:

Returns a Boolean value that indicates whether the receiver is equal to another given attributed string.

Example

NSMutableDictionary *attributesDictionary = [NSMutableDictionary dictionary];
[attributesDictionary setObject:[UIFont systemFontOfSize:12] forKey:NSFontAttributeName];
    
NSAttributedString *attributedString = [[NSAttributedString alloc]initWithString:@"Eezy Tutorials Website" attributes:attributesDictionary];
    
NSAttributedString *secondAttributedString = [[NSAttributedString alloc]initWithString:@"Eezy Tutorials Website" attributes:attributesDictionary];

    
NSLog(@"%d",[attributedString isEqualToAttributedString:secondAttributedString]);		

Output

2014-04-02 06:06:43.623 iOS-Tutorial[705:a0b] 1
		

Extracting a Substring

- attributedSubstringFromRange:

Returns an NSAttributedString object consisting of the characters and attributes within a given range in the receiver.

Example

NSMutableDictionary *attributesDictionary = [NSMutableDictionary dictionary];
[attributesDictionary setObject:[UIFont systemFontOfSize:12] forKey:NSFontAttributeName];
    
NSAttributedString *attributedString = [[NSAttributedString alloc]initWithString:@"Eezy Tutorials Website" attributes:attributesDictionary];
    
NSLog(@"%@",[attributedString attributedSubstringFromRange:NSMakeRange(0, 4)]);		

Output

2014-04-02 06:10:04.719 iOS-Tutorial[724:a0b] Eezy{
    NSFont = " font-family: \".HelveticaNeueInterface-M3\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}		

Enumerating over Attributes in a String

- enumerateAttribute:inRange:options:usingBlock:

Executes the Block for the specified attribute run in the specified range.

Example

NSMutableDictionary *attributesDictionary = [NSMutableDictionary dictionary];
[attributesDictionary setObject:[UIFont systemFontOfSize:12] forKey:NSFontAttributeName];
    
NSAttributedString *attributedString = [[NSAttributedString alloc]initWithString:@"Eezy Tutorials Website" attributes:attributesDictionary];
    
[attributedString enumerateAttribute:(NSString *) NSFontAttributeName
                                 inRange:NSMakeRange(0, [attributedString length])
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
                              usingBlock:^(id value, NSRange range, BOOL *stop) {
                                  NSLog(@"Attribute: %@, %@", value, NSStringFromRange(range));
                              }];	
	

Output

2014-04-02 06:13:06.358 iOS-Tutorial[741:a0b] Attribute:  font-family: ".HelveticaNeueInterface-M3"; font-weight: normal; font-style: normal; font-size: 12.00pt, {0, 22}
		

- enumerateAttributesInRange:options:usingBlock:

Executes the Block for each attribute in the range.

Example

NSMutableDictionary *attributesDictionary = [NSMutableDictionary dictionary];
[attributesDictionary setObject:[UIFont systemFontOfSize:12] forKey:NSFontAttributeName];
    
NSAttributedString *attributedString = [[NSAttributedString alloc]initWithString:@"Eezy Tutorials Website" attributes:attributesDictionary];
    
[attributedString enumerateAttributesInRange:NSMakeRange(0, [attributedString length]) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) {
        NSLog(@"Attribute: %@, %@", attrs, NSStringFromRange(range));

    }];		

Output

2014-04-02 06:15:16.124 iOS-Tutorial[769:a0b] Attribute: {
    NSFont = " font-family: \".HelveticaNeueInterface-M3\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}, {0, 22}
		

Advertisements