iOS中各种UI控件属性设置示例代码
程序员文章站
2022-06-27 18:25:57
//视图已经加载完了,可以进行ui的添加了- (void)viewdidload { [superviewdidload]; // do any additional setup after load...
//视图已经加载完了,可以进行ui的添加了 - (void)viewdidload { [superviewdidload]; // do any additional setup after loading the view. //初始化uilabel注意指定该对象的位置及大小 uilabel *lb = [[uilabelalloc]initwithframe:cgrectmake(0,20,300,200)]; //设置文字 lb.text =@"label测试我在学习中学些ui story水电费水电费未入围 i肉煨入味哦水电费水电费水电费"; //设置背景色 lb.backgroundcolor = [uicolorcolorwithred:0green:191.0/255.0blue:243.0/255.0alpha:1.0]; //设置文字颜色 lb.textcolor = [uicolorwhitecolor]; //文字大小,文字字体 lb.font = [uifontsystemfontofsize:25]; nslog(@"系统字体名字:%@",lb.font.familyname); //打印文字字体列表 nsarray *arrfonts = [uifontfamilynames]; nslog(@"系统字体列表:%@",arrfonts); //文字对齐 lb.textalignment =nstextalignmentjustified; // nstextalignmentleft = 0, //居左对齐,默认 // 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, //每一行的结尾以字或者一个完整单词换行(若不够一个单词的位置) // nslinebreakbycharwrapping,//在每一行的结尾以字母进行换行 // nslinebreakbyclipping,// simply clip // nslinebreakbytruncatinghead,// truncate at head of line: "...wxyz" // nslinebreakbytruncatingtail,// truncate at tail of line: "abcd..." // nslinebreakbytruncatingmiddle// truncate middle of line: "ab...yz" //指定行数,0为不限制行树,可以指定具体的数字 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的首行缩进一直是个很头疼的问题,现在ios6只有有一个 attributedtext的属性值得我们深究,可以达到我们自定义的行高,还有首行缩进,各种行距和间隔问题。下面这个是两个label, 一个是username,另一个是content文本多行信息
创建标签
@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 = @"首行缩进根据用户昵称自动调整 间隔可自定根据需求随意改变。。。。。。。" ; self . contentlabel . adjustsfontsizetofitwidth = yes ; self . contentlabel . adjustsletterspacingtofitwidth = yes ; [ self resetcontent ]; }
自适应计算间距
- ( 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 ]; //首行缩进 根据用户昵称宽度在加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; //当文字超过视图的边框时是否允许滑动,默认为“yes” textview.editable = yes; //是否允许编辑内容,默认为“yes” textview.delegate = self; //设置代理方法的实现类 textview.font=[uifont fontwithname:@"arial" size:18.0]; //设置字体名字和字体大小; textview.returnkeytype = uireturnkeydefault;//return键的类型 textview.keyboardtype = uikeyboardtypedefault;//键盘类型 textview.textalignment = nstextalignmentleft; //文本显示的位置默认为居左 textview.datadetectortypes = uidatadetectortypeall; //显示数据类型的连接模式(如电话号码、网址、地址等) textview.textcolor = [uicolor blackcolor]; textview.text = @"uitextview详解";//设置显示的文本内容 [self.view addsubview:textview];
uitextview的代理方法如下:
//将要开始编辑 - (bool)textviewshouldbeginediting:(uitextview *)textview; //将要结束编辑 - (bool)textviewshouldendediting:(uitextview *)textview; //开始编辑 - (void)textviewdidbeginediting:(uitextview *)textview; //结束编辑 - (void)textviewdidendediting:(uitextview *)textview; //内容将要发生改变编辑 - (bool)textview:(uitextview *)textview shouldchangetextinrange:(nsrange)range replacementtext:(nsstring*)text; //内容发生改变编辑 - (void)textviewdidchange:(uitextview *)textview; //焦点发生改变 - (void)textviewdidchangeselection:(uitextview *)textview;
有时候我们要控件自适应输入的文本的内容的高度,只要在textviewdidchange的代理方法中加入调整控件大小的代理即可
- (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]; //重新调整textview的高度 textview.frame =cgrectmake(textview.frame.origin.x,textview.frame.origin.y,textview.frame.size.width,sizeframe.height+5); }
控制输入文字的长度和内容,可通调用以下代理方法实现
- (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的软键盘没有自带的退键盘键,所以要实现退出键盘需要自己实现,有如下几种方式:
1)如果你程序是有导航条的,可以在导航条上面加多一个done的按钮,用来退出键盘,当然要先实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里不用回车键,可以把回车键当做退出键盘的响应键。
代码如下:
-(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];//当文本输入框加上topview [topview release]; topview = nil; -(ibaction)dismisskeyboard { [tvtextview resignfirstresponder]; }
总结
到此这篇关于ios中各种ui控件属性设置的文章就介绍到这了,更多相关ios各种ui控件属性设置内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 创意字母标志设计组图欣赏
下一篇: 现代风格企业标志设计作品欣赏