1.创建并初始化
创建uitextview的文件,并在.h文件中写入如下代码:
#import <uikit/uikit.h>
@interface textviewcontroller : uiviewcontroller <uitextviewdelegate>
{
uitextview *textview;
}
@property (nonatomic, retain) uitextview *textview;
@end
在.m文件中初始化这个textview,写入代码如下:
self.textview = [[[uitextview alloc] initwithframe:self.view.frame]autorelease]; //初始化大小并自动释放
self.textview.textcolor = [uicolor blackcolor];//设置textview里面的字体颜色
self.textview.font = [uifont fontwithname:@"arial" size:18.0];//设置字体名字和字体大小
self.textview.delegate = self;//设置它的委托方法
self.textview.backgroundcolor = [uicolor whitecolor];//设置它的背景颜色
self.textview.text = @"now is the time for all good developers tocome to serve their country.\n\nnow is the time for all good developers to cometo serve their country.";//设置它显示的内容
self.textview.returnkeytype = uireturnkeydefault;//返回键的类型
self.textview.keyboardtype = uikeyboardtypedefault;//键盘类型
self.textview.scrollenabled = yes;//是否可以拖动
self.textview.autoresizingmask = uiviewautoresizingflexibleheight;//自适应高度
[self.view addsubview: self.textview];//加入到整个页面中
2. uitextview退出键盘的几种方式
因为你点击uitextview会出现键盘,如果你退出键盘,有如下几种方式:
(1)如果你程序是有导航条的,可以在导航条上面加多一个done的按钮,用来退出键盘,当然要先实uitextviewdelegate。代码如下:
- (void)textviewdidbeginediting:(uitextview *)textview {
uibarbuttonitem *done = [[[uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemdone target:self action:@selector(leaveeditmode)] autorelease];
self.navigationitem.rightbarbuttonitem = done;
}
- (void)textviewdidendediting:(uitextview *)textview {
self.navigationitem.rightbarbuttonitem = nil;
}
- (void)leaveeditmode {
[self.textview resignfirstresponder];
}
(2)如果你的textview里不用回车键,可以把回车键当做退出键盘的响应键。代码如下:
#pragma mark - uitextview delegate methods
-(bool)textview:(uitextview *)textview shouldchangetextinrange:(nsrange)range replacementtext:(nsstring *)text
{
if ([text isequaltostring:@"\n"]) {
[textview resignfirstresponder];
return no;
}
return yes;
}
这样无论你是使用电脑键盘上的回车键还是使用弹出键盘里的return键都可以达到退出键盘的效果。
(3)还有你也可以自定义其他加载键盘上面用来退出,比如在弹出的键盘上面加一个view来放置退出键盘的done按钮。
代码如下:
uitoolbar * topview = [[uitoolbar alloc]initwithframe:cgrectmake(0, 0, 320, 30)];
[topview setbarstyle:uibarstyleblack];
uibarbuttonitem * hellobutton = [[uibarbuttonitem alloc]initwithtitle:@"hello" style:uibarbuttonitemstylebordered target:self action:nil];
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 = [nsarray arraywithobjects:hellobutton,btnspace,donebutton,nil];
[donebutton release];
[btnspace release];
[hellobutton release];
[topview setitems:buttonsarray];
[tvtextview setinputaccessoryview:topview];
-(ibaction)dismisskeyboard
{
[tvtextview resignfirstresponder];
}
(4)设置uitextview圆角问题
做法是在 #import quartzcore/quartzcore.h 后,便能調用[textview.layer setcornerradius:10]; 來把 uitextview 设定圓角
(5)uitextview根据文本大小自适应高度
通过实现文本字数来确定高度,如下:
nsstring * desc = @"description it is a test font, and don't become angry for which i use to do here.now here is a very nice party from american or not!";
cgsize size = [desc sizewithfont:[uifont systemfontofsize:14] constrainedtosize:cgsizemake(240, 2000) linebreakmode:uilinebreakmodewordwrap];
只有uilabel需要定义的numberoflines为0,即不做行数的限制。如下:
[label setnumberoflines:0];
[label setframe:cgrectmake(40, 135, 240, size.height+10)];
[label settext:desc];
(6)uitextview自定选择文字后的菜单
在viewdidload中加入:
uimenuitem *menuitem = [[uimenuitem alloc]initwithtitle:@"分享到新浪微博" action:@selector(changecolor:)];
uimenucontroller *menu = [uimenucontroller sharedmenucontroller];
[menu setmenuitems:[nsarray arraywithobject:menuitem]];
[menuitem release];
当然上面那个@selector里面的changecolor方法还是自己写吧,也就是说点击了我们自定义的菜单项后会触发的方法。
然后还得在代码里加上一个方法:
-(bool)canperformaction:(sel)action withsender:(id)sender
{
if(action ==@selector(changecolor:))
{
if(textview.selectedrange.length>0)
return yes;
}
return no;
}
实现后如下图:
3.一些个性化定制
总体来说个性化定制uitextview中的内容有两种方法:
(1)从文件中读取内容到uitextview,这个个人感觉使用rtfd和rtf格式文件效果非常好。
(2)使用nsattributestring进行定制
具体方法如下:
nsmutableparagraphstyle *paragraphstyle = [[nsmutableparagraphstyle alloc]init];
paragraphstyle.lineheightmultiple = 20.f;
paragraphstyle.maximumlineheight = 25.f;
paragraphstyle.minimumlineheight = 15.f;
paragraphstyle.firstlineheadindent = 20.f;
paragraphstyle.alignment = nstextalignmentjustified;
nsdictionary *attributes = @{ nsfontattributename:[uifont systemfontofsize:14], nsparagraphstyleattributename:paragraphstyle, nsforegroundcolorattributename:[uicolor colorwithred:76./255. green:75./255. blue:71./255. alpha:1]
};
textview.attributedtext = [[nsattributedstring alloc]initwithstring:content attributes:attributes];
当然也可以初始化一个nsmutableattributedstring,然后向里面添加文字样式,最后将它赋给textview的attributedtext即可
nsmutableattributedstring *atr = [[nsmutableattributedstring alloc]initwithstring:detail];
[atr addattribute:nsfontattributename value:[uifont systemfontofsize:14] range:nsmakerange(0, detail.length)];
textview.attributedtext = atr;
另外,对于textview中的链接样式,同样也可以定制
nsdictionary *linkattributes = @{nsforegroundcolorattributename: [uicolor bluecolor],
nsunderlinecolorattributename: [uicolor blackcolor],
nsunderlinestyleattributename: @(nsunderlinepatterndash)};
self.linktextattributes = linkattributes;
这里只是个简单的例子,具体还有很多属性可以自行参考头文件