更改UITextView文本方向

时间:2011-02-05 06:29:41

标签: ios uitextview iphone uitextviewdelegate

默认情况下iOS不支持我的语言,因此unicode不是一个选项,因此我在UITextView上使用嵌入式真实字体

它在很大程度上起作用,但我的问题就像阿拉伯语,希伯来语我的语言是从右到左书写的。所以我需要改变文本的方向(Not Text Alignment)。

我做了一些搜索,他们都谈论NSLocale和东西,但可以在代码中更改吗?如果我能把它改成像阿拉伯语/希伯来语这样的东西我会猜测,但它应该在代码中完成,因为我不想改变手机的语言。

那么我的文本输入选项究竟是什么?任何帮助将不胜感激。

感谢。

7 个答案:

答案 0 :(得分:9)

您可以在文本视图中的每一行的开头放置一个“从右向左嵌入”的Unicode字符。

名称:右向嵌入式 Unicode:202B UTF8:E2 80 AB

请注意,此字符不可见(无形状)。

以下代码用EOL + RTL_EMBEDDING字符串替换行尾字符:

appDescription.text = [rtlDescriptionString stringByReplacingOccurrencesOfString:@"\n" withString:@"\n‫;["

答案 1 :(得分:7)

你的问题真的让我感兴趣。所以在几个小时内我就找到了解决方案。它远非完美,但您可以使用它并修复剩余的错误。 这是一个带文本视图的简单视图控制器。您可以添加输入文本的文本,也可以使用

设置值
-(void) setText:(NSString*) txt;

所以这是代码:

ReverseTextVC.h

@interface ReverseTextVC : UIViewController <UITextViewDelegate> {
    NSMutableArray* _lines;
    UITextView* _tv;
}

/**
 Returns reversed text.
 The lines is also updated. This array contains all lines. You should pass this array again if you want to append the text.
 */
+(NSString*) reverseText:(NSString*) text withFont:(UIFont*) font carretPosition:(NSRange*) cpos Lines:(NSMutableArray*) lines Bounds:(CGRect) bounds;

-(void) setText:(NSString*) txt;

@end

ReverseTextVC.m

@implementation ReverseTextVC

-(id) init {
    if( self = [super init] ) {
        _tv = [[UITextView alloc] initWithFrame: CGRectMake(0, 0, 320, 480)];
        _tv.textAlignment = UITextAlignmentRight;
        _tv.delegate = self;
        [self.view addSubview: _tv];
        [_tv release];

        _lines = [[NSMutableArray alloc] initWithCapacity: 10];
        [_lines addObject: [NSMutableString stringWithCapacity: 255]];
    }

    return self;
}

-(void) dealloc {
    [_lines release];

    [super dealloc];
}

-(void) loadView {
    UIView* v = [[UIView alloc] initWithFrame: CGRectMake(0, 20, 320, 480)];
    self.view = v;
    [v release];
}

-(void) setText:(NSString*) txt {
    NSString* result = nil;
    NSRange rng;
    NSArray* words = [txt componentsSeparatedByString: @" "];
    //we should do it iteratively (cause it's the simplest way =) )
    for(NSString* word in words) {
        word = [NSString stringWithFormat: @"%@ ", word];
        for(int i=0; i<[word length]; ++i) {
            NSRange r; r.length = 1; r.location = i;
            result = [ReverseTextVC reverseText: [word substringWithRange: r]
                                                                withFont: _tv.font
                                                      carretPosition: &rng 
                                                                Lines: _lines
                                                              Bounds: _tv.bounds];
        }
    }

    _tv.text = result;
}


#pragma mark -
#pragma mark UITextViewDelegate

-(BOOL) textView:(UITextView*) textView shouldChangeTextInRange:(NSRange) range replacementText:(NSString*) text {
    NSRange rng;

    textView.text = [ReverseTextVC reverseText: text
                                      withFont: textView.font
                                carretPosition: &rng 
                                         Lines: _lines
                                        Bounds: textView.bounds];

    textView.selectedRange = rng;

    return NO;
}


#pragma mark -
#pragma mark Static

+(NSString*) reverseText:(NSString*) text withFont:(UIFont*) font carretPosition:(NSRange*) cpos Lines:(NSMutableArray*) lines Bounds:(CGRect) bounds {
    cpos->length = 0;
    cpos->location = 0;
    if( [text length] ) {
        if( ![text isEqualToString: @"\n"] ) {
            [(NSMutableString*)[lines lastObject] insertString: text
                                                        atIndex: 0];
        } else {
            [lines addObject: [NSMutableString stringWithCapacity: 255]];
        }
    } else {
        //backspace
        //TODO:
        NSRange del_rng;
        del_rng.length = 1;
        del_rng.location = 0;
        if( [(NSMutableString*)[lines lastObject] length] ) {
            [(NSMutableString*)[lines lastObject] deleteCharactersInRange: del_rng];
        }
        if( ![(NSMutableString*)[lines lastObject] length] ) {
            [lines removeLastObject];
        }
    }

    CGSize sz = [(NSString*)[lines lastObject] sizeWithFont: font];
    if( sz.width >= bounds.size.width-15 ) {
        NSMutableArray* words = [NSMutableArray arrayWithArray: [(NSString*)[lines lastObject] componentsSeparatedByString: @" "]];
        NSString* first_word = [words objectAtIndex: 0];
        [words removeObjectAtIndex: 0];
        [(NSMutableString*)[lines lastObject] setString: [words componentsJoinedByString: @" "]];
        [lines addObject: [NSMutableString stringWithString: first_word]];
    }

    NSMutableString* txt = [NSMutableString stringWithCapacity: 100];
    for(int i=0; i<[lines count]; ++i) {
        NSString* line = [lines objectAtIndex: i];
        if( i<([lines count]-1) ) {
            [txt appendFormat: @"%@\n", line];
            cpos->location += [line length]+1;
        } else {
            [txt appendFormat: @"%@", line];
        }
    }

    return txt;
}

@end

希望它可以帮助你=)

答案 2 :(得分:4)

只需使用Unicode 202B字符进行RIGHT-TO-LEFT EMBEDDING。

示例:

    uiTextView.text = [NSString stringWithFormat:@“\ u202B%@”,textString];

答案 3 :(得分:0)

您好 我认为通过使用带有html和css的UIWebView,你可以完成这项工作

答案 4 :(得分:0)

对于我来说,简单地将\ u202B直接添加到.strings文件中的字符串中没有做到这一点(最终结果将打印出字面打印出“202B”部分)。但是,在从对localizedStringForKey的调用返回之后将\ u202B添加到字符串中确实可以解决问题。

因此,我的解决方案,我将任意字符串“RLE_SYMBOL”直接插入.strings文件中的字符串,然后在调用localizedStringForKey后,我将“RLE_SYMBOL”替换为“\ u202B”。

有点间接,不是最好的解决方案,而是快速解决方案。

   mystring = [mystring stringByReplacingOccurrencesOfString:@"RLE_SYMBOL" withString:@"\u202B"];

答案 5 :(得分:0)

我一直面临同样的问题。我的语言类似阿拉伯语,从右到左书写。对我来说最简单的解决方案是将文本对齐方式更改为右侧,并在输入每个字符后将光标向左移动。这适用于SWIFT 3.0

func textViewDidChange(_ textView: UITextView) {

    let newPosition = myTextView.beginningOfDocument
    myTextView.selectedTextRange = myTextView.textRange(from: newPosition, to: newPosition)

    print(myTextView.text)


    return

}

答案 6 :(得分:-3)

Iphone无法正确显示希伯来语和其他从右到左的文本。所以我认为你无法改变文本的方向。

相关问题