gpt4 book ai didi

ios - 在Swift中使用NSRegularExpression在特定范围内为单词赋予属性

转载 作者:行者123 更新时间:2023-12-01 16:14:58 25 4
gpt4 key购买 nike

我有一些纯HTML字符串,其中一些带有标题标签<b>Title</b>

facilities: "<b>Facilities</b><br/>24-hour security, Barbecue area, Car park, Clubhouse, Function room, Gym, Outdoor swimming pool, Playground, Swimming pool<br/><br/><b>Rooms</b><br/>Dining room, Ensuites, Living room, Maid\'s room, Utility room<br/><br/><b>Outdoor</b><br/>Balcony<br/><br/><b>View</b><br/>City, Open<br/><br/><b>Direction</b><br/>South East"

因此,我使用 NSRegularExpression模式从字符串中提取标题并将其存储在字符串数组中。然后,我将这些标题设为 粗体(归因字符串)并显示。所以这就是我这样做的方式:
var titlesArray = [String]()
let regex = try! NSRegularExpression(pattern: "<b>(.*?)</b>", options: [])
let basicDescription = facilities as NSString

regex.enumerateMatchesInString(facilities, options: [], range: NSMakeRange(0, facilities.characters.count)) { result, flags, stop in
if let range = result?.rangeAtIndex(1) {
titlesArray.append(basicDescription.substringWithRange(range))
}
}

let convertedDescription = facilities.html2String as NSString
let attributedString = NSMutableAttributedString(string: convertedDescription as String, attributes: [NSFontAttributeName:UIFont.systemFontOfSize(14.0)])
let boldFontAttribute = [NSFontAttributeName: UIFont.boldSystemFontOfSize(15.0)]

if titlesArray.count > 0 {
for i in 0..<titlesArray.count {
attributedString.addAttributes(boldFontAttribute, range: convertedDescription.rangeOfString(titlesArray[i]))
}
}

所以,一切都很好。但是问题是,有时我会收到带有Html标记的字符串,其中包含重复的单词,其中一个是带有标题标签的title,另一个只是一个简单的单词,我不需要加粗。但是此函数将查找该单词并将其在for循环内加粗,并忽略该简单单词后面的真实标题。

这就是我得到的:

enter image description here

因此,在这里,我该如何忽略第一个“室外”,并加粗我想要的第二个“室外”。感谢您的任何帮助。

最佳答案

在Objective-C中,在Swift中进行翻译应该不难(因为您似乎已经知道其中一些方法)。
attr1init(data:, options:, documentAttributes:)呈现。我没有添加任何其他效果(例如,粗体/标准,颜色的首选大小,您只需要枚举并更改效果)attr2的呈现方式与您使用正则表达式所需的方式相同。它只是不考虑所有标记,仅考虑粗体,而且我几乎没有编码新行的替换(<br/>\n)。但这可能是要使用的东西。我没有对您的正则表达式进行更多测试(while循环可能卡住了?)

NSString *str = @"<b>Facilities</b><br/>24-hour security, Barbecue area, Car park, Clubhouse, Function room, Gym, Outdoor swimming pool, Playground, Swimming pool<br/><br/><b>Rooms</b><br/>Dining room, Ensuites, Living room, Maid\'s room, Utility room<br/><br/><b>Outdoor</b><br/>Balcony<br/><br/><b>View</b><br/>City, Open<br/><br/><b>Direction</b><br/>South East";

NSError *errorAttr1 = nil;
NSAttributedString *attr1 = [[NSAttributedString alloc] initWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:&errorAttr1];
if (errorAttr1)
{
NSLog(@"Error AttributedStr Conversion with initWithData:options:documentsAttributes:error: %@", errorAttr1);
}
else
{
NSLog(@"attr1: %@", attr1);
[_tv1 setAttributedText:attr1];

}

str = [str stringByReplacingOccurrencesOfString:@"<br/>" withString:@"\n"];

NSError *errorRegex = nil;
NSString *openingTag = @"<b>";
NSString *closingTag = @"</b>";
NSString *pattern = [NSString stringWithFormat:@"%@(.*?)%@", openingTag, closingTag];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&errorRegex];
if (errorRegex)
{
NSLog(@"Error regex: %@", errorRegex);
return;
}


NSDictionary *boldAttributes = @{NSForegroundColorAttributeName:[UIColor darkGrayColor],
NSFontAttributeName:[UIFont boldSystemFontOfSize:15]};
NSDictionary *normalAttributes = @{NSForegroundColorAttributeName:[UIColor darkGrayColor],
NSFontAttributeName:[UIFont systemFontOfSize:14]};

NSMutableAttributedString *attr2 = [[NSMutableAttributedString alloc] initWithString:str attributes:normalAttributes]; //Add the initial attributes there

//Now we'll add the specific attribues
NSTextCheckingResult *match = [regex firstMatchInString:[attr2 string] options:0 range:NSMakeRange(0, [attr2 length])];
while (match)
{
NSRange range = [match range];
NSString *foundStr = [[attr2 string] substringWithRange:range];
NSAttributedString *temp = [[NSAttributedString alloc] initWithString:[foundStr substringWithRange:NSMakeRange([openingTag length], [foundStr length]-[openingTag length]-[closingTag length])] attributes:boldAttributes];
[attr2 replaceCharactersInRange:range withAttributedString:temp];
match = [regex firstMatchInString:[attr2 string] options:0 range:NSMakeRange(0, [attr2 length])];
}
NSLog(@"attr2: %@", attr2);
[_tv2 setAttributedText:attr2];
_tv1_tv2是两个 UITextView( IBOulet)。
它呈现:( _tv1是最上面的一个, _tv2是第二个)。

enter image description here

关于ios - 在Swift中使用NSRegularExpression在特定范围内为单词赋予属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41745894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com