首页 技术 正文
技术 2022年11月11日
0 收藏 540 点赞 4,561 浏览 4152 个字

ParagraphString – 段落样式的简易处理

ParagraphString – 段落样式的简易处理

效果

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
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,494
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297