IOS AttributeString Line break

Source: Internet
Author: User

-(void) awakefromnib {

[Self.descriptionlabel addobserver:self forkeypath:@ "text" Options:nskeyvalueobservingoptionnew Context:nil];

[Super awakefromnib];

Initialization code

}

-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (nsdictionary<nsstring *,id> *) Change context: (void *) context

{

Self.descriptionLabel.attributedText = [[self class] attributeWithString:self.descriptionLabel.text] copy];

}

+ (nsmutableattributedstring *) attributewithstring: (NSString *) string

{

nsmutableattributedstring *muattrstring = [[Nsmutableattributedstring alloc] initwithstring:string];

Nsmutableparagraphstyle *paragtaphstyle = [[Nsmutableparagraphstyle alloc] init];

Paragtaphstyle.alignment = nstextalignmentjustified;

paragtaphstyle.paragraphspacing = 11.0;

Paragtaphstyle.paragraphspacingbefore = 10.0;

paragtaphstyle.headindent = 0.0;

Paragtaphstyle.linebreakmode = nslinebreakbywordwrapping;

Paragtaphstyle.tailindent = 30.0;

Paragtaphstyle.firstlineheadindent = self.descriptionLabel.font.pointSize * 2.0;

Nsdictionary *dic = @{

Nsforegroundcolorattributename:colors (@ "#585C67"),

Nsfontattributename:fontwithsize (14.0),

Nsparagraphstyleattributename:paragtaphstyle,

nsunderlinestyleattributename:@ (Nsunderlinestylenone)

};

[muattrstring Setattributes:dic range:nsmakerange (0, muattrstring.length)];

return muattrstring;

}

+ (CGFloat) descriptionlabelwidth

{

return screen_width-30*2;

}

+ (CGFloat) Descriptionheightwithtext: (NSString *) Text width: (cgfloat) width

{

Nsmutableparagraphstyle *paragtaphstyle = [[Nsmutableparagraphstyle alloc] init];

Paragtaphstyle.alignment = nstextalignmentjustified;

paragtaphstyle.paragraphspacing = 11.0;

Paragtaphstyle.paragraphspacingbefore = 10.0; Spacing between paragraphs

paragtaphstyle.headindent = 0.0;

Paragtaphstyle.linebreakmode = nslinebreakbywordwrapping;

Cgsize size = [Text Boundingrectwithsize:cgsizemake (width, Cgfloat_max)

Options:nsstringdrawingusesfontleading| Nsstringdrawinguseslinefragmentorigin

Attributes:@{nsfontattributename:fontwithsize (14.0),

Nsparagraphstyleattributename:paragtaphstyle}

Context:nil].size;

Return size.height + 10.0 + 75.0 + paragtaphstyle.paragraphspacingbefore;

}

IOS attributestring Line wrapping

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.