Ios 更改UITextView文本方向

Ios 更改UITextView文本方向,ios,uitextview,iphone,uitextviewdelegate,Ios,Uitextview,Iphone,Uitextviewdelegate,默认情况下,iOS不支持我的语言,因此unicode不是一个选项,因此我在UITextView上使用嵌入式true type字体 这在很大程度上是可行的,但我的问题类似于阿拉伯语,希伯来语我的语言是从右到左写的。所以我需要改变文本的方向(不是文本对齐) 我做了一些搜索,他们都在谈论NSLocale之类的东西,但是可以在代码中更改吗?如果我能把它改成阿拉伯语/希伯来语的话,我想会有用的,但是应该用代码来完成,因为我不想改变手机的语言 那么,我的文本输入选项到底是什么呢?任何帮助都将不胜感激 谢谢

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

这在很大程度上是可行的,但我的问题类似于阿拉伯语,希伯来语我的语言是从右到左写的。所以我需要改变文本的方向(不是文本对齐)

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

那么,我的文本输入选项到底是什么呢?任何帮助都将不胜感激


谢谢

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

你的问题让我很感兴趣。所以在一个小时内我想出了解决办法。它远非完美,但您可以使用它并修复剩余的bug。 这是一个带有文本视图的简单视图控制器。您可以添加文本并键入它,也可以使用

-(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
@接口反向ExtVC:UIViewController{
NSMutableArray*_行;
UITextView*\u电视;
}
/**
返回反向文本。
这些行也会更新。此数组包含所有行。如果要追加文本,应再次传递此数组。
*/
+(NSString*)reverseText:(NSString*)带字体的文本:(UIFont*)字体carretPosition:(NSRange*)cpos行:(NSMutableArray*)行边界:(CGRect)边界;
-(void)setText:(NSString*)txt;
@结束
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
@implementation ReverseTextVC
-(id)init{
if(self=[super init]){
_tv=[[UITextView alloc]initWithFrame:CGRectMake(0,0320480)];
_tv.textAlignment=UITextAlignmentRight;
_tv.delegate=self;
[self.view addSubview:_tv];
[电视发布];
_lines=[[NSMutableArray alloc]initWithCapacity:10];
[\u lines addObject:[NSMutableString stringWithCapacity:255]];
}
回归自我;
}
-(无效)解除锁定{
[_线发布];
[super dealoc];
}
-(void)负荷视图{
UIView*v=[[UIView alloc]initWithFrame:CGRectMake(0,20,320,480)];
self.view=v;
[v释放];
}
-(void)setText:(NSString*)txt{
NSString*结果=nil;
NSRange rng;
NSArray*words=[txt组件由字符串分隔:@”“];
//我们应该迭代地做(因为这是最简单的方法=)
for(NSString*字中的字){
word=[NSString stringWithFormat:@“%@”,word];
对于(int i=0;i长度=0;
cpos->location=0;
如果([文本长度]){
如果(![text IsequalString:@“\n”]){
[(NSMutableString*)[lines lastObject]插入字符串:文本
a指数:0];
}否则{
[lines addObject:[NSMutableString stringWithCapacity:255];
}
}否则{
//退格
//待办事项:
NSRange del_rng;
del_rng.length=1;
del_rng.location=0;
if([(NSMutableString*)[lines lastObject]长度]){
[(NSMutableString*)[lines lastObject]deleteCharactersRange:del_rng];
}
if(![(NSMutableString*)[lines lastObject]长度]){
[线条移除对象];
}
}
CGSize sz=[(NSString*)[lines lastObject]sizeWithFont:font];
如果(sz.width>=bounds.size.width-15){
NSMutableArray*单词=[NSMutableArray数组WithArray:[(NSString*)[lines lastObject]组件由字符串分隔:@”“];
NSString*第一个单词=[words objectAtIndex:0];
[words removeObjectAtIndex:0];
[(NSMutableString*)[lines lastObject]setString:[words componentsJoinedByString:@”“];
[lines addObject:[nsmutablestringwithstring:first_word];
}
NSMutableString*txt=[NSMutableString stringWithCapacity:100];
对于(inti=0;iHi
我认为,通过将UIWebView与html和css结合使用,您可以完成这项工作

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

名称:从右到左嵌入 Unicode:202B UTF8:E280抗体

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

以下代码段使用EOL+RTL_嵌入字符串替换行尾字符:

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

只需使用Unicode 202B字符从右向左嵌入即可。

示例:


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

对于我来说,简单地将\u202B添加到字符串中直接添加到.strings文件并没有起到作用(最终结果将打印出“202B”部分)。但是,在调用localizedStringForKey返回字符串后,将\u202B添加到字符串中可以实现此目的

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

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

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

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

func textViewDidChange(_ textView: UITextView) {

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

    print(myTextView.text)


    return

}

我以前在显示阿拉伯语文本时没有遇到过任何问题,我认为希伯来语也是如此,但我不能说文本输入。UIWebView仅用于显示内容。非常有用,我尝试将其与此自定义键盘配合使用:但我将方向恢复为默认方向。您能帮助我如何将代码与此自定义键盘集成吗控件或任何类似的建议,只想获得具有RTL文本方向的自定义键盘字符。谢谢。@Max:我还想实现与hafedh相同的功能。我想实现乌尔都语键盘wi