【iOS - 周总结】开发中遇到的小知识点(2018.12.10-2018.12.15)

1.WKWebview加载html文本图片过大,没有自适应屏幕宽高。

在用Webview加载html文本有时候会遇到加载的图片过大,不能自适应屏幕宽高的问题。那么如何解决这个问题?如何使图片自适应屏幕?很简单,只需要加一个js就可以。

直接上代码:

- (WKWebView *)detailWeb {
    if (!_detailWeb) {
        
        NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta); var imgs = document.getElementsByTagName('img');for (var i in imgs){imgs[i].style.maxWidth='100%';imgs[i].style.height='auto';}";
        
        WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        WKUserContentController *wkUController = [[WKUserContentController alloc] init];
        [wkUController addUserScript:wkUScript];
        
        WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
        wkWebConfig.userContentController = wkUController;
        
        _detailWeb = [[WKWebView alloc] initWithFrame:CGRectMake(0, AdaptedWidth(40), SCREEN_WIDTH, SCREEN_HEIGHT-TopNavHeight-AdaptedWidth(40)) configuration:wkWebConfig];
        _detailWeb.backgroundColor = [UIColor whiteColor];
        _detailWeb.navigationDelegate = self;

    }
    return _detailWeb;
}

2.scrollview使用Masnory自动布局

近期在项目中使用了Masnory。在一个关于我们的页面由于数据网络请求下来,且内容长度不一定,我就决定使用Masnory+ScrollView来实现。结果就出现了一些些问题。

使用过程就在代码里显示。

#import "ViewController.h"

#import "Masonry/Masonry.h"

@interface ViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UIView *scrollContentView;

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"1";
    
    // 1.把scrollView添加到控制器view
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];

    // 2.给scrollView添加一个containerView
    // 重点 唯一 大小贴合scrollview
    // 即scrollView的子视图,即过渡视图contentSize,并设置其约束
    [self.scrollView addSubview:self.scrollContentView];
    [self.scrollContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        // 注意点:首先确定contentView的位置。如果上下滑动等宽 左右滑动等高 类似于xib
        make.left.top.right.bottom.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
    }];
    
    // 3.此后所有视图添加在scrollContentView上
    [self.scrollContentView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        // 注意点这里面设置子视图都要添加在contentView上 必须注意
        make.left.equalTo(self.scrollContentView.mas_left).offset(20);
        make.right.equalTo(self.scrollContentView.mas_right).offset(-20);
        make.top.equalTo(self.scrollContentView.mas_top).offset(100);
        make.height.mas_equalTo(40);
    }];

    [self.scrollContentView addSubview:self.subLabel];
    [self.subLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.scrollContentView.mas_left).offset(20);
        make.right.equalTo(self.scrollContentView.mas_right).offset(-20);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(40);
        
        // 这个可写在这个位置 也可以在网络请求结束后添加
        make.bottom.equalTo(self.scrollContentView.mas_bottom).offset(-100);
    }];
  
    // 模拟网络请求
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.subLabel.text = @"精雕机哦跌跤就叠加偶家迪欧家饿哦就掉得哦啊基金的年纪大递交炯炯都我阿尔加到的骄傲的   待机饿哦案件都安静哦啊接打带哦接啊哦啊接待偶尔接爱哦的旧爱带哦精雕机带偶觉得 基调而骄傲的奥迪偶记带哦精雕机哦跌跤就叠加偶家迪欧家饿哦就掉得哦啊基金的年纪大递交炯炯都我阿尔加到的骄傲的   待机饿哦案件都安静哦啊接打带哦接啊哦啊接待偶尔接爱哦的旧爱带哦精雕机带偶觉得 基调而骄傲的奥迪偶记带哦精雕机哦跌跤就叠加偶家迪欧家饿哦就掉得哦啊基金的年纪大递交炯炯都我阿尔加到的骄傲的   待机饿哦案件都安静哦啊接打带哦接啊哦啊接待偶尔接爱哦的旧爱带哦精雕机带偶觉得 基调而骄傲的奥迪偶记带哦精雕机哦跌跤就叠加偶家迪欧家饿哦就掉得哦啊基金的年纪大递交炯炯都我阿尔加到的骄傲的   待机饿哦案件都安静哦啊接打带哦接啊哦啊接待偶尔接爱哦的旧爱带哦精雕机带偶觉得 基调而骄傲的奥迪偶记带哦精雕机哦跌跤就叠加偶家迪欧家饿哦就掉得哦啊基金的年纪大递交炯炯都我阿尔加到的骄傲的   待机饿哦案件都安静哦啊接打带哦接啊哦啊接待偶尔接爱哦的旧爱带哦精雕机带偶觉得 基调而骄傲的奥迪偶记带哦精雕机哦跌跤就叠加偶家迪欧家饿哦就掉得哦啊基金的年纪大递交炯炯都我阿尔加到的骄傲的   待机饿哦案件都安静哦啊接打带哦接啊哦啊接待偶尔接爱哦的旧爱带哦精雕机带偶觉得 基调而骄傲的奥迪偶记带哦";

        // 在上面写了这个位置可以不写 最好不要都写  会造成代码混乱
//        // 设置过渡视图的底边距(此设置将影响到scrollView的contentSize)
//        [self.scrollContentView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.bottom.equalTo(self.subLabel.mas_bottom).offset(30);
//        }];
    });

    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark - Lazy Setter
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.backgroundColor = [UIColor greenColor];
    }
    return _scrollView;
}

- (UIView *)scrollContentView {
    if (!_scrollContentView) {
        _scrollContentView = [[UIView alloc] init];
        _scrollContentView.backgroundColor = [UIColor redColor];
    }
    return _scrollContentView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.text = @"title";
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.backgroundColor = [UIColor whiteColor];
        _titleLabel.font = [UIFont systemFontOfSize:16];
        _titleLabel.numberOfLines = 1;
    }
    return _titleLabel;
}

- (UILabel *)subLabel {
    if (!_subLabel) {
        _subLabel = [[UILabel alloc] init];
        _subLabel.textColor = [UIColor blackColor];
        _subLabel.backgroundColor = [UIColor whiteColor];
        _subLabel.font = [UIFont systemFontOfSize:16];
        _subLabel.numberOfLines = 0;
    }
    return _subLabel;
}

@end

参考:博客一博客二

3.设置label的最小字体。

 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 100, 100, 30)];
    label.text = @"fjirfjirjfi积分敌人的积极对得金蝶鸡的解决跌阿达啊打发 啊打发";
    label.adjustsFontSizeToFitWidth = YES;
    label.font = [UIFont systemFontOfSize:18];
    label.textColor = [UIColor blackColor];
//    label.minimumFontSize = 14;
    label.minimumScaleFactor = 0.5;
    [self.view addSubview:label];

在6.0系统后minimumFontSize属性已经被弃用。推荐使用minimumScaleFactor。

注意点:

    1、0<minimumScaleFactor<1时才能达到效果。(字体10,想要最小字体5,设置0.5即可)

    2、另外要设置adjustsFontSizeToFitWidth=YES.

    3、还有需要UIlabel的行数是1的时候才有用,多行的label是不行的

    4、minimumScaleFactor默认值是0,此时最小字体就是设置的字体大小,和1的效果一样。

猜你喜欢

转载自www.cnblogs.com/weicyNo-1/p/10123555.html