程序员首页投稿(暂停使用,暂停投稿)

UITableView 修改自带检索栏字体大小

2017-12-15  本文已影响367人  Jniying

UITableView 使用自带的检索栏,是不能修改的,如何修改?

HeaderView 将要显示之前 获取 UITableViewIndex


-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    for (UIView *view in [tableView subviews]) {
        if ([view isKindOfClass:[NSClassFromString(@"UITableViewIndex") class]]) {
           // 设置字体大小
            [view setValue:FontSize forKey:@"_font"];
           //设置view的大小
            view.bounds = CGRectMake(0, 0, 30, 30);
            //单单设置其中一个是无效的
        }
    }

}

UITableViewIndex 属性详解

titles      //不知名属性
font       //字体大小
drawingInsets  //拓展范围
indexColor  //字体颜色
indexBackgroundColor //背景颜色
indexTrackingBackgroundColor 
selectedSection  //选中之类
selectedSectionTitle  //猜测试 indexTitle
pastTop 
pastBottom

运行时获取 UITableViewIndex 所有属性
如需要查看自行 #import <objc/runtime.h> 以下代码获取

            // 获取当前类的所有属性
            unsigned int count;// 记录属性个数
            objc_property_t *properties = class_copyPropertyList([view class], &count);
            // 遍历
            for (int i = 0; i < count; i++) {
                
                // An opaque type that represents an Objective-C declared property.
                // objc_property_t 属性类型
                objc_property_t property = properties[i];
                // 获取属性的名称 C语言字符串
                const char *cName = property_getName(property);
                // 转换为Objective C 字符串
                NSString *name = [NSString stringWithCString:cName encoding:NSUTF8StringEncoding];
                NSLog(@"%@",name);
            }
猜测 UITableViewIndex 原理: UITableViewIndex 跟 UILabel 差不多,都是把字体和直接 draw 出来.为什么单单设置font 不能解决问题? 是UITableViewIndex 排版出来之后 就已经固定了大小.所以 设置单单设置 font 无效.
上一篇 下一篇

猜你喜欢

热点阅读