IOS text attribute dictionary and ios text dictionary
During iOS development, it is believed that the parameter is a NSDictionary dictionary when the font, color, underline, and other attributes are required.
But which key values are correct in the dictionary?
Let's summarize the commonly used
First, we will create the simplest one. Set the font and size.
We use the NSString method.
-(Void) drawInRect :( CGRect) rect withAttributes :( NSDictionary *) attrs
To print a string to the view.
-(Void) drawRect :( CGRect) rect {self. backgroundColor = [UIColor whiteColor]; NSString * attrString = @ "hello word"; NSDictionary * attrs =@{ NSFontAttributeName: [UIFont fontWithName: @ "AmericanTypewriter" size: 30]}; // Add the text font size to the dictionary [attrString drawInRect: CGRectMake (20,120,320,200) withAttributes: attrs];}
Font color
NSDictionary * attrs ={ NSFontAttributeName: [UIFont fontWithName: @ "AmericanTypewriter" size: 30], // text color font size NSForegroundColorAttributeName: [UIColor redColor]/text color };
Effect
Ii. NSParagraphStyleAttributeName
Paragraph Format
-(Void) drawRect :( CGRect) rect {NSString * attrString = @ "hello word"; NSMutableParagraphStyle * paragraph = [[NSMutableParagraphStyle alloc] init]; paragraph. alignment = NSTextAlignmentCenter; // center NSDictionary * attrs =@{ NSFontAttributeName: [UIFont fontWithName: @ "AmericanTypewriter" size: 30], // text color font size: [UIColor redColor], // text color NSParagraphStyleAttributeName: paragraph, // paragraph Format}; [attrString drawInRect: CGRectMake (20,120,320,200) withAttributes: attrs];}
Effect: (center)
3. NSBackgroundColorAttributeName
Background Color
NSDictionary * attrs ={ NSFontAttributeName: [UIFont fontWithName: @ "AmericanTypewriter" size: 30], // text color font size NSForegroundColorAttributeName: [UIColor redColor], // text color NSParagraphStyleAttributeName: paragraph, // paragraph Format NSBackgroundColorAttributeName: [UIColor blueColor], // background color };
Effect:
4. NSStrokeColorAttributeName
Set stroke color, which must be used with NSStrokeWidthAttributeName
NSDictionary * attrs ={ NSFontAttributeName: [UIFont fontWithName: @ "AmericanTypewriter" size: 30], // text color font size NSForegroundColorAttributeName: [UIColor redColor], // text color: paragraph, // paragraph Format // NSBackgroundColorAttributeName: [UIColor blueColor], // NSStrokeWidthAttributeName: @ 3, // stroke width NSStrokeColorAttributeName: [UIColor greenColor], // you can set the stroke color and use it with NSStrokeWidthAttributeName. If NSForegroundColorAttributeName is set, it will be invalid };
Effect:
5. NSStrikethroughStyleAttributeName
Strikethrough
NSDictionary * attrs ={ NSFontAttributeName: [UIFont fontWithName: @ "AmericanTypewriter" size: 30], // text color font size NSForegroundColorAttributeName: [UIColor redColor], // text color: paragraph, // paragraph Format // NSBackgroundColorAttributeName: [UIColor blueColor], // NSStrokeWidthAttributeName: @ 3, // stroke width NSStrokeColorAttributeName: [UIColor greenColor], // set the stroke color, and use it with NSStrokeWidthAttributeName. If NSForegroundColorAttributeName is set, NSStrikethroughStyleAttributeName: @ 1, // strikethrough, And the number indicates the line width };
Effect:
6. NSUnderlineStyleAttributeName
Underline
NSDictionary * attrs ={ NSFontAttributeName: [UIFont fontWithName: @ "AmericanTypewriter" size: 30], // text color font size NSForegroundColorAttributeName: [UIColor redColor], // text color: paragraph, // paragraph Format // NSBackgroundColorAttributeName: [UIColor blueColor], // NSStrokeWidthAttributeName: @ 3, // stroke width NSStrokeColorAttributeName: [UIColor greenColor], // sets the stroke color, and works with NSStrokeWidthAttributeName. If NSForegroundColorAttributeName is set, it becomes invalid. // NSStrikethroughStyleAttributeName: @ 1, // deletes the line, number indicates the line width NSUnderlineStyleAttributeName: @ (NSUnderlineStyleSingle), // underline, the value is an enumeration type, you can try };
Effect:
7. NSShadowAttributeName
Sets the shadow. Its object is an NSShadow object.
NSDictionary * attrs ={ NSFontAttributeName: [UIFont fontWithName: @ "AmericanTypewriter" size: 30], // text color font size NSForegroundColorAttributeName: [UIColor redColor], // text color: paragraph, // paragraph Format // NSBackgroundColorAttributeName: [UIColor blueColor], // NSStrokeWidthAttributeName: @ 3, // stroke width NSStrokeColorAttributeName: [UIColor greenColor], // sets the stroke color, and works with NSStrokeWidthAttributeName. If NSForegroundColorAttributeName is set, it becomes invalid. // NSStrikethroughStyleAttributeName: @ 1, // deletes the line, number represents the line width NSUnderlineStyleAttributeName: @ (NSUnderlineStyleSingle), // underline, the value is an enumeration type, you can try NSShadowAttributeName: shadow, // set the shadow, copy to an NSShadow object };
NSShadow
NSShadow * shadow = [[NSShadow alloc] init]; shadow. shadowBlurRadius = 5; // blur the shadow. shadowColor = [UIColor blueColor]; // shadow color. shadowOffset = CGSizeMake (6, 6); // The Shadow offset relative to the original one.
Effect:
8. NSObliquenessAttributeName
Skew
NSDictionary * attrs ={ NSFontAttributeName: [UIFont fontWithName: @ "AmericanTypewriter" size: 30], // text color font size NSForegroundColorAttributeName: [UIColor redColor], // text color: paragraph, // paragraph Format // NSBackgroundColorAttributeName: [UIColor blueColor], // NSStrokeWidthAttributeName: @ 3, // stroke width NSStrokeColorAttributeName: [UIColor greenColor], // sets the stroke color, and works with NSStrokeWidthAttributeName. If NSForegroundColorAttributeName is set, it becomes invalid. // NSStrikethroughStyleAttributeName: @ 1, // deletes the line, number represents the line width NSUnderlineStyleAttributeName: @ (NSUnderlineStyleSingle), // underline, the value is an enumeration type, you can try NSShadowAttributeName: shadow, // set the shadow, copy to an NSShadow object NSObliquenessAttributeName: @ 1 // skew };
Effect:
We have sorted out some of these commonly used ones, and some of them have not been sorted out. If you are interested, you can study it. You are welcome to join us and discuss it with us.
Apple Development Group: 414319235 welcome to join discussions
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.