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. Windows Azure Virtual Network (11) 创建VNet-to-VNet的连接

    <Windows Azure Platform 系列文章目录> 我们知道,Azure Virtual Network可以 1.将对台Azure VM加入到同一个网段里,同时绑定内网IP地址 ...

  2. Android Studio快捷键每日一练(2)

    原文地址:http://www.developerphil.com/android-studio-tips-of-the-day-roundup-2/ 12.复制行 苹果:Cmd+D    Windo ...

  3. C#基础04

    介绍:泛型介绍,索引,Foreach遍历的解释,yield方法,path文件操作,Directory类基本操作<目录> 一:泛型   百度资料:泛型是 2.0 版 C# 语言和公共语言运行 ...

  4. call、apply、bind的区别

    me.setname.call(me,"dpd","dpd12");      通过call执行say方法 ,第一个参数为作用对象,后边的一些列参数作为实参传入 ...

  5. C语言学习007:重定向标准输入和输出

    先来完成一个将输入数据转换成json格式输出的小任务 #include <stdio.h> int main(){ float latitude; float longtitude; ]; ...

  6. ASP.NET Core Web API 开发-RESTful API实现

    ASP.NET Core Web API 开发-RESTful API实现 REST 介绍: 符合REST设计风格的Web API称为RESTful API. 具象状态传输(英文:Representa ...

  7. C++ VS2012 内存泄露检测

    在VS2012中添加部分代码,可以起到检测内存泄露的作用. 今天刚刚收到的解决办法,原理还不是很清楚.先分享出来 1. 头文件中添加以下代码 #ifdef _DEBUG #define DEBUG_C ...

  8. Base64 字符串转图片 问题整理汇总

    前言 最近碰到了一些base64字符串转图片的开发任务,开始觉得没啥难度,但随着开发的进展还是发现有些东西需要记录下. Base64 转二进制 这个在net有现有方法调用: Convert.FromB ...

  9. IOS高德地图逆地理编码定位+网络判断

    先说下这功能的流程,  流程:判断用户是否联网--->获取用户地理位置经纬度--->通过经纬度去查询地理位置名称 //高德地图 @property (nonatomic, strong) ...

  10. HDU 5510---Bazinga(指针模拟)

    题目链接 http://acm.hdu.edu.cn/search.php?action=listproblem Problem Description Ladies and gentlemen, p ...