当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS应用开发中的文字选中操作控件UITextView用法讲解

iOS应用开发中的文字选中操作控件UITextView用法讲解

2019年07月24日  | 移动技术网IT编程  | 我要评论

索尼彩电,斑秃的治疗,轻机枪图片

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;   
}  

实现后如下图:

201621790458014.png (311×166)

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; 

这里只是个简单的例子,具体还有很多属性可以自行参考头文件

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网