3 回答

TA貢獻1946條經驗 獲得超3個贊
在iOS 7之后,styleString方法不再起作用。
有兩種新的選擇。
首先是TextKit;強大的新版式引擎。要更改行距,請設置UITextView的布局管理器的委托:
textView.layoutManager.delegate = self; // you'll need to declare you implement the NSLayoutManagerDelegate protocol
然后重寫此委托方法:
- (CGFloat)layoutManager:(NSLayoutManager *)layoutManager lineSpacingAfterGlyphAtIndex:(NSUInteger)glyphIndex withProposedLineFragmentRect:(CGRect)rect
{
return 20; // For really wide spacing; pick your own value
}
其次,iOS 7現在支持NSParagraphStyle的lineSpacing。這樣可以提供更多控制,例如第一行縮進和邊界矩形的計算。所以或者...
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.headIndent = 15; // <--- indention if you need it
paragraphStyle.firstLineHeadIndent = 15;
paragraphStyle.lineSpacing = 7; // <--- magic line spacing here!
NSDictionary *attrsDictionary =
@{ NSParagraphStyleAttributeName: paragraphStyle }; // <-- there are many more attrs, e.g NSFontAttributeName
self.textView.attributedText = [[NSAttributedString alloc] initWithString:@"Hello World over many lines!" attributes:attrsDictionary];
FWIW,在iOS7下也沒有使用舊的contentInset方法來沿UITextView的左邊緣對齊文本。相反,要刪除邊距:
textView.textContainer.lineFragmentPadding = 0;

TA貢獻1772條經驗 獲得超8個贊
僅當您在UITextView上定義了定義styleString的類別時,才可以使用styleString的UITextView子類重寫,否則會出現編譯錯誤。例如,在您的UITextView子類中:
#import "SomeDangTextView.h"
@interface UITextView ()
- (id)styleString;
@end
@implementation SomeDangTextView
- (id)styleString {
return [[super styleString] stringByAppendingString:@"; line-height: 1.5em"];
}
@end
- 3 回答
- 0 關注
- 1425 瀏覽
添加回答
舉報