13

私の言語はデフォルトで iOS でサポートされていないため、Unicode はオプションではないため、UITextView に埋め込まれた True Type フォントを使用しています

それはほとんどの部分で機能しますが、私の問題はアラビア語のようなもので、私の言語は右から左に書かれています。したがって、テキストの方向を変更する必要があります(テキストの配置ではありません)。

私はいくつかの検索を行いましたが、それらはすべて NSLocale などについて話していますが、コードで変更できますか? アラビア語/ヘブライ語などに変更できればうまくいくと思いますが、電話の言語を変更したくないので、コードで行う必要があります。

では、テキスト入力のオプションは実際には何ですか? 任意の助けをいただければ幸いです。

ありがとう。

4

7 に答える 7

9

テキストビューの各行の先頭に「右から左への埋め込み」Unicode文字を配置できます。

名前:右から左への埋め込みUnicode:202B UTF8:E2 80 AB

このキャラクターは見えない(形がない)ことに注意してください。

次のコードは、行末文字をEOL+RTL_EMBEDDING文字列に置き換えます。

appDescription.text = [rtlDescriptionString stringByReplacingOccurrencesOfString:@"\n" withString:@"\n‫;["
于 2011-06-28T10:30:20.763 に答える
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

それがあなたを助けることを願っています=)

于 2011-02-05T12:27:44.393 に答える
4

RIGHT-TO-LEFT EMBEDDING に Unicode 202B 文字を使用するだけです。

例:

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

于 2012-02-07T09:45:36.907 に答える
0

私にとっては、\ u202Bを文字列に直接.stringsファイルに追加するだけでは、うまくいきませんでした(最終的な結果は、文字通り「202B」の部分を印刷します)。ただし、localizedStringForKeyの呼び出しから戻った後、文字列に\ u202Bを追加すると、うまくいきます。

したがって、私の解決策では、任意の文字列「RLE_SYMBOL」を.stringsファイルの文字列に直接挿入し、localizedStringForKeyを呼び出した後、「RLE_SYMBOL」を「\u202B」に置き換えます。

少し間接的で、最高の解決策ではありませんが、迅速な解決策です。

   mystring = [mystring stringByReplacingOccurrencesOfString:@"RLE_SYMBOL" withString:@"\u202B"];
于 2013-01-16T21:07:51.303 に答える
0

こんにちは私はHTMLとCSSでUIWebViewを使用することであなたが仕事をすることができると思います

于 2011-04-06T19:56:51.920 に答える
0

私は同じ問題に直面しています。私の言語はアラビア語に似ており、右から左方向に書きます。私にとって最も簡単な解決策は、テキストの配置を右に変更し、各文字が入力された後にカーソルを左に移動することでした。これはSWIFT 3.0で動作します

func textViewDidChange(_ textView: UITextView) {

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

    print(myTextView.text)


    return

}
于 2017-01-02T09:48:54.860 に答える
-3

Iphoneは、ヘブライ語やその他の右から左へのテキストを正しく表示できません。だから私はあなたがテキストの方向を変えることができる方法はないと思います。

于 2011-02-05T10:56:08.977 に答える