更改 UITextView 字体而不删除粗体/下划线/斜体格式 [英] Change UITextView Font w/o Removing Bold/Underline/Italics Formatting

查看:49
本文介绍了更改 UITextView 字体而不删除粗体/下划线/斜体格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 iOS 应用中有一个 UITextView,我想允许用户随意加粗/斜体/下划线/等,所以我在文本视图上将 allowedEditingTextAttributes 属性设置为 true.但是,我还希望允许用户更改字体和颜色等内容.目前,似乎即使更改 UITextView 的颜色属性也会重置 UITextView 的格式(即摆脱粗体/斜体/等).有没有办法在不重置格式的情况下改变它?作为参考,我目前正在这样做以更改颜色:

I have a UITextView in an iOS app, and I want to allow the user to bold/italicize/underline/etc at will, so I gave set the allowsEditingTextAttributes property to true on the text view. However, I would also like to allow the user to change things like font and color. Currently, it seems that changing even the color property of the UITextView resets the formatting of the UITextView (i.e. gets rid of the bolding/italics/etc). Is there any way to change this without resetting the formatting? For reference, I am currently doing this to change the color:

self.textView.textColor = colorTheUserPicked //a UIColor picked by the user

EDIT:实际上,这是我的错,当我更改颜色时,我也在重置文本属性本身的值.删除它允许我根据需要更改颜色.但是,如果不删除粗体/斜体/等,我仍然无法更改字体甚至字体大小.我意识到这可能是不可能的,不过...

EDIT: Actually, this was my bad, I was also resetting the value of the text property itself when I changed the color. Removing that allowed me to change the color as desired. However, I still can't change the font or even the font size without removing the bold/italics/etc. I realize this might be impossible, though...

推荐答案

我会用一个 Objective-C 的解决方案来回答,因为我不会用 Swift 编写代码,但它应该很容易翻译成 Swift.

I'll answer with an Objective-C solution since I don't code in Swift, but it should be translated easily into Swift.

NSAttributedString 效果"存储在 NSDictionary 中.所以它是一个 Key:Value 系统(具有唯一性的 Key).
粗体/斜体(来自您的示例)在 NSFontAttributedName 的值内.这就是为什么你不能像这样重新设置它.

NSAttributedString "effects" are stored in a NSDictionary. So it's a Key:Value system (with unicity of Key).
Bold/Italic (from your example) are inside the NSFontAttributedName's value. That's why you can't set it again like this.

主要是使用enumerateAttributesInRange:options:usingBlock::

[attr enumerateAttributesInRange:NSMakeRange(0, [attr length])
                         options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
                      usingBlock:^(NSDictionary *attributes, NSRange range, BOOL *stop)
 {
     NSMutableDictionary *newAttributes = [attributes mutableCopy];
    //Do changes
    [attr addAttributes:newAttributes range:range]; //Apply effects
 }];

以下几行位于//Do changes"处.所以,如果你想改变字体大小:

The following lines are at "//Do changes" place. So, if you want to change the Font size:

if ([newAttributes objectForKey:NSFontAttributeName])
{
    UIFont *currentFont = (UIFont *)[attributes objectForKey:NSFontAttributeName];
    UIFont *newFont = [UIFont fontWithName:[currentFont fontName] size:[currentFont pointSize]*0.5];//Here, I multiplied the previous size with 0.5
    [newAttributes setValue:newFont forKey:NSFontAttributeName];
}

如果你想加粗/斜体等你可能感兴趣的字体 UIFontDescriptor一个相关问题.

If you want to bold/italic, etc. the font you may be interested in UIFontDescriptor, and a related question.

它向您展示了如何获取以前的字体并更改获取与以前字体有一些共同点的新字体.

It shows you how to get the previous font and change get a new font with some commons with the previous one.

如果你想改变颜色:

if ([newAttributes objectForKey:NSForegroundColorAttributeName])//In case if there is some part of the text without color that you don't want to override, or some custom- things
{
     UIColor *newColor = [UIColor blueColor];
    [newAttributes setValue:newColor forKey:NSForegroundColorAttributeName];
}

下划线效果在NSUnderlineStyleAttributeName的值中.所以可以做类比的变化.

The underline effect is in NSUnderlineStyleAttributeName's value. So analogic changes can be done.

这篇关于更改 UITextView 字体而不删除粗体/下划线/斜体格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆