亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在UITextView中設置行高

在UITextView中設置行高

呼喚遠方 2019-12-13 09:03:59
我已經很確定不能使用任何公共API來完成此操作,但是我仍然想問:有什么辦法可以改變UITextView中的行高嗎?足以靜態地完成它,而無需在運行時進行更改。問題是默認的行高太小。文本看起來極度壓縮,并且是嘗試編寫較長文本時的噩夢。謝謝,馬克斯編輯:我知道有UIWebView而且很好,可以做樣式等。但是它是不可編輯的。我需要具有可接受的行高的可編輯文本組件。Omni Frameworks提供的功能也無濟于事,因為它太慢且感覺不正確...
查看完整描述

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;


查看完整回答
反對 回復 2019-12-13
?
料青山看我應如是

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


查看完整回答
反對 回復 2019-12-13
  • 3 回答
  • 0 關注
  • 1425 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號