iOS Rich Text (i)

Source: Internet
Author: User

Nsattributedstring is called Rich Text, is a string with attributes, it can easily in a string to show a variety of fonts, font size, font sizes and other different styles, you can also format the paragraph.

The following code to achieve the above graphic effect, it is very convenient, never set up two uilable, and deliberately deal with their length.

1     UILabel * alable = [[UILabel alloc] Initwithframe:cgrectmake (+, A, max, +)]; 2     alable.textalignment = Nstex Talignmentcenter; 3     [Self.view addsubview:alable]; 4      5     nsstring * astring = @ "¥ 150 yuan/bit"; 6      7     //Rich Text Object 8     nsmutableattributedstring * aattributedstring = [[Nsmutableattributedstring alloc] initwithstring:astring]; 9     //Rich Text style one by one     [aattributedstring addattribute:nsforegroundcolorattributename  //Text color                               Value:[uicolor redcolor]13                               range:nsmakerange (0, 4)];14     [aattributedstring addattribute: Nsfontattributename             //Text font                               value:[uifont systemfontofsize:25]17                               range:nsmakerange (0, 4)];18     alable.attributedtext = aattributedstring;

Common Properties:

Nsfontattributename text Font

Nsparagraphstyleattributename paragraph style (string is segmented by "\ n", this setting must be valid at lable.numberoflines = 0 o'clock, Value is set by Nsmutableparagraphstyle, which has the following properties)

[Paragraph Style-episode]
1 @property (ReadWrite) cgfloat linespacing;           Line spacing 2 @property (readwrite) cgfloat paragraphspacing;           Segment Spacing 3 @property (readwrite) nstextalignment alignment;          Alignment 4 @property (readwrite) cgfloat firstlineheadindent;               The first line of tightening 5 @property (readwrite) cgfloat headindent;               Indent 6 @property (ReadWrite) CGFloat tailindent other than the first line;        Each line holds the width of the character 7 @property (ReadWrite) Nslinebreakmode Linebreakmode;           Line break 8 @property (ReadWrite) cgfloat minimumlineheight;           Minimum row height 9 @property (readwrite) cgfloat maximumlineheight;  Maximum line height of @property (readwrite) nswritingdirection basewritingdirection; Writing style (Nswritingdirectionnatural,nswritingdirectionlefttoright,nswritingdirectionrighttoleft)
One @property (readwrite) cgfloat lineheightmultiple;
@property (ReadWrite) cgfloat Paragraphspacingbefore;
@property (readwrite) float hyphenationfactor;
@property (readwrite,copy,ns_nonatomic_iosonly) nsarray *tabstops Ns_available_ios (7_0);
@property (readwrite,ns_nonatomic_iosonly) cgfloat defaulttabinterval Ns_available_ios (7_0);
[Paragraph style demo]
1 UILabel * lable = [[UILabel alloc] Initwithframe:cgrectmake (self.view.frame.size.width-100, 200)]; 2 Lable.backgroundcolor = [Uicolor Lightgraycolor]; 3 Lable.numberoflines = 0; 4 [Self.view addsubview:lable]; 5 6 NSString * string = @ "Always believe this something wonderful is about \nto happen! "; 7 8//Rich Text 9 nsmutableattributedstring * attributedstring = [[Nsmutableattributedstring alloc] initwithstring: STRING];10 11//Paragraph style Nsmutableparagraphstyle * Paragraphstyle = [[Nsmutableparagraphstyle alloc] Init];13 #warning Lable.numberoflines must be 0, paragraph style only takes effect 15//Line spacing paragraphstyle.linespacing = 10.0;17//Paragraph spacing par agraphstyle.paragraphspacing = 20.0;19//paragraphstyle.basewritingdirection = nswritingdirectionlefttoright;21// Paragraphstyle.firstlineheadindent = 10.0;22//paragraphstyle.headindent = 50.0;23//paragraphstyle.tailindent = 200.0;24 [attributedstring addattribute:NSParagraphStyleAttributeName26 Value:paragraphstyle27 RANGE:NSM Akerange (0, string.length)];28 lable.attributedtext = attributedstring;

Nsforegroundcolorattributename Text foreground color

Nsbackgroundcolorattributename Text Background color

Nsligatureattributename conjoined word (nsnumber @0: No conjoined, @1: Default conjoined, system font does not include support for conjoined)

Nsunderlinestyleattributename Underline

Nsstrokecolorattributename valid only after Nsstrokewidthattributename has set a value (the default font color is the same as the foreground colour, and the foreground color is not valid if you set a color that is inconsistent with the foreground color)

Nsstrokewidthattributename after setting this property, the font becomes a hollow font with a font edge width of value set

The Nsbaselineoffsetattributename value is the NSNumber type, indicating the offset of the text upward from the other text baseline

Nsunderlinecolorattributename value is uicolor type, underline color (valid only if Nsunderlinestyleattributename value is @1)

Nsunderlinestyleattributename value is nsnumber type, underline width (default value is @0: underline width is 0--unrealistic underline, @1: string underlined)

A lot of properties, there are other need to command point to look at OK, if they do not understand the function of three fingers to click the keyword, or hold option to see the official document description believe will have some understanding, the other temporarily not introduced.

If there is a problem, please correct me, the younger brother in this humbly.

Here is an article on Rich text: http://www.2cto.com/kf/201409/334308.html

iOS Rich Text (i)

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.