自学知识(二)

1.判断ipad还是iphone来设置表视图行高的尺寸:

1  self.tableView.rowHeight = [[UIDevice currentDevice].model hasSuffix:@"iPad"] ? 60.0 : 44.0;

2.富文本显示文字和图片:

1  NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:@""];
2     NSTextAttachment *attatchment = [[NSTextAttachment alloc] init];
3     attatchment.image = [UIImage imageNamed:@"icon_cat"];
4     attatchment.bounds = CGRectMake(0, -3, attatchment.image.size.width, attatchment.image.size.height);
5     [attributedText appendAttributedString:[NSAttributedString attributedStringWithAttachment:attatchment]];
6     [attributedText appendAttributedString:[[NSAttributedString alloc] initWithString:@"  Hey Daily Event  "]];
7     [attributedText appendAttributedString:[NSAttributedString attributedStringWithAttachment:attatchment]];
8     self.eventLabel.attributedText = attributedText.copy;

3.自定义键盘上的按钮:

 1   //在键盘上加上一个ToolBar,在上面加上一个按钮来隐藏键盘
 2     
 3     //定义一个toolBar
 4     UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
 5     
 6     //设置style
 7     [topView setBarStyle:UIBarStyleBlack];
 8     
 9     //定义两个flexibleSpace的button,放在toolBar上,这样完成按钮就会在最右边
10     UIBarButtonItem * button1 =[[UIBarButtonItem  alloc]initWithBarButtonSystemItem:                                        UIBarButtonSystemItemFlexibleSpace target:self action:nil];
11     
12     UIBarButtonItem * button2 = [[UIBarButtonItem  alloc]initWithBarButtonSystemItem:                                        UIBarButtonSystemItemFlexibleSpace target:self action:nil];
13     
14     //定义完成按钮
15     UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone  target:self action:@selector(resignKeyboard)];
16     
17     //在toolBar上加上这些按钮
18     NSArray * buttonsArray = [NSArray arrayWithObjects:button1,button2,doneButton,nil];
19     [topView setItems:buttonsArray];
20     
21     [textView setInputAccessoryView:topView];
22     
1 //点击完成按钮
2 - (void)resignKeyboard{
3     
4     [textView resignFirstResponder];
5 }

4.监听键盘的弹出与隐藏:

1  //注册通知,监听键盘弹出事件
2     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
3     
4     //注册通知,监听键盘消失事件
5     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHidden) name:UIKeyboardDidHideNotification object:nil];

5.手势触摸问题:

 1 - (void)viewDidLoad{
 2     
 3 
 4     [super viewDidLoad];
 5     
 6     
 7     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
 8     [self.view addGestureRecognizer:tap];
 9     
10 }
11 
12 //后触发
13 - (void)tap:(UITapGestureRecognizer *)tap{
14     
15     NSLog(@"点击手势被触发");
16     
17 }
18 
19 //先触发
20 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
21     
22     NSLog(@"被触摸");
23     
24 }

6.可以使用通知解决手机的翻转问题:

 1  //检测手机翻转
 2     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(orientationChanged) name:UIDeviceOrientationDidChangeNotification object:nil];
 3     
 4     
 5 }
 6 
 7 
 8 - (void)orientationChanged{
 9     
10     switch ([[UIDevice currentDevice] orientation]) {
11         case UIDeviceOrientationPortrait:
12             NSLog(@"portrait");
13             break;
14         case UIDeviceOrientationPortraitUpsideDown:
15             NSLog(@"portraitUpSideDown");
16             break;
17         case UIDeviceOrientationLandscapeLeft:
18             NSLog(@"landscapeLeft");
19             break;
20         case UIDeviceOrientationLandscapeRight:
21             NSLog(@"landscapeRight");
22             break;
23         case UIDeviceOrientationFaceDown:
24             NSLog(@"facedown!!");
25             break;
26         case UIDeviceOrientationFaceUp:
27             NSLog(@"FaceUp");
28             break;
29         default:
30             break;
31     }
32     
原文地址:https://www.cnblogs.com/pengsi/p/5344730.html