IOS text attribute dictionary

Source: Internet
Author: User

IOS text attribute 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 = align; // 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 limit: 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 limit: paragraph, // paragraph Format // NSBackgroundColorAttributeName: [UIColor blueColor], // NSStrokeWidthAttributeName: @ 3, // stroke width NSStrokeColorAttributeName: [UIColor greenColor], // set the stroke color and use it with NSStrokeWidthAttributeName. This NSForegroundColorAttributeName is invalid };


 


Effect:

5. NSStrikethroughStyleAttributeName

Strikethrough

 

NSDictionary * attrs ={ NSFontAttributeName: [UIFont fontWithName: @ AmericanTypewriter size: 30], // text color font size NSForegroundColorAttributeName: [UIColor redColor], // text color limit: 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 is invalid. // strikethrough. 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 limit: 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, the value is invalid. // Optional: @ 1, // The number indicates the line width. NSUnderlineStyleAttributeName: @ (NSUnderlineStyleSingle), // underline, the value is an enumeration type, you can try it respectively };


 

 

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 limit: 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, the value is invalid. // Optional: @ 1, // The number indicates 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 limit: 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, the value is invalid. // Optional: @ 1, // The number indicates the line width. NSUnderlineStyleAttributeName: @ (NSUnderlineStyleSingle), // underline, the value is an enumeration type. You can try NSShadowAttributeName: shadow, // set the shadow, and copy it as 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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.