你的問題真的讓我感興趣。所以在一小時內我就想出瞭解決方案。它遠非完美,但您可以使用它並修復其餘的錯誤。 這是一個帶有文本視圖的簡單視圖控制器。您可以添加文字輸入,或者你可以設置使用
-(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-05 12:27:44
Max
我還希望實現與您的功能相同的功能。我想實現**烏爾都語鍵盤**,文本方向從**右到左**。所以,你有沒有找到任何方法來獲取RTL文本方向的自定義鍵盤字符(正如你在接受答案下的評論中提到的那樣)?如果**是**,請在此分享代碼。我也陷入了像你這樣的狀況。請幫忙。 – Bhavin 2013-09-19 05:41:30
[請按照此鏈接在SWIFT上解決此問題](http://stackoverflow.com/questions/4905500/change-the-uitextview-text-direction/41424462#41424462) – 2017-01-02 09:53:10