如何在目标C中使用html保存粗体字体?

结束

我在uitextview中键入了一些文本,并且还选择了一些文本并将其设置为粗体。之后,我要将这些数据保存到我的应用程序中。现在,当我要获取该数据时,它将不会显示与我保存的相同。

它不保存粗体。

下面是代码:

实用标记-btnActions

   -(IBAction)btnActions:(UIButton *)sender
        {
            [self addOrRemoveFontTraitWithName:@"Bold" andValue:UIFontDescriptorTraitBold];

        }

实用标记-私有方法的实现

-(void)addOrRemoveFontTraitWithName:(NSString *)traitName andValue:(uint32_t)traitValue{
NSRange selectedRange = [txtViewNote selectedRange];

NSDictionary *currentAttributesDict = [txtViewNote.textStorage attributesAtIndex:selectedRange.location
                                                                  effectiveRange:nil];

UIFont *currentFont = [currentAttributesDict objectForKey:NSFontAttributeName];

UIFontDescriptor *fontDescriptor = [currentFont fontDescriptor];

NSString *fontNameAttribute = [[fontDescriptor fontAttributes] objectForKey:UIFontDescriptorNameAttribute];
UIFontDescriptor *changedFontDescriptor;

if ([fontNameAttribute rangeOfString:traitName].location == NSNotFound) {
    uint32_t existingTraitsWithNewTrait = [fontDescriptor symbolicTraits] | traitValue;
    changedFontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:existingTraitsWithNewTrait];
}
else{
    uint32_t existingTraitsWithoutTrait = [fontDescriptor symbolicTraits] & ~traitValue;
    changedFontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:existingTraitsWithoutTrait];
}

UIFont *updatedFont = [UIFont fontWithDescriptor:changedFontDescriptor size:0.0];

NSDictionary *dict = @{NSFontAttributeName: updatedFont};

[txtViewNote.textStorage beginEditing];
[txtViewNote.textStorage setAttributes:dict range:selectedRange];
[txtViewNote.textStorage endEditing];
}

实用标记-btnSave

-(IBAction)btnSave:(id)sender
  {
            NSURL *documentDirectoryURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];

            NSURL *documentURL = [documentDirectoryURL URLByAppendingPathComponent:@"test.html"];


            NSString *htmlCode = txtViewNote.text;

            NSError* error;

            if (![htmlCode writeToURL:documentURL atomically:YES encoding:NSUTF8StringEncoding error:&error]) {
                NSLog(@"Couldn't save file because: %@", error);
            }

            NSString* fileToUpload = [NSString stringWithContentsOfURL:documentURL encoding:NSUTF8StringEncoding error:&error];

            if (!fileToUpload) {
                NSLog(@"Couldn't read file because: %@", error);
            }


}

可以给我HTML吗?

卑诗省

问题是您要保存textView的纯文本而不是attributedText。

NSString * htmlCode = txtViewNote.text;

相反,您应该像这样保存属性文本:

NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:NSHTMLTextDocumentType,NSDocumentTypeDocumentAttribute, nil];
NSData *htmlData = [[self txtViewNote].attributedText dataFromRange:NSMakeRange(0, [self txtViewNote].attributedText.length) documentAttributes:attributes error:NULL];
NSString *htmlCode = [[NSString alloc]initWithData:htmlData encoding:NSUTF8StringEncoding];

如果要将htmlCode写入textView,则应该:

NSMutableAttributedString *tmp = [[NSMutableAttributedString alloc] initWithData:htmlData options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} documentAttributes:nil error:nil];
[txtViewNote setAttributedText:tmp];

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在HTML中使文本变为粗体?

如何在目标C中使用UIImpactFeedbackGenerator?

如何在html canvas中将字体文件用于粗体,斜体等?

如何在html / css中使用字体

如何在HTML和CSS中使用超棒的字体?

如何在目标C中使用NSPredicate指定范围

如何在iOS的Realm中使用主键(目标c)

OpenCV:如何在目标 c 中使用凸包?

如何在Bootstrap中使文本标签变为常规字体(非粗体)?

如何在swift 4中将html字符串设置为标签时使用常规和粗体字体设置自定义字体?

如何在单个String中使用常规和粗体?

linux +如何在echo命令中使用tput粗体

如何在WatchKit中使用等宽字体

如何在neovim中使用Operator字体?

如何在QML中使用Awesome字体

如何在 vscode 中使用下载的字体?

如何在 Xcode 中使用 iOS 目标?

如何在Python中使文本变为粗体?

如何在 React 和 Material Ui 中使选择列表文本的某些部分变为粗体和一些正常字体

如何在C ++中使用Class函数保存大量数字

想要在 html 中使用 * 的粗体文本

如何在Apache FOP中自动创建斜体/粗体字体?

如何在Resharper中禁用局部变量字体粗体

如何在Plotly中设置粗体字体样式

如何在C中使用cons char函数设置目标C标签

如何使用 OpenSans 自定义字体在 **Swift 4** 中使文本范围带有下划线和粗体

如何在VB,C#中使用itextsharp为utf char字体着色

Xamarin.Forms:如何在 C# 代码中使用自定义字体

如何在目标c-swift桥接项目中使用intentdefinition文件?