当前位置: 首页 > news >正文

西安知名的网站建设公司wordpress登录失败

西安知名的网站建设公司,wordpress登录失败,河北石家庄最新消息今天,做汽车保养的网站上1、编写逻辑 使用分类(Category)的方法拓展NSString,本文使用NSString (Markdown),NSString的分类来编写一个通用方法,使用正则表达式匹配字符串实现去除特殊字符,并自定义文字属性。 在接入AI大模型后,返回的字符串…

1、编写逻辑

使用分类(Category)的方法拓展NSString,本文使用NSString (Markdown),NSString的分类来编写一个通用方法,使用正则表达式匹配字符串实现去除特殊字符,并自定义文字属性。

在接入AI大模型后,返回的字符串会带有特殊字符用于做文字处理,下面代码简单进行了文字处理展示。

2、代码实现

1、NSString+Markdown.h

#import <Foundation/Foundation.h>NS_ASSUME_NONNULL_BEGIN@interface NSString (Markdown)- (NSAttributedString *)attributedStringFromMarkdown;@endNS_ASSUME_NONNULL_END

2、NSString+Markdown.m

文中做了处理 ### 与 **加粗文本** 的处理,可根据需求进行拓展

#import "NSString+Markdown.h"static NSRegularExpression *_headerRegex;
static NSRegularExpression *_boldRegex;
static dispatch_once_t onceToken;@implementation NSString (Markdown)- (NSAttributedString *)attributedStringFromMarkdown {NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self];// 设置默认字体,做自适应高度时,必须要设置默认字体UIFont *defaultFont = [UIFont systemFontOfSize:20];[attributedString addAttribute:NSFontAttributeName value:defaultFont range:NSMakeRange(0, attributedString.length)];// 一次性初始化正则表达式dispatch_once(&onceToken, ^{NSError *error;// 匹配 ### 标题_headerRegex = [NSRegularExpression regularExpressionWithPattern:@"^###\\s*(.*?)\\s*(?=\n)"options:NSRegularExpressionAnchorsMatchLineserror:&error];if (error) {NSLog(@"### 正则表达式初始化失败: %@", error.localizedDescription);}// 匹配 **加粗文本**_boldRegex = [NSRegularExpression regularExpressionWithPattern:@"\\*\\*(.*?)\\*\\*"options:0error:&error];if (error) {NSLog(@"** 正则表达式初始化失败: %@", error.localizedDescription);}});// 处理 "### 标题" 的加粗,并去掉 "###"NSArray *headerMatches = [_headerRegex matchesInString:attributedString.stringoptions:0range:NSMakeRange(0, attributedString.length)];for (NSTextCheckingResult *match in [headerMatches reverseObjectEnumerator]) {NSRange fullMatchRange = match.range;          // 包含 ### 的完整匹配范围NSRange contentRange = [match rangeAtIndex:1]; // 实际要加粗的内容if (contentRange.location != NSNotFound) {// 1️⃣ 应用加粗样式[attributedString addAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:20]} range:contentRange];// 2️⃣ 替换 "### 标题" 只保留标题文本NSString *content = [attributedString.string substringWithRange:contentRange];[attributedString replaceCharactersInRange:fullMatchRange withString:content];// 3️⃣重新获取新文本的位置NSRange newRange = NSMakeRange(fullMatchRange.location, content.length);// 重新加粗[attributedString addAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:20]} range:newRange];}}// 处理 "**加粗文本**" 的加粗,并去掉 "**"NSArray *boldMatches = [_boldRegex matchesInString:attributedString.stringoptions:0range:NSMakeRange(0, attributedString.length)];for (NSTextCheckingResult *match in [boldMatches reverseObjectEnumerator]) {NSRange fullMatchRange = match.range;          // 包含 ** 的完整匹配范围NSRange contentRange = [match rangeAtIndex:1]; // 实际要加粗的内容if (contentRange.location != NSNotFound) {// 1️⃣ 应用加粗样式[attributedString addAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:20]} range:contentRange];// 2️⃣ 替换 "**加粗文本**" 只保留加粗文本NSString *content = [attributedString.string substringWithRange:contentRange];[attributedString replaceCharactersInRange:fullMatchRange withString:content];// 3️⃣重新获取新文本的位置NSRange newRange = NSMakeRange(fullMatchRange.location, content.length);// 重新加粗[attributedString addAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:20]} range:newRange];}}return attributedString;
}@end

3、调用方法

NSString *text = [message.text attributedStringFromMarkdown].string;

http://www.dtcms.com/a/611682.html

相关文章:

  • 如何在会议决策错误后进行纠正与说明
  • 哪个网站免费h5模板多网络推广外包注意哪些
  • 网站的布局怎么做采集文章留在网站
  • 淘宝做导航网站织梦网站搬家教程
  • 男人直接做的视频网站注册网站会不会有问题
  • 在那个网站做直播好赚钱吗企业网站seo模板
  • 无锡网站建设开发外贸自建站类型
  • 晋江网站建设联系电话天眼查网站建设公司
  • 什么网站都有漏洞电脑上安装wordpress
  • 宁波网站搭建公司定制企业营销推广
  • ISTJ+RCI_突然的想法-称之为灵感
  • 手机做网站用什么软件吐鲁番高端网站建设平台
  • Java 黑马程序员学习笔记(进阶篇26)
  • 网站开发的进度怎么写网站开发里程碑
  • Xshell终端连接Ubuntu/Debian无颜色的解决方案
  • 国外推广网站有哪些网页设计介绍北京网站
  • 贵港网站建设兼职企业做网站需要什么
  • 怎样在网站做转向连接网站建设素材模板下载
  • 音乐网站建站如何做视频网站
  • 广东宇晟建设工程有限公司网站注册公司取名推荐
  • 网站开发遵循的标准或规范陕西政务服务网
  • 平板电脑可以做网站不张家港网站网络公司
  • docker中安装conda环境
  • 网站模块顺序调整云服务器服务安全
  • 网站开发和网页上传的说法用vscode做网站
  • Bootstrap Wells
  • 网站设计中下拉列表怎么做网费一年多少钱
  • 网站主页和子页风格如何统一提升网站权重的策略
  • 却持网站招聘网站代做
  • 做代理的网站wordpress系统是什么意思