ios 设置文本属性后正在重置 UITextView 样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19049917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITextView style is being reset after setting text property
提问by B?a?ej
I have UITextView *_masterText
and after call method setText
property font is being reset.
It's happening after I change sdk 7.
_masterText is IBOutlet
, global and properties are set in storyboard. It's only me or this is general SDK bug?
我有UITextView *_masterText
并且在调用方法setText
属性字体之后被重置。它发生在我更改 sdk 7 之后。 _masterText 是IBOutlet
,全局和属性在故事板中设置。只有我一个人还是这是一般的 SDK 错误?
@interface myViewController : UIViewController
{
IBOutlet UITextView *_masterText;
}
@implementation myViewController
-(void)viewWillAppear:(BOOL)animated
{
[_masterText setText:@"New text"];
}
回答by Bosse Nilsson
Sitting with this for hours, I found the bug. If the property "Selectable" = NO it will reset the font and fontcolor when setText is used.
坐了几个小时,我发现了这个错误。如果属性“Selectable”= NO,它将在使用 setText 时重置字体和字体颜色。
So turn Selectable ON and the bug is gone.
因此,打开 Selectable,错误就消失了。
回答by Ken Steele
I ran into the same issue (on Xcode 6.1) and while John Cogan's answerworked for me, I found that extending the UITextView class with a category was a better solution for my particular project.
我遇到了同样的问题(在 Xcode 6.1 上),虽然John Cogan 的回答对我有用,但我发现使用类别扩展 UITextView 类是我特定项目的更好解决方案。
interface
界面
@interface UITextView (XcodeSetTextFormattingBugWorkaround)
- (void)setSafeText:(NSString *)textValue;
@end
implementation
执行
@implementation UITextView (XcodeSetTextFormattingBugWorkaround)
- (void)setSafeText:(NSString *)textValue
{
BOOL selectable = [self isSelectable];
[self setSelectable:YES];
[self setText:textValue];
[self setSelectable:selectable];
}
@end
回答by Chuy47
回答by John Cogan
Had this issue myself and the above answer helped but I added a wrapper to my ViewController code as follows and just pass the uiview instance and text to change and the wrapper function toggles the Selectable value on, changes text and then turns it off again. Helpful when you need the uitextview to be off at all times by default.
我自己有这个问题,上面的答案有帮助,但我向我的 ViewController 代码添加了一个包装器,如下所示,只需传递 uiview 实例和文本即可更改,包装器函数会打开 Selectable 值,更改文本,然后再次将其关闭。当您需要在默认情况下始终关闭 uitextview 时很有帮助。
/*
We set the text views Selectable value to YES temporarily, change text and turn it off again.
This is a known bug that if the selectable value = NO the view loses its formatting.
*/
-(void)changeTextOfUiTextViewAndKeepFormatting:(UITextView*)viewToUpdate withText:(NSString*)textValue
{
if(![viewToUpdate isSelectable]){
[viewToUpdate setSelectable:YES];
[viewToUpdate setText:textValue];
[viewToUpdate setSelectable:NO];
}else{
[viewToUpdate setText:textValue];
[viewToUpdate setSelectable:NO];
}
}
回答by Jordan Montel
EDIT :
编辑 :
Setting font for UITextView in iOS 7 work for me if firstly you set the text and after that you set the font :
如果首先设置文本然后设置字体,则在 iOS 7 中为 UITextView 设置字体对我有用:
@property (nonatomic, weak) IBOutlet UITextView *masterText;
@implementation myViewController
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
_myTextView.text = @"My Text";
_myTextView.font = [UIFont fontWithName:@"Helvetica.ttf" size:16]; // Set Font
}
On a XIB file, if you add some text in your UITextView and change the font or the color it will work.
在 XIB 文件中,如果您在 UITextView 中添加一些文本并更改字体或颜色,它将起作用。
回答by WaltersGE1
Here's a quick subclass solution I often use for this problem.
这是我经常用于此问题的快速子类解决方案。
class WorkaroundTextView: UITextView {
override var text: String! {
get {
return super.text
}
set {
let originalSelectableValue = self.selectable
self.selectable = true
super.text = newValue
self.selectable = originalSelectableValue
}
}
}
回答by Alessandro Ranaldi
This issue resurfaced in Xcode 8. This is how I fixed it:
这个问题在 Xcode 8 中再次出现。这是我修复它的方法:
Changed the extension to:
将扩展名更改为:
extension UITextView{
func setTextAvoidXcodeIssue(newText : String, selectable: Bool){
isSelectable = true
text = newText
isSelectable = selectable
}
}
and checked the Selectable option in the Interface Builder.
并检查了 Interface Builder 中的 Selectable 选项。
It's not very elegant to have that 'selectable' parameter but it'll do.
拥有那个“可选”参数不是很优雅,但它会做。
回答by Peter Johnson
In iOS 8.3, the workaround of setting "selectable" to YES before the setText, and NO after, didn't fix it for me.
在 iOS 8.3 中,在 setText 之前将“selectable”设置为 YES,之后设置为 NO 的解决方法并没有为我解决。
I found I needed to set "selectable" to YES in the storyboard, too, before this would work.
我发现我也需要在故事板中将“可选”设置为“是”,然后才能工作。
回答by David Green
This worked for me:
这对我有用:
let font = textView.font
textView.attributedText = attributedString
textView.font = font
回答by Mark Bourke
Its been 3 years and the bug still exists in the latest stable version of Xcode (7.3). Clearly apple wont be fixing it any time soon leaving developers with two options: leaving selectable on and setting UserInteractionEnabled to false or Method swizzling.
已经 3 年了,该错误仍然存在于最新的稳定版 Xcode (7.3) 中。很明显,苹果不会很快修复它,给开发人员留下两个选项:保持可选状态和将 UserInteractionEnabled 设置为 false 或 Method swizzling。
If you have a button on your textView the former will not suffice.
如果你的 textView 上有一个按钮,前者是不够的。
No-code-change-requied solution in swift:
无需代码更改的 swift 解决方案:
import UIKit
extension UITextView {
@nonobjc var text: String! {
get {
return performSelector(Selector("text")).takeUnretainedValue() as? String ?? ""
} set {
let originalSelectableValue = selectable
selectable = true
performSelector(Selector("setText:"), withObject: newValue)
selectable = originalSelectableValue
}
}
}
Objective-C:
目标-C:
#import <objc/runtime.h>
#import <UIKit/UIKit.h>
@implementation UITextView (SetTextFix)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
SEL originalSelector = @selector(setText:);
SEL swizzledSelector = @selector(xxx_setText:);
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod =
class_addMethod(class,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
- (void)xxx_setText:(NSString *)text {
BOOL originalSelectableValue = self.selectable;
self.selectable = YES;
[self xxx_setText:text];
self.selectable = originalSelectableValue;
}
@end