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(0, self.string.length); NSMutableAttributedString *richString = [[NSMutableAttributedString alloc] initWithString:self.string]; self.font ? [richString addAttribute:NSFontAttributeName value:self.font range:range] : 0; self.textColor ? [richString addAttribute:NSForegroundColorAttributeName value:self.textColor range:range] : 0; self.paragraphStyle ? [richString addAttribute: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]; paragraphString.string = string; paragraphString.font = font; paragraphString.textColor = color; paragraphString.paragraphStyle = style; [paragraphString makeConfigEffective]; return paragraphString; } - (CGFloat)heightWithFixedWidth:(CGFloat)width { CGFloat height = 0; 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(0, self.string.length); NSMutableAttributedString *richString = [[NSMutableAttributedString alloc] initWithString:self.string]; self.font ? [richString addAttribute:NSFontAttributeName value:self.font range:range] : 0; self.textColor ? [richString addAttribute:NSForegroundColorAttributeName value:self.textColor range:range] : 0; self.paragraphStyle ? [richString addAttribute: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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2015-11-16 POP按钮动画
2014-11-16 DES加密(支持ARC与MRC)