1. 程式人生 > >textView 彈出鍵盤上面新增完成按鈕,並設定輸入內容的格式。

textView 彈出鍵盤上面新增完成按鈕,並設定輸入內容的格式。

- (void)setContentView{

self.contentTextView = [[UITextView alloc]initWithFrame:CGRectMake(1170ScreenWidth-22ScreenHeight-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 allocinit];

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,唯一支援的值分別為01

    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(00ScreenWidth35)];

    topView.backgroundColor = [UIColor whiteColor];

    //設定style

    [topView setBarStyle:UIBarStyleDefault];

//定義兩個flexibleSpacebutton,放在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];

}