textView 彈出鍵盤上面新增完成按鈕,並設定輸入內容的格式。
- (void)setContentView{
self.contentTextView = [[UITextView alloc]initWithFrame:CGRectMake(11, 70, ScreenWidth-22, ScreenHeight-70)];
self.contentTextView.backgroundColor = [UIColor whiteColor];
self.contentTextView.delegate = self;
self.contentTextView.font = FONT_LB(15.0);
// self.contentTextView.text = self.item.content;
[self.view addSubview:self.contentTextView];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 6;// 字型的行間距
paragraphStyle.alignment = NSTextAlignmentJustified;// 左右對齊
paragraphStyle.firstLineHeadIndent = 15.0f;//首行縮排
paragraphStyle.paragraphSpacingBefore = 1.0f;//段首行空白空間/
paragraphStyle.alignment = NSTextAlignmentJustified;//(兩端對齊的)文字對齊方式:(左,中,右,兩端對齊,自然)
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;//結尾部分的內容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz")
paragraphStyle.headIndent = 20;//整體縮排(首行除外)
paragraphStyle.tailIndent = 20;//
paragraphStyle.minimumLineHeight = 10;//最低行高
paragraphStyle.maximumLineHeight = 20;//最大行高
paragraphStyle.paragraphSpacing = 15;//段與段之間的間距
paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;//從左到右的書寫方向(一共➡️三種)
paragraphStyle.lineHeightMultiple = 15;/* Natural line height is multiplied by this factor (if positive) before being constrained by minimum and maximum line height. */
paragraphStyle.hyphenationFactor = 1;//連字屬性 在iOS,唯一支援的值分別為0和1
NSDictionary *attributes = @{
NSFontAttributeName:[UIFont systemFontOfSize:15],
NSForegroundColorAttributeName:LableTextColor333,
NSParagraphStyleAttributeName:paragraphStyle
};
self.contentTextView.attributedText = [[NSAttributedString alloc] initWithString:self.item.content attributes:attributes];
//定義一個toolBar
UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 35)];
topView.backgroundColor = [UIColor whiteColor];
//設定style
[topView setBarStyle:UIBarStyleDefault];
//定義兩個flexibleSpace的button,放在toolBar上,這樣完成按鈕就會在最右邊
UIBarButtonItem * button1 =[[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem * button2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil];
//定義完成按鈕
UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:selfaction:@selector(resignKeyboard)];
//在toolBar上加上這些按鈕
NSArray * buttonsArray = [NSArray arrayWithObjects:button1,button2,doneButton,nil];
[topView setItems:buttonsArray];
[self.contentTextView setInputAccessoryView:topView];
}