ParagraphString - 段落样式的简易处理

效果

源码

https://github.com/YouXianMing/UI-Component-Collection 中的 ParagraphString

//
// 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) effective.
*/
- (void)makeConfigEffective; /**
The attributedString, before you get this, you should set property and run makeConfigEffective 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)makeConfigEffective { if (self.string) { NSRange range = NSMakeRange(, self.string.length); NSMutableAttributedString *richString = [[NSMutableAttributedString alloc] initWithString:self.string]; self.font ? [richString addAttribute:NSFontAttributeName value:self.font range:range] : ;
self.textColor ? [richString addAttribute:NSForegroundColorAttributeName value:self.textColor range:range] : ;
self.paragraphStyle ? [richString addAttribute:NSParagraphStyleAttributeName value:self.paragraphStyle range:range] : ; 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];
paragraphString.string = string;
paragraphString.font = font;
paragraphString.textColor = color;
paragraphString.paragraphStyle = style;
[paragraphString makeConfigEffective]; return paragraphString;
} - (CGFloat)heightWithFixedWidth:(CGFloat)width { CGFloat height = ; if (self.attributedString) { CGRect rect = [self.attributedString boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
context:nil]; height = rect.size.height;
} return height;
} - (CGFloat)numberOfLines:(NSInteger)lines fixedWidth:(CGFloat)width { NSRange range = NSMakeRange(, self.string.length);
NSMutableAttributedString *richString = [[NSMutableAttributedString alloc] initWithString:self.string]; self.font ? [richString addAttribute:NSFontAttributeName value:self.font range:range] : ;
self.textColor ? [richString addAttribute:NSForegroundColorAttributeName value:self.textColor range:range] : ;
self.paragraphStyle ? [richString addAttribute:NSParagraphStyleAttributeName value:self.paragraphStyle range:range] : ; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , width, )];
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

ParagraphString - 段落样式的简易处理的更多相关文章

  1. CSS3初学篇章_4(边框样式/段落样式)

    边框样式 1.边框线语法:border-style : none | hidden | dotted | dashed | solid | double | groove | ridge | inse ...

  2. css中的段落样式及背景

    一.段落样式 css中关于段落的样式主要有行高,缩进,段落对齐,文字间距,文字溢出,段落换行等.它们的具体语法如下: line-height : normal | length text-indent ...

  3. bootstrap学习笔记<二>(标题,段落样式)

    标题.样式:class="h1"~class="h6" bootstrap中重新定义了h1~h6标签,具体差别如下: 在bootstrap中其他任何标签使用cl ...

  4. HTML标题 段落 样式

    HTML 标题 标题(Heading)是通过 <h1> - <h6> 等标签进行定义的. <h1> 定义最大的标题.<h6> 定义最小的标题. 注释:默 ...

  5. iOS-文本段落样式NSMutableParagraphStyle与NSParagraphStyle的使用和一些富文本处理属性

    开发过程中,经常会遇到动态计算行高的问题, - (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)op ...

  6. CSS 文章段落样式

    #adiv p { text-align: left; text-indent: 2em; line-height:25px; font-family:微软雅黑; font-size:medium; ...

  7. css段落样式

    字间距 letter-spacing 首行缩进 text-indent

  8. CSS常用样式(二)

    一.边框样式 1.border:复合属性.设置对象边框的特性. 取值: border-width: 设置或检索对象边框宽度. border-style: 设置或检索对象边框样式. border-col ...

  9. CSS第四天总结 更多的属性 圆角 边框图片 段落属性 颜色渐变 盒子阴影

    圆角边框: border-radius    一个值时表示四个角用的圆半径,4个值时分别是左上角.右上角.左下角.右下角,单位可以是px和百分比,百分比是半径相对于边框长度的比例 在CSS3中我们终于 ...

随机推荐

  1. CentOS7 Tomcat安装

    CentOS7 Tomcat安装 CentOS7 Tomcat安装 Download 从Tomcat下载apache-tomcat-8.0.18.tar.gz Install 安装 上传RPM文件到/ ...

  2. java中String的相等比较

    首先贴出测试用例: package test; import org.junit.Test; /** * Created by Administrator on 2015/9/16. * */ pub ...

  3. 【图论】深入理解Dijsktra算法

    1. Dijsktra算法介绍 Dijsktra算法是大牛Dijsktra于1956年提出,用来解决有向图单源最短路径问题.但不能解决负权的有向图,若要解决负权图则需要用到Bellman-Ford算法 ...

  4. HT for Web 3D游戏设计设计--汉诺塔(Towers of Hanoi)

    在这里我们将构造一个基于HT for Web的HTML5+JavaScript来实现汉诺塔游戏. 汉诺塔的游戏规则及递归算法分析请参考http://en.wikipedia.org/wiki/Towe ...

  5. StreamHelper

    public static MemoryStream CreateMemoryStreamFromBytes(byte[] inputData) { if (inputData == null || ...

  6. Python语言特性之2:元类

    问题:Python中的元类(metaclasses)是什么?一般使用它干什么? 原地址:http://stackoverflow.com/questions/100003/what-is-a-meta ...

  7. 【原创】验证代理IP是否有用

    /// <summary> /// 验证代理IP是否有用 /// </summary> /// <param name="ip">IP地址< ...

  8. javascript小数点后4舍5进

    toFixed();获得小数点后面多少为数字.四舍五入的方法. 例如: var floatNum = 5.234 var temp = floatNum.toFixed(1); -- temp = 5 ...

  9. 不用插件 让Firefox 支持网页翻译

    1.进入http://labs.microsofttranslator.com/bookmarklet/ 2.在语言选择框的下拉列表中选择“简体中文” 3.右键点击“翻译”按钮,选择“将此链接加为书签 ...

  10. No.013:Roman to Integer

    问题: Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from ...