ParagraphString, paragraph

Source: Internet
Author: User

ParagraphString, paragraph

ParagraphString-simple processing of paragraph styles

 

Effect

 

Source code

ParagraphString in the https://github.com/YouXianMing/UI-Component-Collection

//// ParagraphString. h // RichString // Created by YouXianMing on 2016/11/11. // Copyright 2016 TechCode. all rights reserved. // # import <Foundation/Foundation. h> # import <UIKit/UIKit. h> # import "BaseParagraphStyle. h "@ interface ParagraphString: NSObject/** The input string. */@ property (nonatomic, strong) NSString * string;/** Set the string's font, default is nil. */@ property (nonatomic, strong) UIFont * font;/** Set the string's textColor, default is nil. */@ property (nonatomic, strong) UIColor * textColor;/** Set the paragraph style, default is nil. */@ property (nonatomic, strong) BaseParagraphStyle * paragraphStyle;/** Make the config (Font, textColor, paragraphStyle) Valid tive. */-(void) makeconfigpolictive;/** The attributedString, before you get this, you shoshould set property and run makeconfigpolictive first. */@ property (nonatomic, strong, readonly) NSMutableAttributedString * attributedString;/** The string's height with the fixed width. @ param width The specified width. @ return The string's height. */-(CGFloat) heightWithFixedWidth :( CGFloat) width;/** The string's height with the fixed width. @ param lines The number of lines. @ param width The specified width. @ return The string's height. */-(CGFloat) numberOfLines :( NSInteger) lines fixedWidth :( CGFloat) width;/** ParagraphString's constructor. @ param string The string. @ param font The font. @ param color The color. @ param style The paragraph style. @ return The ParagraphString's instance. * // + (instancetype) paragraphStringWithString :( NSString *) string font :( UIFont *) font color :( UIColor *) color paragraphStyle :( BaseParagraphStyle *) style; @ end
//// ParagraphString. m // RichString /// Created by YouXianMing on 2016/11/11. // Copyright 2016 TechCode. all rights reserved. // # import "ParagraphString. h "@ interface ParagraphString () @ property (nonatomic, strong) NSMutableAttributedString * attributedString; @ end @ implementation ParagraphString-(void) makeconfigconfigurtive {if (self. string) {nsange range = NSMakeRange (0, self. string. length); NSMutab LeAttributedString * richString = [[NSMutableAttributedString alloc] initWithString: self. string]; self. font? [RichString addattriename: NSFontAttributeName value: self. font range: range]: 0; self. textColor? [RichString addattristyle: NSForegroundColorAttributeName value: self. textColor range: range]: 0; self. paragraphStyle? [RichString addattriename: NSParagraphStyleAttributeName value: self. paragraphStyle range: range]: 0; self. attributedString = richString;} else {self. attributedString = nil; }}+ (instancetype) paragraphStringWithString :( NSString *) string font :( UIFont *) font color :( UIColor *) color paragraphStyle :( BaseParagraphStyle *) style {ParagraphString * paragraphString = [[self class] alloc] init]; paragraphStr Ing. string = string; paragraphString. font = font; paragraphString. textColor = color; paragraphString. paragraphStyle = style; [paragraphString makeconfigvalid tive]; return paragraphString;}-(CGFloat) heightWithFixedWidth :( CGFloat) width {CGFloat height = 0; if (self. attributedString) {CGRect rect = [self. attributedString boundingRectWithSize: CGSizeMake (width, MAXFLOAT) options: NSStringDrawingTrunc AtesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context: nil]; height = rect. size. height;} return height;}-(CGFloat) numberOfLines :( NSInteger) lines fixedWidth :( CGFloat) width {nsange range = NSMakeRange (0, self. string. length); NSMutableAttributedString * richString = [[NSMutableAttributedString alloc] initWithString: self. string]; self. font? [RichString addattriename: NSFontAttributeName value: self. font range: range]: 0; self. textColor? [RichString addattristyle: NSForegroundColorAttributeName value: self. textColor range: range]: 0; self. paragraphStyle? [RichString addattriename: NSParagraphStyleAttributeName value: self. paragraphStyle range: range]: 0; UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (0, 0, width, 0)]; label. numberOfLines = lines; label. attributedText = richString; [label sizeToFit]; return label. frame. size. height;} @ end
/// BaseParagraphStyle. h // RichString // Created by YouXianMing on 2016/11/11. // Copyright 2016 TechCode. all rights reserved. // # import <UIKit/UIKit. h> @ interface BaseParagraphStyle: NSMutableParagraphStyle @ end
/// BaseParagraphStyle. m // RichString /// Created by YouXianMing on 2016/11/11. // Copyright 2016 TechCode. all rights reserved. // # import "BaseParagraphStyle. h "@ implementation BaseParagraphStyle @ 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.