Advertisements
NSMutableAttributedString by example
Retrieving Character Information
- mutableString
Returns the character contents of the receiver as an NSMutableString object.
Example
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:@"Eezy Tutorials Website"]; NSLog(@"%@",[attributedString mutableString]);
Output
2014-04-02 06:25:33.065 iOS-Tutorial[791:a0b] Eezy Tutorials Website
Changing Characters
- replaceCharactersInRange:withString:
Replaces the characters in the given range with the characters of the given string.
Example
NSMutableDictionary *attributesDictionary = [NSMutableDictionary dictionary]; [attributesDictionary setObject:[UIFont systemFontOfSize:12] forKey:NSFontAttributeName]; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:@"Eezy Tutorials Website"]; [attributedString replaceCharactersInRange:NSMakeRange(0, 4) withString:@"iOS"]; NSLog(@"%@",[attributedString mutableString]);
Output
2014-04-02 06:28:12.131 iOS-Tutorial[820:a0b] iOS Tutorials Website
- deleteCharactersInRange:
Deletes the characters in the given range along with their associated attributes.
Example
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:@"Eezy Tutorials Website"]; [attributedString deleteCharactersInRange:NSMakeRange(14, 8)]; NSLog(@"%@",[attributedString mutableString]);
Output
2014-04-02 06:30:58.574 iOS-Tutorial[862:a0b] Eezy Tutorials
Changing Attributes
- setAttributes:range:
Sets the attributes for the characters in the specified range to the specified attributes.
Example
NSMutableDictionary *attributesDictionary = [NSMutableDictionary dictionary]; [attributesDictionary setObject:[UIFont systemFontOfSize:12] forKey:NSFontAttributeName]; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:@"Eezy Tutorials Website"]; [attributedString setAttributes:attributesDictionary range:NSMakeRange(0, 4)]; NSLog(@"%@",attributedString );
Output
2014-04-02 06:35:53.638 iOS-Tutorial[875:a0b] Eezy{
NSFont = " font-family: \".HelveticaNeueInterface-M3\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
} Tutorials Website{
}
- addAttribute:value:range:
Adds an attribute with the given name and value to the characters in the specified range.
Example
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:@"Eezy Tutorials Website"]; [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, 4)]; NSLog(@"%@",attributedString );
Output
2014-04-02 06:40:03.818 iOS-Tutorial[904:a0b] Eezy{
NSFont = " font-family: \".HelveticaNeueInterface-M3\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
} Tutorials Website{
}
- addAttributes:range:
Adds the given collection of attributes to the characters in the specified range.
Example
NSMutableDictionary *attributesDictionary = [NSMutableDictionary dictionary];
[attributesDictionary setObject:[UIFont systemFontOfSize:12] forKey:NSFontAttributeName];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:@"Eezy Tutorials Website"];
[attributedString addAttributes:attributesDictionary range:NSMakeRange(0, 4)];
NSLog(@"%@",attributedString );
Output
2014-04-02 06:41:12.886 iOS-Tutorial[916:a0b] Eezy{
NSFont = " font-family: \".HelveticaNeueInterface-M3\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
} Tutorials Website{
}
- removeAttribute:range:
Removes the named attribute from the characters in the specified range.
Example
NSMutableDictionary *attributesDictionary = [NSMutableDictionary dictionary]; [attributesDictionary setObject:[UIFont systemFontOfSize:12] forKey:NSFontAttributeName]; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:@"Eezy Tutorials Website" attributes:attributesDictionary]; [attributedString removeAttribute:NSFontAttributeName range:NSMakeRange(0, 4)]; NSLog(@"%@",attributedString );
Output
2014-04-02 06:44:01.050 iOS-Tutorial[930:a0b] Eezy{
} Tutorials Website{
NSFont = " font-family: \".HelveticaNeueInterface-M3\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}
Changing Characters and Attributes
- appendAttributedString:
Adds the characters and attributes of a given attributed string to the end of the receiver.
Example
NSMutableDictionary *attributesDictionary = [NSMutableDictionary dictionary]; [attributesDictionary setObject:[UIFont systemFontOfSize:12] forKey:NSFontAttributeName]; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:@"Eezy Tutorials " attributes:attributesDictionary]; NSMutableAttributedString *secondAttributedString = [[NSMutableAttributedString alloc]initWithString:@"Website"]; [attributedString appendAttributedString:secondAttributedString]; NSLog(@"%@",attributedString );
Output
2014-04-02 06:48:31.642 iOS-Tutorial[950:a0b] Eezy Tutorials {
NSFont = " font-family: \".HelveticaNeueInterface-M3\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}Website{
}
- insertAttributedString:atIndex:
Inserts the characters and attributes of the given attributed string into the receiver at the given index.
Example
NSMutableDictionary *attributesDictionary = [NSMutableDictionary dictionary]; [attributesDictionary setObject:[UIFont systemFontOfSize:12] forKey:NSFontAttributeName]; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:@"Tutorials " attributes:attributesDictionary]; NSMutableAttributedString *secondAttributedString = [[NSMutableAttributedString alloc]initWithString:@"Eezy"]; [attributedString insertAttributedString:secondAttributedString atIndex:0]; NSLog(@"%@",attributedString );
Output
2014-04-02 06:50:34.075 iOS-Tutorial[964:a0b] Eezy{
}Tutorials {
NSFont = " font-family: \".HelveticaNeueInterface-M3\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}
- replaceCharactersInRange:withAttributedString:
Replaces the characters and attributes in a given range with the characters and attributes of the given attributed string.
Example
NSMutableDictionary *attributesDictionary = [NSMutableDictionary dictionary]; [attributesDictionary setObject:[UIFont systemFontOfSize:12] forKey:NSFontAttributeName]; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:@"Tutorials " attributes:attributesDictionary]; NSMutableAttributedString *secondAttributedString = [[NSMutableAttributedString alloc]initWithString:@"Eezy"]; [attributedString replaceCharactersInRange:NSMakeRange(0, 9) withAttributedString:secondAttributedString]; NSLog(@"%@",attributedString );
Output
2014-04-02 06:53:11.913 iOS-Tutorial[978:a0b] Eezy{
} {
NSFont = " font-family: \".HelveticaNeueInterface-M3\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}
- setAttributedString:
Replaces the receivers entire contents with the characters and attributes of the given attributed string.
Example
NSMutableDictionary *attributesDictionary = [NSMutableDictionary dictionary]; [attributesDictionary setObject:[UIFont systemFontOfSize:12] forKey:NSFontAttributeName]; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:@"iOS Tutorials " attributes:attributesDictionary]; NSMutableAttributedString *secondAttributedString = [[NSMutableAttributedString alloc]initWithString:@"Eezy Tutorials"]; [attributedString setAttributedString:secondAttributedString]; NSLog(@"%@",attributedString );
Output
2014-04-02 06:55:51.784 iOS-Tutorial[1006:a0b] Eezy Tutorials{
}