UITextView、UITextFeild、block界面传值

1、UITextView小结
{
    UITextView *textview = [[UITextView alloc] initWithFrame:CGRectMake(0, 15, self.view.frame.size.width, 45)];
//控制textView中的文字垂直居中
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0) {
        textview.textContainerInset = UIEdgeInsetsMake(12, 5, 12, 5);
    }
    textview.backgroundColor = [UIColor whiteColor];
//文字左端对齐
    textview.textAlignment = NSTextAlignmentLeft;
    textview.font = [UIFont systemFontOfSize:16.0];
//用16进制设置的颜色设置UIColor
    textview.textColor = [UIColor colorWithHexString:@"333333"];
//设置代理实现控制输入字数,以及不允许输入表情
    textview.delegate = self;//UITextViewDelegate
//设置textview为第一响应者,聚焦
  [textview becomeFirstResponder];
}

#pragma mark - UITextViewDelegate Methods
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
//限制textView中输入的字数不超过20
    if (textView.text.length + text.length > 20) {
        int remainLength = 20 -(int)textView.text.length;
        if (remainLength > 0) {
            textView.text = [NSString stringWithFormat:@"%@%@", textView.text, [text substringToIndex:remainLength]];
        }
        return NO;
    } else {
       //不允许输入换行
        if ( [text isEqualToString:@"\n"] ) {
            //Do whatever you want
            return NO;
        }
        return YES;
    }
}
-(void)textViewDidChange:(UITextView *)textView
{
    //防止输入法的联想,导致输入字数超过20
    if (textView.text.length > 20) {
        dispatch_async(dispatch_get_main_queue(), ^{
            textView.text = [textView.text substringToIndex:20];
        });
    }
 //自适应高度
    CGRect frame = textView.frame;
    frame.size.height = textView.contentSize.height;
    textView.frame = frame;
}
2、UITextFeild小结
{
self.textfeild = [[UITextField alloc] initWithFrame:CGRectMake(0, 15, self.view.frame.size.width, 45)];
    self.textfeild.backgroundColor = [UIColor whiteColor];
//文字左端对齐
    self.textfeild.textAlignment = NSTextAlignmentLeft;
    self.textfeild.font = [UIFont systemFontOfSize:16.0];
    self.textfeild.textColor = [UIColor colorWithHexString:@"333333"];
//设置代理实现不允许输入表情
    self.textfeild.delegate = self;//UITextFieldDelegate
    self.textfeild.text = self.text;
//设置清除按钮
    self.textfeild.clearButtonMode = UITextFieldViewModeWhileEditing;
//设置控制输入字数的监听
    [self.textfeild addTarget:self action:@selector(textChange:) 
forControlEvents:UIControlEventEditingChanged];
//垂直对齐
    self.textfeild.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//向左偏移10
    UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 20)];
    self.textfeild.leftView = paddingView;
    self.textfeild.leftViewMode = UITextFieldViewModeAlways;
//聚焦
    [self.textfeild becomeFirstResponder];
}

#pragma mark - UITextFeildDelegate Methods
- (void)textChange:(UITextField *)textField
{
 //防止输入法的联想,导致输入字数超过20
    if (textField.text.length > 20) {
        dispatch_async(dispatch_get_main_queue(), ^{
            textField.text = [textField.text substringToIndex:20];
        });
    }
//限制textView中输入的字数不超过20
    if ([textField.text length] > 20) {
        UITextRange *selectedRange = textField.markedTextRange;
        if (selectedRange == nil || selectedRange.empty) {
            textField.text = [textField.text substringToIndex:20];
        }
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //禁止输入表情
    if ([[[UITextInputMode currentInputMode] primaryLanguage] isEqual:@"emoji"]) {
        return NO;
    }
    return YES;
}

UITextFeild参考链接

3、dictionary的enumerateObjectsUsingBlock取值方法
{
        [dataDic.allKeys enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSLog(@" = %@, %@ = %@ = %@ , %p", [dataDic[obj] class], dataDic[obj], [obj class], obj ,dataDic[obj]);
        }];
}
4、利用block实现界面传值
typedef void (^RefreshBlock)(NSString *text, NSString *title);

@interface B : UIViewController 
@property (nonatomic, copy) RefreshBlock refreshBlock;
@end

@implementation B
{
//在想要调用A界面刷新的时候
 self.refreshBlock(str1, str2);
}
@end

@implementation A
{
  B *b = [[B alloc] init];
   __weak A *self1 = self;
   b.refreshBlock = ^(NSString *text, NSString *title) {
       //在这里实现A界面的刷新
      [self1 re....];
   };
   [self.navigationController pushViewController:b animated:YES];
}
@end
5、圆角图片的实现

代码实现:

{
ImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(x, view.frame.size.height / 3 - 35, 70, 70)];
//图片宽度的1/2为圆形
Icon.layer.cornerRadius = self.headIcon.frame.size.width / 2;
Icon.layer.masksToBounds = YES;
}

xib实现:

UITextView、UITextFeild、block界面传值_第1张图片
xib实现圆角

6、给ImageView添加监听事件
ImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(x, view.frame.size.height / 3 - 35, 70, 70)];
[icon addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(editHeadIcon)]];

你可能感兴趣的:(UITextView、UITextFeild、block界面传值)