Label _ set the line spacing, word spacing, and calculate the label height with line spacing. The line spacing label

Source: Internet
Author: User

Label _ set the line spacing, word spacing, and calculate the label height with line spacing. The line spacing label

//

// ViewController. m

// CNBlogs

//

// Created by PXJ on 16/5/27.

// Copyright©2016 PXJ. All rights reserved.

//

 

# Import "ViewController. h"

 

@ Interface ViewController ()

 

@ End

 

@ Implementation ViewController

 

-(Void) viewDidLoad {

[Super viewDidLoad];

NSString * conText = @ "\ n your mouth is slightly tilted, sexy, hopeless \ n you can't imagine such a heartbeat, everything you want \ n soft drinks, rising bubbles, I will be right for you I like to drink a whole bottle. \ n there is no shortage here. You are excited to hug. \ n candle fire is burning. There is a certain sentiment. Your eyes are scorched for a few seconds. \ n you are lazy twisting about your dance. the waist is unbearable \ n you smile with the wind \ n the smell of rosemary with a mint-flavored coquetry sent a sign of love to me \ n your elegance is like a cat movements light around the sweetness of love is spreading, and it's just \ n. Soft Drinks are rising. I will drink all your preferences in a bottle \ n. The best thing you can do here is to stir up your emotions and give it a hug. \ n candle fire burning has a certain sentiment eyes lost for a few seconds \ n about your dance you're lazy twisting waist can't stand \ n you are flying with the wind smile with the smell of rosemary band mint-flavored coquetry sends out a sign of love to me \ n your elegant action is as simple as a cat, spread around the sweetness of love, and it's just \ n ";

UIFont * font = [UIFont systemFontOfSize: 12]; // font size

CGFloat lineSpace = 8; // row spacing

CGFloat paragraphSpacing = 0; // segment spacing

CGSize labSize = CGSizeMake (400,100 0); // label width and height

NSNumber * textLengthSpace = @ 0.2; // word spacing

NSDictionary * dic = [self setTextLineSpaceWithString: conText withFont: font withLineSpace: lineSpace withTextlengthSpace: textLengthSpace paragrapheat acing: paragrapheat acing];

CGSize size = [conText boundingRectWithSize: labSize options: NSStringDrawingUsesLineFragmentOrigin attributes: dic context: nil]. size;

CGFloat sizeHeight = size. height; // lab height

 

// CGFloat sizeHeight = [self getSpaceLabelHeight: conText withFont: font withLineSpace: lineSpace size: labSize textlengthSpace: textLengthSpace paragrapheat acing: paragrapheat acing];

//

UILabel * lab = [[UILabel alloc] init];

Lab. numberOfLines = 0;

Lab. backgroundColor = [UIColor yellowColor];

Lab. frame = CGRectMake (20, 20, 400, sizeHeight );

Lab. attributedText = [[NSAttributedString alloc] initWithString: conText attributes: dic];

[Self. view addSubview: lab];

}

 

/*

* Set row spacing and word spacing for UILabel

*/

-(NSDictionary *) syntax :( NSString *) str withFont :( UIFont *) font withLineSpace :( CGFloat) lineSpace usage :( NSNumber *) textlengthSpace paragrapheat acing :( CGFloat) paragrapheat acing {

NSMutableParagraphStyle * paraStyle = [[NSMutableParagraphStyle alloc] init];

ParaStyle. lineBreakMode = NSLineBreakByCharWrapping;

ParaStyle. alignment = NSTextAlignmentLeft;

ParaStyle. lineSpacing = lineSpace; // you can specify the line spacing.

ParaStyle. hyphenationFactor = 1.0;

ParaStyle. firstLineHeadIndent = 0.0;

ParaStyle. paragraphSpacingBefore = 0.0;

ParaStyle. headIndent = 0;

ParaStyle. tailIndent = 0;

NSDictionary * dic =@{ NSFontAttributeName: font,

NSParagraphStyleAttributeName: paraStyle,

NSKernAttributeName: textlengthSpace

};

Return dic;

}

 

/*

* Calculate the UILabel height (with row spacing)

*/

 

 

// (CGFloat) returns :( NSString *) str withFont :( UIFont *) font withLineSpace :( CGFloat) lineSpace size :( CGSize) textSize textlengthSpace :( NSNumber *) textlengthSpace returns :( CGFloat) returns

//{

//

//

// NSMutableParagraphStyle * paraStyle = [[NSMutableParagraphStyle alloc] init];

// ParaStyle. lineBreakMode = NSLineBreakByCharWrapping;

// ParaStyle. alignment = NSTextAlignmentLeft;

// ParaStyle. lineSpacing = lineSpace;

// ParaStyle. paragrapheat acing = paragrapheat acing;

// ParaStyle. hyphenationFactor = 1.0;

// ParaStyle. firstLineHeadIndent = 0.0;

// ParaStyle. paragraphSpacingBefore = 0.0;

// ParaStyle. headIndent = 0;

// ParaStyle. tailIndent = 0;

// NSDictionary * dic =@{ NSFontAttributeName: font,

// NSParagraphStyleAttributeName: paraStyle,

// NSKernAttributeName: textlengthSpace

//};

// CGSize size = [str boundingRectWithSize: textSize options: NSStringDrawingUsesLineFragmentOrigin attributes: dic context: nil]. size;

// Return size. height;

//}

//

@ End

 

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.