IOS中UITextView控件的一些使用方法

来源:互联网 发布:苹果同步铃声软件 编辑:程序博客网 时间:2024/05/29 05:52

1、设置UITextView的属性

textView.returnKeyType = UIReturnKeyDefault;//返回键的类型  textView.keyboardType = UIKeyboardTypeDefault;//键盘类型  
textView.scrollEnabled = YES;//是否可以拖动   textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;//自适应高度


2、设置UITextView圆角问题
先引入#import <QuartzCore/QuartzCore.h>

textView.layer.cornerRadius = 8; //给图层的边框设置为圆角 textView.layer.masksToBounds = YES;


3、给TextView加一个有色边框,并设置背景图片
//给图层添加一个有色边框


 textView.layer.borderWidth = 5;
textView.layer.borderColor = [[UIColor colorWithRed:0.52 green:0.09 blue:0.07 alpha:0.5] CGColor];
 textView.layer.contents = (id)[UIImage imageNamed:@"xxxxx"].CGImage; //给图层添加背景图片


4、UITextView自定选择文字后的菜单
在ViewDidLoad中加入:

- (void)viewDidLoad
{
    
[super viewDidLoad];
    
self._textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 100, 300, 200)];//初始化
  
  _textView.font = [UIFont fontWithName:@"Arial" size:33];//设置字体名字和字体大小
    
[self.view addSubview:_textView];
    
    
UIMenuItem *menuItem = [[UIMenuItem alloc]initWithTitle:@"分享到新浪微博" action:@selector(fun)]; 
   
 UIMenuController *menu = [UIMenuController sharedMenuController]; 
   
 [menu setMenuItems:[NSArray arrayWithObject:menuItem]]; 
   
 [menuItem release]; 
}


@selector里面的fun方法是自己写的函数,也就是说点击了我们自定义的菜单项后会触发的方法。
然后还得在代码里加上一个方法:
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender 
{ 
    
if(action ==@selector(changeColor) || action == @selector(copy:)) 
    { 
    
    if(_textView.selectedRange.length>0) 
           
 return YES; 
    } 
   
 return NO; 
} 

-(void) fun
{
    
NSLog(@"%@",[_textView.text substringWithRange:_textView.selectedRange]);

}


原创粉丝点击