UITextView style is being reset after setting text

2019-01-08 04:55发布

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?

@interface myViewController : UIViewController
{
  IBOutlet UITextView *_masterText;
}

@implementation myViewController

-(void)viewWillAppear:(BOOL)animated
{
    [_masterText setText:@"New text"];
}

13条回答
霸刀☆藐视天下
2楼-- · 2019-01-08 04:56

Using the work-around discussed in this issue, this extension to UITextView provides a setTextInCurrentStyle() function. Based on solution by Alessandro Ranaldi but does not require the current isSelectable value to be passed to the function.

extension UITextView{
    func setTextInCurrentStyle(_ newText: String) {
        let selectablePreviously = self.isSelectable
        isSelectable = true
        text = newText
        isSelectable = selectablePreviously
    }
}
查看更多
叼着烟拽天下
3楼-- · 2019-01-08 05:03

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
        }
    }
}
查看更多
一夜七次
4楼-- · 2019-01-08 05:08

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.

/*
    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];
    }
}
查看更多
三岁会撩人
5楼-- · 2019-01-08 05:09

I ran into the same issue (on Xcode 6.1) and while John Cogan's answer worked for me, I found that extending the UITextView class with a category was a better solution for my particular project.

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
查看更多
兄弟一词,经得起流年.
6楼-- · 2019-01-08 05:09

In iOS 8.3, the workaround of setting "selectable" to YES before the setText, and NO after, didn't fix it for me.

I found I needed to set "selectable" to YES in the storyboard, too, before this would work.

查看更多
ら.Afraid
7楼-- · 2019-01-08 05:09

This worked for me:

let font = textView.font
textView.attributedText = attributedString
textView.font  = font
查看更多
登录 后发表回答