亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁/技術(shù)文章
文章詳情頁

iOS中各種UI控件屬性設(shè)置示例代碼

瀏覽:2日期:2022-09-17 09:07:32

//視圖已經(jīng)加載完了,可以進行ui的添加了- (void)viewDidLoad { [superviewDidLoad]; // Do any additional setup after loading the view. //初始化UILabel注意指定該對象的位置及大小 UILabel *lb = [[UILabelalloc]initWithFrame:CGRectMake(0,20,300,200)]; //設(shè)置文字 lb.text =@'label測試我在學(xué)習(xí)中學(xué)些ui story水電費水電費未入圍 i肉煨入味哦水電費水電費水電費'; //設(shè)置背景色 lb.backgroundColor = [UIColorcolorWithRed:0green:191.0/255.0blue:243.0/255.0alpha:1.0]; //設(shè)置文字顏色 lb.textColor = [UIColorwhiteColor]; //文字大小,文字字體 lb.font = [UIFontsystemFontOfSize:25]; NSLog(@'系統(tǒng)字體名字:%@',lb.font.familyName); //打印文字字體列表 NSArray *arrFonts = [UIFontfamilyNames]; NSLog(@'系統(tǒng)字體列表:%@',arrFonts); //文字對齊 lb.textAlignment =NSTextAlignmentJustified;// NSTextAlignmentLeft = 0, //居左對齊,默認(rèn)// NSTextAlignmentCenter = 1, //居中對齊// NSTextAlignmentRight = 2, //居右對齊// NSTextAlignmentJustified = 3, // Fully-justified. The last line in a paragraph is natural-aligned.// NSTextAlignmentNatural = 4, // Indicates the default alignment for script //換行模式 lb.lineBreakMode =NSLineBreakByCharWrapping;// NSLineBreakByWordWrapping = 0, //每一行的結(jié)尾以字或者一個完整單詞換行(若不夠一個單詞的位置)// NSLineBreakByCharWrapping,//在每一行的結(jié)尾以字母進行換行// NSLineBreakByClipping,// Simply clip// NSLineBreakByTruncatingHead,// Truncate at head of line: '...wxyz'// NSLineBreakByTruncatingTail,// Truncate at tail of line: 'abcd...'// NSLineBreakByTruncatingMiddle// Truncate middle of line: 'ab...yz' //指定行數(shù),0為不限制行樹,可以指定具體的數(shù)字 lb.numberOfLines =0; //加圓角 lb.layer.cornerRadius =30; //此行必須加,將原來的矩形角剪掉 lb.clipsToBounds =YES; //加邊框顏色,寬度,注意給layer加的顏色是CGColor類型 lb.layer.borderColor = [[UIColorredColor]CGColor]; lb.layer.borderWidth =1.0; //把label添加到視圖上,并且會顯示 [self.viewaddSubview:lb];}

Label的首行縮進一直是個很頭疼的問題,現(xiàn)在IOS6只有有一個 attributedText的屬性值得我們深究,可以達(dá)到我們自定義的行高,還有首行縮進,各種行距和間隔問題。下面這個是兩個Label, 一個是UserName,另一個是Content文本多行信息

創(chuàng)建標(biāo)簽

@interface ViewController : UIViewController@property ( weak , nonatomic ) IBOutlet UILabel *usernameLabel@property ( weak , nonatomic ) IBOutlet UILabel *contentLabel;@end

視圖展示層

- ( void )viewDidLoad {self . usernameLabel . text = @'用戶名Jordan CZ: ' ;self . usernameLabel . adjustsFontSizeToFitWidth = YES ;[ self . usernameLabel sizeToFit ]; self . contentLabel . text = @'首行縮進根據(jù)用戶昵稱自動調(diào)整 間隔可自定根據(jù)需求隨意改變。。。。。。。' ;self . contentLabel . adjustsFontSizeToFitWidth = YES ;self . contentLabel . adjustsLetterSpacingToFitWidth = YES ;[ self resetContent ];}

自適應(yīng)計算間距

- ( void )resetContent{NSMutableAttributedString *attributedString = [[ NSMutableAttributedString alloc ]initWithString : self . contentLabel . text ];NSMutableParagraphStyle *paragraphStyle = [[ NSMutableParagraphStyle alloc ]init ];paragraphStyle. alignment = NSTextAlignmentLeft ;paragraphStyle. maximumLineHeight = 60 ; //最大的行高 paragraphStyle. lineSpacing = 5 ; //行自定義行高度[paragraphStyle setFirstLineHeadIndent : self . usernameLabel . frame . size .width + 5 ]; //首行縮進 根據(jù)用戶昵稱寬度在加5個像素[attributedString addAttribute : NSParagraphStyleAttributeName value:paragraphStyle range : NSMakeRange ( 0 , [ self . contentLabel . text length ])];self . contentLabel . attributedText = attributedString;[ self . contentLabel sizeToFit ];}

UITextView的使用詳解

//初始化并定義大小 UITextView *textview = [[UITextView alloc] initWithFrame:CGRectMake(20, 10, 280, 30)]; textview.backgroundColor=[UIColor whiteColor]; //背景色 textview.scrollEnabled = NO; //當(dāng)文字超過視圖的邊框時是否允許滑動,默認(rèn)為“YES” textview.editable = YES; //是否允許編輯內(nèi)容,默認(rèn)為“YES” textview.delegate = self; //設(shè)置代理方法的實現(xiàn)類 textview.font=[UIFont fontWithName:@'Arial' size:18.0]; //設(shè)置字體名字和字體大小; textview.returnKeyType = UIReturnKeyDefault;//return鍵的類型 textview.keyboardType = UIKeyboardTypeDefault;//鍵盤類型 textview.textAlignment = NSTextAlignmentLeft; //文本顯示的位置默認(rèn)為居左 textview.dataDetectorTypes = UIDataDetectorTypeAll; //顯示數(shù)據(jù)類型的連接模式(如電話號碼、網(wǎng)址、地址等) textview.textColor = [UIColor blackColor]; textview.text = @'UITextView詳解';//設(shè)置顯示的文本內(nèi)容 [self.view addSubview:textview];

UITextView的代理方法如下:

//將要開始編輯- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;//將要結(jié)束編輯- (BOOL)textViewShouldEndEditing:(UITextView *)textView;//開始編輯- (void)textViewDidBeginEditing:(UITextView *)textView;//結(jié)束編輯- (void)textViewDidEndEditing:(UITextView *)textView;//內(nèi)容將要發(fā)生改變編輯- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text;//內(nèi)容發(fā)生改變編輯- (void)textViewDidChange:(UITextView *)textView;//焦點發(fā)生改變- (void)textViewDidChangeSelection:(UITextView *)textView;

有時候我們要控件自適應(yīng)輸入的文本的內(nèi)容的高度,只要在textViewDidChange的代理方法中加入調(diào)整控件大小的代理即可

- (void)textViewDidChange:(UITextView *)textView{ //計算文本的高度 CGSize constraintSize; constraintSize.width = textView.frame.size.width-16; constraintSize.height = MAXFLOAT; CGSize sizeFrame =[textView.text sizeWithFont:textView.font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; //重新調(diào)整textView的高度 textView.frame =CGRectMake(textView.frame.origin.x,textView.frame.origin.y,textView.frame.size.width,sizeFrame.height+5);}

控制輸入文字的長度和內(nèi)容,可通調(diào)用以下代理方法實現(xiàn)

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text{ if (range.location>=100) { //控制輸入文本的長度 return NO; } if ([text isEqualToString:@'n']) { //禁止輸入換行 return NO; } else { return YES; }}

UITextView退出鍵盤的幾種方式

因為iphone的軟鍵盤沒有自帶的退鍵盤鍵,所以要實現(xiàn)退出鍵盤需要自己實現(xiàn),有如下幾種方式:

1)如果你程序是有導(dǎo)航條的,可以在導(dǎo)航條上面加多一個Done的按鈕,用來退出鍵盤,當(dāng)然要先實UITextViewDelegate。

- (void)textViewDidBeginEditing:(UITextView *)textView { UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissKeyBoard)]; self.navigationItem.rightBarButtonItem = done; [done release]; done = nil;}- (void)textViewDidEndEditing:(UITextView *)textView { self.navigationItem.rightBarButtonItem = nil; }- (void)dismissKeyBoard { [self.textView resignFirstResponder]; }

2)如果你的textview里不用回車鍵,可以把回車鍵當(dāng)做退出鍵盤的響應(yīng)鍵。

代碼如下:

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text{ if ([text isEqualToString:@'n']) { [textView resignFirstResponder]; return NO; } return YES;}

3)還有你也可以自定義其他加載鍵盤上面用來退出,比如在彈出的鍵盤上面加一個view來放置退出鍵盤的Done按鈕。代碼如下:

UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320,30)]; [topView setBarStyle:UIBarStyleBlack]; UIBarButtonItem *btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:@'Done' style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)]; NSArray * buttonsArray = @[btnSpace, doneButton];; [doneButton release]; [btnSpace release]; [topView setItems:buttonsArray]; [textView setInputAccessoryView:topView];//當(dāng)文本輸入框加上topView [topView release]; topView = nil;-(IBAction)dismissKeyBoard{ [tvTextView resignFirstResponder];}

總結(jié)

到此這篇關(guān)于iOS中各種UI控件屬性設(shè)置的文章就介紹到這了,更多相關(guān)iOS各種UI控件屬性設(shè)置內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: IOS
相關(guān)文章:
主站蜘蛛池模板: 亚洲夂夂婷婷色拍ww47 | 爽爽影院色黄网站在线观看 | 日韩一区二区三区四区五区 | 国产欧美亚洲精品第一区 | 亚洲国产精品久久精品怡红院 | 三级网站免费 | 女毛片| 国内自拍 在线播放 网红 | 国产限制级在线观看 | 欧美日产欧美日产精品 | 国产成人亚洲综合一区 | 国产主播久久 | 成人午夜国产福到在线 | 精品欧美一区视频在线观看 | 最新精品在线视频 | 国产精品制服 | 国产大伊香蕉精品视频 | 日韩高清性爽一级毛片免费 | 国产亚洲欧洲一区二区三区 | 久久国产亚洲高清观看5388 | 久久亚洲国产成人精品性色 | 欧美色欧美亚洲高清在线观看 | 欧美一区二区在线播放 | 成人开心激情 | 超h福利视频在线观看 | 91日本在线精品高清观看 | 国产综合久久一区二区三区 | 青木玲中文字幕一区二区 | 精品亚洲一区二区在线播放 | 在线看av的网址 | 精品久久香蕉国产线看观看亚洲 | 九九99九九视频在线观看 | 一级女性全黄生活片看看 | 久久91亚洲精品久久91综合 | 日韩中文字幕久久久经典网 | 亚洲第5页 | 国产精品久久久天天影视香蕉 | 97视频免费播放观看在线视频 | 欧美精品久久久久久久免费观看 | 久久无码精品一区二区三区 | 成年日韩免费大片黄在线观看 |