ios 日常记录 不定时更新

325 阅读1分钟

1.一行代码收起键盘

tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;

2.取消tableview的分割线

TableView.separatorStyle = UITableViewCellAccessoryNone

3.取消头部视图跟随效果

//切换tableview的style  UITableViewStyleGrouped头部视图不跟随   UITableViewStylePlain  头部视图不跟随

self.mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 50) style:UITableViewStyleGrouped]

4.解决tableview视图上移

方案一
self.automaticallyAdjustsScrollViewInsets = NO;// 默认是YES

方案二
self.edgesForExtendedLayout = UIRectEdgeNone;// 推荐使用

5.html字符串转换成富文本

NSAttributedString *att = [[NSAttributedString alloc] initWithData:[model.detail dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];

通过设置控件的字体大小来改变字的大小,位置要放在给控件赋值之后

6.计算文本高度

-(CGSize)sizeWithString:(NSString *)string font:(UIFont *)font
{
    CGRect rect = [string boundingRectWithSize:CGSizeMake(kWidth-30,MAXFLOAT) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading  |NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: font} context:nil];
    
    return rect.size;
}

7.获取点击cell的indexpath

//获取当前点击cell的row或者section
self.tableView.indexPathForSelectedRow.row
self.tableView.indexPathForSelectedRow.section

//获取当前点击cell的indexpath
self.mainView.indexPathsForSelectedRows

8.设置label不同位置字体大小和颜色

//设置不同字体颜色
-(void)setTextColor:(UILabel *)label FontNumber:(id)font AndRange:(NSRange)range AndColor:(UIColor *)vaColor
{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:label.text];
    //设置字号
    [str addAttribute:NSFontAttributeName value:font range:range];
    //设置文字颜色
    [str addAttribute:NSForegroundColorAttributeName value:vaColor range:range];
    
    label.attributedText = str;
}

9.UITextView :启动时文字位置不从顶部开始

- (void)viewDidLayoutSubviews {
    [self.textView setContentOffset:CGPointZero animated:NO];
}

10.获取HTML字符串的文本信息

-(NSString *)filterHTML:(NSString *)html
{
    NSScanner * scanner = [NSScanner scannerWithString:html];
    NSString * text = nil;
    while([scanner isAtEnd]==NO)
    {
        [scanner scanUpToString:@"<" intoString:nil];
        [scanner scanUpToString:@">" intoString:&text];
        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
        //去除空格 &nbsp;
        html = [html stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@""];
    }
    return html;
}