IOS rich text (rich text display and calculated height) (two)

There is no basis for the students, you can see me explain the basis of articles

IOS rich text (text and pictures displayed in tabs)

Here we’ll talk about quick setting, rich text display, and calculation of rich text height

NSAttributedString


Click download

Set the paragraph style (without considering the indent situation)

* / * * * * @param lineSpacing set the paragraph style for high @param * textcolor * @param font font font color * * * - @return rich text (NSAttributedString *) stringWithParagraphlineSpeace: (CGFloat) lineSpacing textColor: (UIColor * textcolor) textFont: (UIFont * font) {/ / NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle a paragraph alloc] init]; paragraphStyle.lineSpacing = lineSpacing; / / NSKernAttributeName *attributes = NSParagraphStyleAttributeName: NSDictionary fonts @{paragraphStyle, NSKernAttributeName:@1.5f}; NSMutableAttributedString * attriStr = [[NSMutableAttributedString alloc] InitWithString:self attributes:attributes]; / / create a text attribute NSDictionary * attriBute = @{NSForegroundColorAttributeName:textcolor, NSFontAttributeName:font}; [attriStr addAttributes:attriBute range:NSMakeRange (0, self.length); return attriStr;}

Calculate rich text font height

/ * * * calculation rich text font height * * @param lineSpeace * @param * @param high font fonts width fonts for width * * height * / @return rich text - (CGFloat) getSpaceLabelHeightwithSpeace: (CGFloat) lineSpeace withFont: (UIFont*) font withWidth: (CGFloat) width *paraStyle alloc] init] {NSMutableParagraphStyle = [[NSMutableParagraphStyle; / / paraStyle.lineBreakMode = NSLineBreakByCharWrapping; / * * for high / paraStyle.lineSpacing = lineSpeace; *dic = NSDictionary / NSKernAttributeName fonts @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f} CGSize size = [self; boundingRectWithSize:CGSizeMake (width, MAXFLOAT) optio Ns:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size; return size.height;}
/******************************************************************** * return keyword rich text editor * * @param lineSpacing * @param * textcolor high font color font @param font * @param KeyColor * @param KeyFont keyword keyword font color font @param * * * @return KeyWords keyword array ********************************************************************/ - (NSAttributedString) stringWithParagraphlineSpeace: (CGFloat) lineSpacing textColor: (UIColor * textcolor textFont: (UIFont) *) font withKeyTextColor: (UIColor *) KeyColor K EyFont: (UIFont *) KeyFont keyWords: (NSArray *) KeyWords;

The.M file is as follows

/******************************************************************** * return keyword rich text editor * * @param lineSpacing * @param * textcolor high font color font @param font * @param KeyColor * @param KeyFont keyword keyword font color font @param * * * @return KeyWords keyword array *******************************************************************/ - (NSAttributedString) stringWithParagraphlineSpeace: (CGFloat) lineSpacing textColor: (UIColor * textcolor textFont: (UIFont) *) font withKeyTextColor: (UIColor * KeyColor) KeyFont: (UIFont * KeyFont) keyWords: (NSArray * KeyWords) {NSAttributedString = [self * AttributeString stringWithParagraphlineSpeace:lineSpacing textColor:textcolor textFont:font compated:^ (NSMutableAttributedString *attriStr) {NSDictionary * KeyattriBute = @{NSForegroundColorAttributeName:KeyColor, NSFontAttributeName:KeyFont}; for (NSString * item in KeyWords NSRange range = [self rangeOfString:item) {options: (NSCaseInsensitiveSearch)]; [attriStr addAttributes:KeyattriBute range:range];}}]; return AttributeString /********************************************************************}; * * @param * Basic Calibration Method Lin ESpacing high textcolor * @param * @param * font font color font font color @param KeyColor keywords * @param * @param KeyWords font KeyFont keyword keyword character array * * @return < #return value description#> ********************************** **********************************/ - (NSAttributedString) stringWithParagraphlineSpeace: (CGFloat) lineSpacing textColor: (UIColor * textcolor) textFont: (UIFont * compated: (font) void (^) (NSMutableAttributedString * attriStr) compalted) {/ / NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphSt a paragraph YLE alloc] init]; paragraphStyle.lineSpacing = lineSpacing; *attributes = NSDictionary / NSKernAttributeName fonts @{NSParagraphStyleAttributeName:paragraphStyle, NSKernAttributeName:@1.5f}; NSMutableAttributedString attriStr = "NSMutableAttributedString alloc] initWithString:self * attributes:attributes]; / / create a text attribute of NSDictionary * attriBute = @{NSForegroundColorAttributeName:textcolor, NSFontAttributeName:font}; [attriStr addAttributes:attriBute range:NSMakeRange (0, self.length)]; if (compalted) compalted (attriStr) {return}; attriStr;}

Gentlemen, let’s see how we can use it

The most dramatic scenario is that we have to figure out the height of this rich text to do something. Here we calculate the height of the rich text so that he can fully display it

Set a rich text
/ / Frma is not set, we need to set the UILabel *label=[[UILabel alloc]init] according to his own size; label.backgroundColor=[UIColor grayColor]; label.numberOfLines=0; [self.view addSubview:label]; NSString * conts = @ "in 3000 BC, the use of digital river basin of India residents have more common residents using the calculation method of the decimal system. During the Vedic period (1500 BC - 500 BC), the Aryan people were aware of the role of numbers in productive activities and daily life, creating simple and incomplete numbers. /n/n third Century BC, India appeared a set of numbers, but the writing is not a country, which is the most typical of the Brahmin, originality is from 1 to 9 each number has a special symbol, modern digital from them is born out of the. /n at that time, "0" has not yet appeared. The Gupta era (300 to 500) is "0", called "Shun if" (Shunya), the representation is a black spot "-", later evolved into a "0". /n so a whole set of numbers came into being. This work of creation also contributed greatly to world culture. LLL "; / / set the rich text display, label.attributedText = [conts stringWithParagraphlineSpeace:6 textColor:[UIColor redColor] textFont:[UIFont systemFontOfSize:15]];
Calculate the height of the rich text and set its height
/ / calculation rich text height CGFloat contHeight = [conts getSpaceLabelHeightwithSpeace:6 withFont:[UIFont systemFontOfSize:15] withWidth:300]; label.frame = CGRectMake (0, 50, 300, contHeight);

This is over. The big guy can do it…
if the problem can be added to my QQ:1290925041
can also add QQ group: 234812704 (Geeker brother College)
welcome a study, to improve the force!
can also add WeChat public number Geeker’s brother

More news

More information please refer to the WeChat iOS development letter public attention Geeker brother, do not regularly push dry cargo:

IOS rich text (rich text display and calculated height) (two)
, here’s the picture description