Sag*_*ari 24 iphone uitextview
在我的应用程序中,我有UITextView很多行,但我需要具有适当外观的数据.
是否可以进行对齐对齐UITextView?
fen*_*ode 70
我知道这是一个老问题,但现在我们有关于的消息:
UITextView *textView = // init your text view
textView.textAlignment = NSTextAlignmentJustified;
Run Code Online (Sandbox Code Playgroud)
就是这样!
更新:这仅适用于iOS 6及更高版本
Rog*_*ger 12
我找到了一个有效的解决方案.首先,您需要更改UITextView并使用UIWebView替代.
Details.h
@interface Details : UIViewController {
IBOutlet UIWebView *descripcion;
}
@property (nonatomic, retain) UIWebView *descripcion;
Run Code Online (Sandbox Code Playgroud)
然后,加载您的UIWebView如下:
Details.m
[descripcion loadHTMLString:[NSString stringWithFormat:@"<div align='justify'>%@<div>",YOUR_TEXT] baseURL:nil];
Run Code Online (Sandbox Code Playgroud)
小智 10
有解决方案:
NSString *text1 = @"Sample text : A his is my weblog in English, German and Korean. If you want to know more about a pizza stone once it’s cooled down.";
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setAlignment: kCTJustifiedTextAlignment];
NSAttributedString * subText1 = [[NSAttributedString alloc] initWithString:text1 attributes:@{
NSParagraphStyleAttributeName : style
}];
_myUiTextView.attributedText = subText1;
Run Code Online (Sandbox Code Playgroud)
还有另一种选择.CATextLayer.
它明确支持合理的对齐.它的重量轻,速度快UIWebView.因为它本质上是一层薄层CoreText,所以不需要像布局那样繁重的布局UIWebView.
或者您可以使用另一个选项,直接使用较低级别的CoreText框架.
无论如何,他们不支持文本编辑.如果您需要它,您必须自己实现它.
对于从右到左的语言,如波斯语
UITextPosition *beginning = textview.beginningOfDocument;
UITextPosition *start = [textview positionFromPosition:beginning offset:0];
UITextPosition *end = [textview positionFromPosition:start offset:[textview.text length]];
UITextRange *textRange = [textview textRangeFromPosition:start toPosition:end];
[textview setBaseWritingDirection:UITextWritingDirectionRightToLeft forRange:textRange];
textview.textAlignment = NSTextAlignmentJustified;
Run Code Online (Sandbox Code Playgroud)