UITextView不在此处插入换行符

时间:2021-09-11 02:59:55

I have UITextView. It contains some math formulas. For example, "In triangle ABC side AB = 5". Sometimes UITextView breaks line after "=". It becomes like this

我有UITextView。它包含一些数学公式。例如,“在三角形ABC侧AB = 5”。有时UITextView会在“=”之后断行。它变得像这样

In triangle ABC side AB =

在三角形ABC侧AB =

5

But I want "AB = 5" to be together always:

但我希望“AB = 5”总是在一起:

In triangle ABC side

在三角形ABC方面

AB = 5

AB = 5

How can I do it? I don't want delete spaces like "AB=5". Thank you.

我该怎么做?我不想删除像“AB = 5”这样的空格。谢谢。

1 个解决方案

#1


If I understood your question right, you are looking for \n a line break:

如果我理解你的问题,你正在寻找\ n换行符:

NSLog(@"In triangle ABC side\nAB = 5")

Ok since I understood your intention better, maybe this can be a solution or a start for your solution:

好了,因为我更了解你的意图,也许这可以成为解决方案或解决方案的开始:

- (NSString *) makeStringNonWrappable : (NSString *) string
{
    NSMutableString *myString = [NSMutableString stringWithString:string];
    NSRange rangeValue = [myString rangeOfString:@" = " options:NSCaseInsensitiveSearch];
    if (rangeValue.length > 0){
        NSLog(@"string contains  =  %d %d", rangeValue.location, rangeValue.length);
        NSString *nonbreakingspace = @"\u00A0";
        NSString * replacementString = [NSString stringWithFormat:@"%@=%@", nonbreakingspace, nonbreakingspace];
        [myString replaceCharactersInRange:rangeValue withString:replacementString];

        NSLog(@"%@", myString);
        return myString;
    }
    else {
        NSLog(@"string does not contain = !");
        return string;
    }
}

Basically, I find the = occurance in your String and replace it with a string which looks exactly same but is processed different by the UITextView. the spaces are not processed as spaces anymore. Note, i didnt compile the code there might be some typos..

基本上,我在你的String中找到= occurrence,并用一个看起来完全相同但由UITextView处理不同的字符串替换它。空格不再作为空格处理。注意,我没有编译代码可能有一些错别字..

#1


If I understood your question right, you are looking for \n a line break:

如果我理解你的问题,你正在寻找\ n换行符:

NSLog(@"In triangle ABC side\nAB = 5")

Ok since I understood your intention better, maybe this can be a solution or a start for your solution:

好了,因为我更了解你的意图,也许这可以成为解决方案或解决方案的开始:

- (NSString *) makeStringNonWrappable : (NSString *) string
{
    NSMutableString *myString = [NSMutableString stringWithString:string];
    NSRange rangeValue = [myString rangeOfString:@" = " options:NSCaseInsensitiveSearch];
    if (rangeValue.length > 0){
        NSLog(@"string contains  =  %d %d", rangeValue.location, rangeValue.length);
        NSString *nonbreakingspace = @"\u00A0";
        NSString * replacementString = [NSString stringWithFormat:@"%@=%@", nonbreakingspace, nonbreakingspace];
        [myString replaceCharactersInRange:rangeValue withString:replacementString];

        NSLog(@"%@", myString);
        return myString;
    }
    else {
        NSLog(@"string does not contain = !");
        return string;
    }
}

Basically, I find the = occurance in your String and replace it with a string which looks exactly same but is processed different by the UITextView. the spaces are not processed as spaces anymore. Note, i didnt compile the code there might be some typos..

基本上,我在你的String中找到= occurrence,并用一个看起来完全相同但由UITextView处理不同的字符串替换它。空格不再作为空格处理。注意,我没有编译代码可能有一些错别字..